Remove a return path from sshcom_write() which was both unreachable
[u/mdw/putty] / cmdgen.c
CommitLineData
47a6b94c 1/*
2 * cmdgen.c - command-line form of PuTTYgen
3 */
4
47a6b94c 5#define PUTTY_DO_GLOBALS
6
7#include <stdio.h>
8#include <stdlib.h>
9#include <ctype.h>
10#include <limits.h>
11#include <assert.h>
12#include <time.h>
13
14#include "putty.h"
15#include "ssh.h"
16
b81cca98 17#ifdef TEST_CMDGEN
18/*
19 * This section overrides some definitions below for test purposes.
20 * When compiled with -DTEST_CMDGEN:
21 *
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
26 * unnecessarily.
27 *
edd0cb8a 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
31 * inputs.
b81cca98 32 *
33 * - main() is renamed to cmdgen_main(); at the bottom of the file
34 * I define another main() which calls the former repeatedly to
35 * run tests.
36 */
47a6b94c 37#define get_random_data get_random_data_diagnostic
38char *get_random_data(int len)
39{
40 char *buf = snewn(len, char);
41 memset(buf, 'x', len);
42 return buf;
43}
edd0cb8a 44#define console_get_userpass_input console_get_userpass_input_diagnostic
b81cca98 45int nprompts, promptsgot;
46const char *prompts[3];
edd0cb8a 47int console_get_userpass_input(prompts_t *p, unsigned char *in, int inlen)
b81cca98 48{
edd0cb8a 49 size_t i;
50 int ret = 1;
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++]);
55 } else {
56 promptsgot++; /* track number of requests anyway */
57 ret = 0;
58 }
b81cca98 59 }
edd0cb8a 60 return ret;
b81cca98 61}
62#define main cmdgen_main
47a6b94c 63#endif
64
65struct progress {
66 int phase, current;
67};
68
69static void progress_update(void *param, int action, int phase, int iprogress)
70{
71 struct progress *p = (struct progress *)param;
72 if (action != PROGFN_PROGRESS)
73 return;
74 if (phase > p->phase) {
75 if (p->phase >= 0)
76 fputc('\n', stderr);
77 p->phase = phase;
78 if (iprogress >= 0)
79 p->current = iprogress - 1;
80 else
81 p->current = iprogress;
82 }
83 while (p->current < iprogress) {
84 fputc('+', stdout);
85 p->current++;
86 }
87 fflush(stdout);
88}
89
90static void no_progress(void *param, int action, int phase, int iprogress)
91{
92}
93
94void modalfatalbox(char *p, ...)
95{
96 va_list ap;
97 fprintf(stderr, "FATAL ERROR: ");
98 va_start(ap, p);
99 vfprintf(stderr, p, ap);
100 va_end(ap);
101 fputc('\n', stderr);
102 cleanup_exit(1);
103}
104
105/*
106 * Stubs to let everything else link sensibly.
107 */
108void log_eventlog(void *handle, const char *event)
109{
110}
111char *x_get_default(const char *key)
112{
113 return NULL;
114}
115void sk_cleanup(void)
116{
117}
118
119void showversion(void)
120{
bcb1823f 121 printf("puttygen: %s\n", ver);
47a6b94c 122}
123
e49a814b 124void usage(int standalone)
47a6b94c 125{
126 fprintf(stderr,
127 "Usage: puttygen ( keyfile | -t type [ -b bits ] )\n"
27507d53 128 " [ -C comment ] [ -P ] [ -q ]\n"
136d127a 129 " [ -o output-keyfile ] [ -O type | -l | -L"
130 " | -p ]\n");
e49a814b 131 if (standalone)
132 fprintf(stderr,
133 "Use \"puttygen --help\" for more detail.\n");
47a6b94c 134}
135
136void help(void)
137{
138 /*
139 * Help message is an extended version of the usage message. So
140 * start with that, plus a version heading.
141 */
142 showversion();
e49a814b 143 usage(FALSE);
47a6b94c 144 fprintf(stderr,
145 " -t specify key type when generating (rsa, dsa, rsa1)\n"
136d127a 146 " -b specify number of bits when generating key\n"
47a6b94c 147 " -C change or specify key comment\n"
148 " -P change key passphrase\n"
27507d53 149 " -q quiet: do not display progress bar\n"
47a6b94c 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"
136d127a 157 " -o specify output file\n"
47a6b94c 158 " -l equivalent to `-O fingerprint'\n"
159 " -L equivalent to `-O public-openssh'\n"
160 " -p equivalent to `-O public'\n"
161 );
162}
163
164static int save_ssh2_pubkey(char *filename, char *comment,
165 void *v_pub_blob, int pub_len)
166{
167 unsigned char *pub_blob = (unsigned char *)v_pub_blob;
168 char *p;
169 int i, column;
170 FILE *fp;
171
172 if (filename) {
173 fp = fopen(filename, "wb");
174 if (!fp)
175 return 0;
176 } else
177 fp = stdout;
178
179 fprintf(fp, "---- BEGIN SSH2 PUBLIC KEY ----\n");
180
181 if (comment) {
182 fprintf(fp, "Comment: \"");
183 for (p = comment; *p; p++) {
184 if (*p == '\\' || *p == '\"')
185 fputc('\\', fp);
186 fputc(*p, fp);
187 }
188 fprintf(fp, "\"\n");
189 }
190
191 i = 0;
192 column = 0;
193 while (i < pub_len) {
194 char buf[5];
195 int n = (pub_len - i < 3 ? pub_len - i : 3);
196 base64_encode_atom(pub_blob + i, n, buf);
197 i += n;
198 buf[4] = '\0';
199 fputs(buf, fp);
200 if (++column >= 16) {
201 fputc('\n', fp);
202 column = 0;
203 }
204 }
205 if (column > 0)
206 fputc('\n', fp);
207
208 fprintf(fp, "---- END SSH2 PUBLIC KEY ----\n");
209 if (filename)
210 fclose(fp);
211 return 1;
212}
213
b81cca98 214static int move(char *from, char *to)
47a6b94c 215{
216 int ret;
217
218 ret = rename(from, to);
219 if (ret) {
220 /*
221 * This OS may require us to remove the original file first.
222 */
223 remove(to);
224 ret = rename(from, to);
225 }
226 if (ret) {
227 perror("puttygen: cannot move new file on to old one");
b81cca98 228 return FALSE;
47a6b94c 229 }
b81cca98 230 return TRUE;
47a6b94c 231}
232
8a9977e5 233static char *blobfp(char *alg, int bits, unsigned char *blob, int bloblen)
47a6b94c 234{
235 char buffer[128];
236 unsigned char digest[16];
237 struct MD5Context md5c;
238 int i;
239
240 MD5Init(&md5c);
241 MD5Update(&md5c, blob, bloblen);
242 MD5Final(digest, &md5c);
243
244 sprintf(buffer, "%s ", alg);
245 if (bits > 0)
246 sprintf(buffer + strlen(buffer), "%d ", bits);
247 for (i = 0; i < 16; i++)
248 sprintf(buffer + strlen(buffer), "%s%02x", i ? ":" : "",
249 digest[i]);
250
251 return dupstr(buffer);
252}
253
254int main(int argc, char **argv)
255{
256 char *infile = NULL;
cfcf7910 257 Filename *infilename = NULL, *outfilename = NULL;
47a6b94c 258 enum { NOKEYGEN, RSA1, RSA2, DSA } keytype = NOKEYGEN;
259 char *outfile = NULL, *outfiletmp = NULL;
47a6b94c 260 enum { PRIVATE, PUBLIC, PUBLICO, FP, OPENSSH, SSHCOM } outtype = PRIVATE;
50d7de97 261 int bits = 2048;
47a6b94c 262 char *comment = NULL, *origcomment = NULL;
263 int change_passphrase = FALSE;
264 int errs = FALSE, nogo = FALSE;
265 int intype = SSH_KEYTYPE_UNOPENABLE;
266 int sshver = 0;
267 struct ssh2_userkey *ssh2key = NULL;
268 struct RSAKey *ssh1key = NULL;
8a9977e5 269 unsigned char *ssh2blob = NULL;
270 char *ssh2alg = NULL;
47a6b94c 271 const struct ssh_signkey *ssh2algf = NULL;
272 int ssh2bloblen;
273 char *passphrase = NULL;
274 int load_encrypted;
275 progfn_t progressfn = is_interactive() ? progress_update : no_progress;
276
277 /* ------------------------------------------------------------------
278 * Parse the command line to figure out what we've been asked to do.
279 */
280
281 /*
282 * If run with no arguments at all, print the usage message and
283 * return success.
284 */
285 if (argc <= 1) {
e49a814b 286 usage(TRUE);
47a6b94c 287 return 0;
288 }
289
290 /*
291 * Parse command line arguments.
292 */
293 while (--argc) {
294 char *p = *++argv;
295 if (*p == '-') {
296 /*
297 * An option.
298 */
299 while (p && *++p) {
300 char c = *p;
301 switch (c) {
302 case '-':
303 /*
304 * Long option.
305 */
306 {
307 char *opt, *val;
308 opt = p++; /* opt will have _one_ leading - */
309 while (*p && *p != '=')
310 p++; /* find end of option */
311 if (*p == '=') {
312 *p++ = '\0';
313 val = p;
314 } else
3ab79841 315 val = NULL;
316
47a6b94c 317 if (!strcmp(opt, "-help")) {
3ab79841 318 if (val) {
319 errs = TRUE;
320 fprintf(stderr, "puttygen: option `-%s'"
321 " expects no argument\n", opt);
322 } else {
323 help();
324 nogo = TRUE;
325 }
47a6b94c 326 } else if (!strcmp(opt, "-version")) {
3ab79841 327 if (val) {
328 errs = TRUE;
329 fprintf(stderr, "puttygen: option `-%s'"
330 " expects no argument\n", opt);
331 } else {
332 showversion();
333 nogo = TRUE;
334 }
2285d016 335 } else if (!strcmp(opt, "-pgpfp")) {
3ab79841 336 if (val) {
337 errs = TRUE;
338 fprintf(stderr, "puttygen: option `-%s'"
339 " expects no argument\n", opt);
340 } else {
341 /* support --pgpfp for consistency */
342 pgp_fingerprints();
343 nogo = TRUE;
344 }
2285d016 345 }
47a6b94c 346 /*
3ab79841 347 * For long options requiring an argument, add
348 * code along the lines of
47a6b94c 349 *
350 * else if (!strcmp(opt, "-output")) {
3ab79841 351 * if (!val) {
352 * errs = TRUE;
353 * fprintf(stderr, "puttygen: option `-%s'"
354 * " expects an argument\n", opt);
355 * } else
47a6b94c 356 * ofile = val;
357 * }
358 */
359 else {
360 errs = TRUE;
361 fprintf(stderr,
27507d53 362 "puttygen: no such option `-%s'\n", opt);
47a6b94c 363 }
364 }
365 p = NULL;
366 break;
367 case 'h':
368 case 'V':
369 case 'P':
370 case 'l':
371 case 'L':
372 case 'p':
373 case 'q':
374 /*
375 * Option requiring no parameter.
376 */
377 switch (c) {
378 case 'h':
379 help();
380 nogo = TRUE;
381 break;
382 case 'V':
383 showversion();
384 nogo = TRUE;
385 break;
386 case 'P':
387 change_passphrase = TRUE;
388 break;
389 case 'l':
390 outtype = FP;
391 break;
392 case 'L':
393 outtype = PUBLICO;
394 break;
395 case 'p':
396 outtype = PUBLIC;
397 break;
398 case 'q':
399 progressfn = no_progress;
400 break;
401 }
402 break;
403 case 't':
404 case 'b':
405 case 'C':
406 case 'O':
407 case 'o':
408 /*
409 * Option requiring parameter.
410 */
411 p++;
412 if (!*p && argc > 1)
413 --argc, p = *++argv;
414 else if (!*p) {
415 fprintf(stderr, "puttygen: option `-%c' expects a"
416 " parameter\n", c);
417 errs = TRUE;
418 }
419 /*
420 * Now c is the option and p is the parameter.
421 */
422 switch (c) {
423 case 't':
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;
430 else {
431 fprintf(stderr,
432 "puttygen: unknown key type `%s'\n", p);
433 errs = TRUE;
434 }
435 break;
436 case 'b':
437 bits = atoi(p);
438 break;
439 case 'C':
440 comment = p;
441 break;
442 case 'O':
443 if (!strcmp(p, "public"))
444 outtype = PUBLIC;
445 else if (!strcmp(p, "public-openssh"))
446 outtype = PUBLICO;
447 else if (!strcmp(p, "private"))
448 outtype = PRIVATE;
449 else if (!strcmp(p, "fingerprint"))
450 outtype = FP;
451 else if (!strcmp(p, "private-openssh"))
452 outtype = OPENSSH, sshver = 2;
453 else if (!strcmp(p, "private-sshcom"))
454 outtype = SSHCOM, sshver = 2;
455 else {
456 fprintf(stderr,
457 "puttygen: unknown output type `%s'\n", p);
458 errs = TRUE;
459 }
460 break;
461 case 'o':
462 outfile = p;
463 break;
464 }
465 p = NULL; /* prevent continued processing */
466 break;
467 default:
468 /*
469 * Unrecognised option.
470 */
471 errs = TRUE;
472 fprintf(stderr, "puttygen: no such option `-%c'\n", c);
473 break;
474 }
475 }
476 } else {
477 /*
478 * A non-option argument.
479 */
480 if (!infile)
481 infile = p;
482 else {
483 errs = TRUE;
484 fprintf(stderr, "puttygen: cannot handle more than one"
485 " input file\n");
486 }
487 }
488 }
489
490 if (errs)
491 return 1;
492
493 if (nogo)
494 return 0;
495
496 /*
497 * If run with at least one argument _but_ not the required
498 * ones, print the usage message and return failure.
499 */
500 if (!infile && keytype == NOKEYGEN) {
e49a814b 501 usage(TRUE);
47a6b94c 502 return 1;
503 }
504
505 /* ------------------------------------------------------------------
506 * Figure out further details of exactly what we're going to do.
507 */
508
509 /*
510 * Bomb out if we've been asked to both load and generate a
511 * key.
512 */
27507d53 513 if (keytype != NOKEYGEN && infile) {
47a6b94c 514 fprintf(stderr, "puttygen: cannot both load and generate a key\n");
515 return 1;
516 }
517
27507d53 518 /*
519 * We must save the private part when generating a new key.
520 */
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");
525 return 1;
526 }
527
47a6b94c 528 /*
529 * Analyse the type of the input file, in case this affects our
530 * course of action.
531 */
532 if (infile) {
533 infilename = filename_from_str(infile);
534
962468d4 535 intype = key_type(infilename);
47a6b94c 536
537 switch (intype) {
538 /*
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!
544 *
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!
558 */
559
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));
564 return 1;
565
566 case SSH_KEYTYPE_SSH1:
567 if (sshver == 2) {
2e85c969 568 fprintf(stderr, "puttygen: conversion from SSH-1 to SSH-2 keys"
47a6b94c 569 " not supported\n");
570 return 1;
571 }
572 sshver = 1;
573 break;
574
575 case SSH_KEYTYPE_SSH2:
576 case SSH_KEYTYPE_OPENSSH:
577 case SSH_KEYTYPE_SSHCOM:
578 if (sshver == 1) {
2e85c969 579 fprintf(stderr, "puttygen: conversion from SSH-2 to SSH-1 keys"
47a6b94c 580 " not supported\n");
581 return 1;
582 }
583 sshver = 2;
584 break;
585 }
586 }
587
588 /*
589 * Determine the default output file, if none is provided.
590 *
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.
594 *
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.
597 */
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)) {
602 if (!outfile) {
603 outfile = infile;
b81cca98 604 outfiletmp = dupcat(outfile, ".tmp", NULL);
47a6b94c 605 }
606
607 if (!change_passphrase && !comment) {
608 fprintf(stderr, "puttygen: this command would perform no useful"
609 " action\n");
610 return 1;
611 }
612 } else {
613 if (!outfile) {
614 /*
615 * Bomb out rather than automatically choosing to write
616 * a private key file to stdout.
617 */
618 if (outtype==PRIVATE || outtype==OPENSSH || outtype==SSHCOM) {
619 fprintf(stderr, "puttygen: need to specify an output file\n");
620 return 1;
621 }
622 }
623 }
624
625 /*
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
629 * is encrypted.
630 */
631 if (outtype == PRIVATE || outtype == OPENSSH || outtype == SSHCOM ||
632 intype == SSH_KEYTYPE_OPENSSH || intype == SSH_KEYTYPE_SSHCOM)
633 load_encrypted = TRUE;
634 else
635 load_encrypted = FALSE;
636
637 /* ------------------------------------------------------------------
638 * Now we're ready to actually do some stuff.
639 */
640
641 /*
642 * Either load or generate a key.
643 */
644 if (keytype != NOKEYGEN) {
645 char *entropy;
646 char default_comment[80];
aca589d9 647 struct tm tm;
47a6b94c 648 struct progress prog;
649
650 prog.phase = -1;
651 prog.current = -1;
652
aca589d9 653 tm = ltime();
47a6b94c 654 if (keytype == DSA)
aca589d9 655 strftime(default_comment, 30, "dsa-key-%Y%m%d", &tm);
47a6b94c 656 else
aca589d9 657 strftime(default_comment, 30, "rsa-key-%Y%m%d", &tm);
47a6b94c 658
5d17ccfc 659 random_ref();
47a6b94c 660 entropy = get_random_data(bits / 8);
7da629c2 661 if (!entropy) {
662 fprintf(stderr, "puttygen: failed to collect entropy, "
663 "could not generate key\n");
664 return 1;
665 }
47a6b94c 666 random_add_heavynoise(entropy, bits / 8);
dfb88efd 667 smemclr(entropy, bits/8);
47a6b94c 668 sfree(entropy);
669
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;
676 ssh1key = NULL;
677 } else {
678 struct RSAKey *rsakey = snew(struct RSAKey);
679 rsa_generate(rsakey, bits, progressfn, &prog);
b81cca98 680 rsakey->comment = NULL;
47a6b94c 681 if (keytype == RSA1) {
682 ssh1key = rsakey;
683 } else {
684 ssh2key = snew(struct ssh2_userkey);
685 ssh2key->data = rsakey;
686 ssh2key->alg = &ssh_rsa;
687 }
688 }
689 progressfn(&prog, PROGFN_PROGRESS, INT_MAX, -1);
690
691 if (ssh2key)
692 ssh2key->comment = dupstr(default_comment);
693 if (ssh1key)
694 ssh1key->comment = dupstr(default_comment);
695
696 } else {
697 const char *error = NULL;
698 int encrypted;
699
700 assert(infile != NULL);
701
702 /*
703 * Find out whether the input key is encrypted.
704 */
705 if (intype == SSH_KEYTYPE_SSH1)
962468d4 706 encrypted = rsakey_encrypted(infilename, &origcomment);
47a6b94c 707 else if (intype == SSH_KEYTYPE_SSH2)
962468d4 708 encrypted = ssh2_userkey_encrypted(infilename, &origcomment);
47a6b94c 709 else
962468d4 710 encrypted = import_encrypted(infilename, intype, &origcomment);
47a6b94c 711
712 /*
713 * If so, ask for a passphrase.
714 */
715 if (encrypted && load_encrypted) {
edd0cb8a 716 prompts_t *p = new_prompts(NULL);
717 int ret;
718 p->to_server = FALSE;
719 p->name = dupstr("SSH key passphrase");
b61f81bc 720 add_prompt(p, dupstr("Enter passphrase to load key: "), FALSE);
edd0cb8a 721 ret = console_get_userpass_input(p, NULL, 0);
722 assert(ret >= 0);
723 if (!ret) {
724 free_prompts(p);
47a6b94c 725 perror("puttygen: unable to read passphrase");
726 return 1;
edd0cb8a 727 } else {
728 passphrase = dupstr(p->prompts[0]->result);
729 free_prompts(p);
47a6b94c 730 }
731 } else {
732 passphrase = NULL;
733 }
734
735 switch (intype) {
736 int ret;
737
738 case SSH_KEYTYPE_SSH1:
739 ssh1key = snew(struct RSAKey);
740 if (!load_encrypted) {
741 void *vblob;
8a9977e5 742 unsigned char *blob;
0016d70b 743 int n, l, bloblen;
47a6b94c 744
962468d4 745 ret = rsakey_pubblob(infilename, &vblob, &bloblen,
e4cb16dd 746 &origcomment, &error);
8a9977e5 747 blob = (unsigned char *)vblob;
47a6b94c 748
749 n = 4; /* skip modulus bits */
0016d70b 750
751 l = ssh1_read_bignum(blob + n, bloblen - n,
752 &ssh1key->exponent);
753 if (l < 0) {
2e85c969 754 error = "SSH-1 public key blob was too short";
0016d70b 755 } else {
756 n += l;
757 l = ssh1_read_bignum(blob + n, bloblen - n,
758 &ssh1key->modulus);
759 if (l < 0) {
2e85c969 760 error = "SSH-1 public key blob was too short";
0016d70b 761 } else
762 n += l;
763 }
e4cb16dd 764 ssh1key->comment = dupstr(origcomment);
b81cca98 765 ssh1key->private_exponent = NULL;
a1e50b70 766 ssh1key->p = NULL;
767 ssh1key->q = NULL;
768 ssh1key->iqmp = NULL;
47a6b94c 769 } else {
962468d4 770 ret = loadrsakey(infilename, ssh1key, passphrase, &error);
47a6b94c 771 }
b81cca98 772 if (ret > 0)
47a6b94c 773 error = NULL;
774 else if (!error)
775 error = "unknown error";
776 break;
777
778 case SSH_KEYTYPE_SSH2:
779 if (!load_encrypted) {
962468d4 780 ssh2blob = ssh2_userkey_loadpub(infilename, &ssh2alg,
06897bd7 781 &ssh2bloblen, NULL, &error);
6e3c47cb 782 if (ssh2blob) {
783 ssh2algf = find_pubkey_alg(ssh2alg);
784 if (ssh2algf)
785 bits = ssh2algf->pubkey_bits(ssh2blob, ssh2bloblen);
786 else
787 bits = -1;
788 }
47a6b94c 789 } else {
962468d4 790 ssh2key = ssh2_load_userkey(infilename, passphrase, &error);
47a6b94c 791 }
b81cca98 792 if ((ssh2key && ssh2key != SSH2_WRONG_PASSPHRASE) || ssh2blob)
47a6b94c 793 error = NULL;
794 else if (!error) {
795 if (ssh2key == SSH2_WRONG_PASSPHRASE)
796 error = "wrong passphrase";
797 else
798 error = "unknown error";
799 }
800 break;
801
802 case SSH_KEYTYPE_OPENSSH:
803 case SSH_KEYTYPE_SSHCOM:
962468d4 804 ssh2key = import_ssh2(infilename, intype, passphrase, &error);
1e87cce5 805 if (ssh2key) {
806 if (ssh2key != SSH2_WRONG_PASSPHRASE)
807 error = NULL;
47a6b94c 808 else
1e87cce5 809 error = "wrong passphrase";
810 } else if (!error)
811 error = "unknown error";
47a6b94c 812 break;
813
814 default:
815 assert(0);
816 }
817
818 if (error) {
819 fprintf(stderr, "puttygen: error loading `%s': %s\n",
820 infile, error);
821 return 1;
822 }
823 }
824
825 /*
826 * Change the comment if asked to.
827 */
828 if (comment) {
829 if (sshver == 1) {
830 assert(ssh1key);
831 sfree(ssh1key->comment);
832 ssh1key->comment = dupstr(comment);
833 } else {
834 assert(ssh2key);
835 sfree(ssh2key->comment);
836 ssh2key->comment = dupstr(comment);
837 }
838 }
839
840 /*
841 * Prompt for a new passphrase if we have been asked to, or if
842 * we have just generated a key.
843 */
844 if (change_passphrase || keytype != NOKEYGEN) {
edd0cb8a 845 prompts_t *p = new_prompts(NULL);
846 int ret;
47a6b94c 847
edd0cb8a 848 p->to_server = FALSE;
849 p->name = dupstr("New SSH key passphrase");
b61f81bc 850 add_prompt(p, dupstr("Enter passphrase to save key: "), FALSE);
851 add_prompt(p, dupstr("Re-enter passphrase to verify: "), FALSE);
edd0cb8a 852 ret = console_get_userpass_input(p, NULL, 0);
853 assert(ret >= 0);
854 if (!ret) {
855 free_prompts(p);
47a6b94c 856 perror("puttygen: unable to read new passphrase");
857 return 1;
edd0cb8a 858 } else {
859 if (strcmp(p->prompts[0]->result, p->prompts[1]->result)) {
860 free_prompts(p);
861 fprintf(stderr, "puttygen: passphrases do not match\n");
862 return 1;
863 }
864 if (passphrase) {
dfb88efd 865 smemclr(passphrase, strlen(passphrase));
edd0cb8a 866 sfree(passphrase);
867 }
868 passphrase = dupstr(p->prompts[0]->result);
869 free_prompts(p);
870 if (!*passphrase) {
871 sfree(passphrase);
872 passphrase = NULL;
873 }
47a6b94c 874 }
875 }
876
877 /*
878 * Write output.
879 *
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.)
884 */
885 if (outfiletmp)
886 outfilename = filename_from_str(outfiletmp);
887 else
888 outfilename = filename_from_str(outfile ? outfile : "");
889
890 switch (outtype) {
891 int ret;
892
893 case PRIVATE:
894 if (sshver == 1) {
895 assert(ssh1key);
962468d4 896 ret = saversakey(outfilename, ssh1key, passphrase);
47a6b94c 897 if (!ret) {
2e85c969 898 fprintf(stderr, "puttygen: unable to save SSH-1 private key\n");
47a6b94c 899 return 1;
900 }
901 } else {
902 assert(ssh2key);
962468d4 903 ret = ssh2_save_userkey(outfilename, ssh2key, passphrase);
47a6b94c 904 if (!ret) {
2e85c969 905 fprintf(stderr, "puttygen: unable to save SSH-2 private key\n");
47a6b94c 906 return 1;
907 }
908 }
b81cca98 909 if (outfiletmp) {
910 if (!move(outfiletmp, outfile))
911 return 1; /* rename failed */
912 }
47a6b94c 913 break;
914
915 case PUBLIC:
916 case PUBLICO:
917 if (sshver == 1) {
918 FILE *fp;
919 char *dec1, *dec2;
920
921 assert(ssh1key);
922
923 if (outfile)
c6940f12 924 fp = f_open(outfilename, "w", FALSE);
47a6b94c 925 else
926 fp = stdout;
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);
931 sfree(dec1);
932 sfree(dec2);
933 if (outfile)
934 fclose(fp);
935 } else if (outtype == PUBLIC) {
936 if (!ssh2blob) {
937 assert(ssh2key);
938 ssh2blob = ssh2key->alg->public_blob(ssh2key->data,
939 &ssh2bloblen);
940 }
941 save_ssh2_pubkey(outfile, ssh2key ? ssh2key->comment : origcomment,
942 ssh2blob, ssh2bloblen);
943 } else if (outtype == PUBLICO) {
944 char *buffer, *p;
945 int i;
946 FILE *fp;
947
948 if (!ssh2blob) {
949 assert(ssh2key);
950 ssh2blob = ssh2key->alg->public_blob(ssh2key->data,
951 &ssh2bloblen);
952 }
953 if (!ssh2alg) {
954 assert(ssh2key);
955 ssh2alg = ssh2key->alg->name;
956 }
957 if (ssh2key)
958 comment = ssh2key->comment;
959 else
960 comment = origcomment;
961
962 buffer = snewn(strlen(ssh2alg) +
963 4 * ((ssh2bloblen+2) / 3) +
964 strlen(comment) + 3, char);
965 strcpy(buffer, ssh2alg);
966 p = buffer + strlen(buffer);
967 *p++ = ' ';
968 i = 0;
969 while (i < ssh2bloblen) {
970 int n = (ssh2bloblen - i < 3 ? ssh2bloblen - i : 3);
971 base64_encode_atom(ssh2blob + i, n, p);
972 i += n;
973 p += 4;
974 }
975 if (*comment) {
976 *p++ = ' ';
977 strcpy(p, comment);
978 } else
979 *p++ = '\0';
980
981 if (outfile)
c6940f12 982 fp = f_open(outfilename, "w", FALSE);
47a6b94c 983 else
984 fp = stdout;
985 fprintf(fp, "%s\n", buffer);
986 if (outfile)
987 fclose(fp);
988
989 sfree(buffer);
990 }
991 break;
992
993 case FP:
994 {
995 FILE *fp;
996 char *fingerprint;
997
998 if (sshver == 1) {
999 assert(ssh1key);
1000 fingerprint = snewn(128, char);
1001 rsa_fingerprint(fingerprint, 128, ssh1key);
1002 } else {
1003 if (ssh2key) {
1004 fingerprint = ssh2key->alg->fingerprint(ssh2key->data);
1005 } else {
1006 assert(ssh2blob);
1007 fingerprint = blobfp(ssh2alg, bits, ssh2blob, ssh2bloblen);
1008 }
1009 }
1010
1011 if (outfile)
c6940f12 1012 fp = f_open(outfilename, "w", FALSE);
47a6b94c 1013 else
1014 fp = stdout;
1015 fprintf(fp, "%s\n", fingerprint);
1016 if (outfile)
1017 fclose(fp);
1018
1019 sfree(fingerprint);
1020 }
1021 break;
1022
1023 case OPENSSH:
1024 case SSHCOM:
1025 assert(sshver == 2);
1026 assert(ssh2key);
962468d4 1027 ret = export_ssh2(outfilename, outtype, ssh2key, passphrase);
47a6b94c 1028 if (!ret) {
1029 fprintf(stderr, "puttygen: unable to export key\n");
1030 return 1;
1031 }
b81cca98 1032 if (outfiletmp) {
1033 if (!move(outfiletmp, outfile))
1034 return 1; /* rename failed */
1035 }
47a6b94c 1036 break;
1037 }
1038
1039 if (passphrase) {
dfb88efd 1040 smemclr(passphrase, strlen(passphrase));
47a6b94c 1041 sfree(passphrase);
1042 }
1043
1044 if (ssh1key)
1045 freersakey(ssh1key);
1046 if (ssh2key) {
1047 ssh2key->alg->freekey(ssh2key->data);
1048 sfree(ssh2key);
1049 }
1050
1051 return 0;
1052}
b81cca98 1053
1054#ifdef TEST_CMDGEN
1055
1056#undef main
1057
1058#include <stdarg.h>
1059
1060int passes, fails;
1061
1062void setup_passphrases(char *first, ...)
1063{
1064 va_list ap;
1065 char *next;
1066
1067 nprompts = 0;
1068 if (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;
1074 }
1075 va_end(ap);
1076 }
1077}
1078
1079void test(int retval, ...)
1080{
1081 va_list ap;
1082 int i, argc, ret;
1083 char **argv;
1084
1085 argc = 0;
1086 va_start(ap, retval);
1087 while (va_arg(ap, char *) != NULL)
1088 argc++;
1089 va_end(ap);
1090
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 *);
1095 va_end(ap);
1096
1097 promptsgot = 0;
1098 ret = cmdgen_main(argc, argv);
1099
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]);
1104 printf("\n");
1105 fails++;
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]);
1110 printf("\n");
1111 fails++;
1112 } else {
1113 passes++;
1114 }
1115}
1116
1117void filecmp(char *file1, char *file2, char *fmt, ...)
1118{
1119 /*
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.
1124 */
1125 char *buf;
1126 int ret;
1127
1128 buf = dupprintf("diff -q '%s' '%s'", file1, file2);
1129 ret = system(buf);
1130 sfree(buf);
1131
1132 if (ret) {
1133 va_list ap;
1134
1135 printf("FAILED diff (ret=%d): ", ret);
1136
1137 va_start(ap, fmt);
1138 vprintf(fmt, ap);
1139 va_end(ap);
1140
1141 printf("\n");
1142
1143 fails++;
1144 } else
1145 passes++;
1146}
1147
1148char *cleanup_fp(char *s)
1149{
1150 char *p;
1151
1152 if (!strncmp(s, "ssh-", 4)) {
1153 s += strcspn(s, " \n\t");
1154 s += strspn(s, " \n\t");
1155 }
1156
1157 p = s;
1158 s += strcspn(s, " \n\t");
1159 s += strspn(s, " \n\t");
1160 s += strcspn(s, " \n\t");
1161
1162 return dupprintf("%.*s", s - p, p);
1163}
1164
1165char *get_fp(char *filename)
1166{
1167 FILE *fp;
1168 char buf[256], *ret;
1169
1170 fp = fopen(filename, "r");
1171 if (!fp)
1172 return NULL;
1173 ret = fgets(buf, sizeof(buf), fp);
1174 fclose(fp);
1175 if (!ret)
1176 return NULL;
1177 return cleanup_fp(buf);
1178}
1179
1180void check_fp(char *filename, char *fp, char *fmt, ...)
1181{
1182 char *newfp;
1183
1184 if (!fp)
1185 return;
1186
1187 newfp = get_fp(filename);
1188
1189 if (!strcmp(fp, newfp)) {
1190 passes++;
1191 } else {
1192 va_list ap;
1193
1194 printf("FAILED check_fp ['%s' != '%s']: ", newfp, fp);
1195
1196 va_start(ap, fmt);
1197 vprintf(fmt, ap);
1198 va_end(ap);
1199
1200 printf("\n");
1201
1202 fails++;
1203 }
1204
1205 sfree(newfp);
1206}
1207
1208int main(int argc, char **argv)
1209{
1210 int i;
1211 static char *const keytypes[] = { "rsa1", "dsa", "rsa" };
1212
1213 /*
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.
1217 */
1218 if (argc > 1)
1219 return cmdgen_main(argc, argv);
1220
1221 passes = fails = 0;
1222
1223 for (i = 0; i < lenof(keytypes); i++) {
1224 char filename[128], osfilename[128], scfilename[128];
1225 char pubfilename[128], tmpfilename1[128], tmpfilename2[128];
1226 char *fp;
1227
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]);
1234
1235 /*
1236 * Create an encrypted key.
1237 */
1238 setup_passphrases("sponge", "sponge", NULL);
1239 test(0, "puttygen", "-t", keytypes[i], "-o", filename, NULL);
1240
1241 /*
1242 * List the public key in OpenSSH format.
1243 */
1244 setup_passphrases(NULL);
1245 test(0, "puttygen", "-L", filename, "-o", pubfilename, NULL);
1246 {
1247 char cmdbuf[256];
1248 fp = 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");
1254 }
1255 }
1256
1257 /*
1258 * List the public key in IETF/ssh.com format.
1259 */
1260 setup_passphrases(NULL);
1261 test(0, "puttygen", "-p", filename, NULL);
1262
1263 /*
1264 * List the fingerprint of the key.
1265 */
1266 setup_passphrases(NULL);
1267 test(0, "puttygen", "-l", filename, "-o", tmpfilename1, NULL);
1268 if (!fp) {
1269 /*
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
1273 * testing.
1274 */
1275 fp = get_fp(tmpfilename1);
1276 } else {
1277 check_fp(tmpfilename1, fp, "%s initial fp", keytypes[i]);
1278 }
1279
1280 /*
1281 * Change the comment of the key; this _does_ require a
1282 * passphrase owing to the tamperproofing.
1283 *
2e85c969 1284 * NOTE: In SSH-1, this only requires a passphrase because
b81cca98 1285 * of inadequacies of the loading and saving mechanisms. In
1286 * _principle_, it should be perfectly possible to modify
2e85c969 1287 * the comment on an SSH-1 key without requiring a
b81cca98 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.
1292 *
1293 * I don't consider this to be a problem worth solving,
1294 * because (a) to fix it would probably end up bloating
2e85c969 1295 * PuTTY proper, and (b) SSH-1 is on the way out anyway so
b81cca98 1296 * it shouldn't be highly significant. If it seriously
1297 * bothers anyone then perhaps I _might_ be persuadable.
1298 */
1299 setup_passphrases("sponge", NULL);
1300 test(0, "puttygen", "-C", "new-comment", filename, NULL);
1301
1302 /*
1303 * Change the passphrase to nothing.
1304 */
1305 setup_passphrases("sponge", "", "", NULL);
1306 test(0, "puttygen", "-P", filename, NULL);
1307
1308 /*
1309 * Change the comment of the key again; this time we expect no
1310 * passphrase to be required.
1311 */
1312 setup_passphrases(NULL);
1313 test(0, "puttygen", "-C", "new-comment-2", filename, NULL);
1314
1315 /*
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.
1319 */
1320 setup_passphrases(NULL);
1321 test((i==0), "puttygen", "-O", "private-openssh", "-o", osfilename,
1322 filename, NULL);
1323
1324 if (i) {
1325 /*
1326 * List the fingerprint of the OpenSSH-formatted key.
1327 */
1328 setup_passphrases(NULL);
1329 test(0, "puttygen", "-l", osfilename, "-o", tmpfilename1, NULL);
1330 check_fp(tmpfilename1, fp, "%s openssh clear fp", keytypes[i]);
1331
1332 /*
1333 * List the public half of the OpenSSH-formatted key in
1334 * OpenSSH format.
1335 */
1336 setup_passphrases(NULL);
1337 test(0, "puttygen", "-L", osfilename, NULL);
1338
1339 /*
1340 * List the public half of the OpenSSH-formatted key in
1341 * IETF/ssh.com format.
1342 */
1343 setup_passphrases(NULL);
1344 test(0, "puttygen", "-p", osfilename, NULL);
1345 }
1346
1347 /*
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.
1351 */
1352 setup_passphrases(NULL);
1353 test((i==0), "puttygen", "-O", "private-sshcom", "-o", scfilename,
1354 filename, NULL);
1355
1356 if (i) {
1357 /*
1358 * List the fingerprint of the ssh.com-formatted key.
1359 */
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]);
1363
1364 /*
1365 * List the public half of the ssh.com-formatted key in
1366 * OpenSSH format.
1367 */
1368 setup_passphrases(NULL);
1369 test(0, "puttygen", "-L", scfilename, NULL);
1370
1371 /*
1372 * List the public half of the ssh.com-formatted key in
1373 * IETF/ssh.com format.
1374 */
1375 setup_passphrases(NULL);
1376 test(0, "puttygen", "-p", scfilename, NULL);
1377 }
1378
1379 if (i) {
1380 /*
1381 * Convert from OpenSSH into ssh.com.
1382 */
1383 setup_passphrases(NULL);
1384 test(0, "puttygen", osfilename, "-o", tmpfilename1,
1385 "-O", "private-sshcom", NULL);
1386
1387 /*
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.
1391 */
1392 setup_passphrases(NULL);
1393 test(0, "puttygen", tmpfilename1, "-C", "new-comment-2",
1394 "-o", tmpfilename2, NULL);
1395
1396 /*
1397 * See if the PuTTY key thus generated is the same as
1398 * the original.
1399 */
1400 filecmp(filename, tmpfilename2,
1401 "p->o->s->p clear %s", keytypes[i]);
1402
1403 /*
1404 * Convert from ssh.com to OpenSSH.
1405 */
1406 setup_passphrases(NULL);
1407 test(0, "puttygen", scfilename, "-o", tmpfilename1,
1408 "-O", "private-openssh", NULL);
1409
1410 /*
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.
1414 */
1415 setup_passphrases(NULL);
1416 test(0, "puttygen", tmpfilename1, "-C", "new-comment-2",
1417 "-o", tmpfilename2, NULL);
1418
1419 /*
1420 * See if the PuTTY key thus generated is the same as
1421 * the original.
1422 */
1423 filecmp(filename, tmpfilename2,
1424 "p->s->o->p clear %s", keytypes[i]);
1425
1426 /*
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.
1430 */
1431 setup_passphrases(NULL);
1432 test(0, "puttygen", "-O", "private-sshcom", "-o", tmpfilename1,
1433 filename, NULL);
1434 setup_passphrases(NULL);
1435 test(0, "puttygen", tmpfilename1, "-o", tmpfilename2, NULL);
1436 filecmp(filename, tmpfilename2,
1437 "p->s->p clear %s", keytypes[i]);
1438 }
1439
1440 /*
1441 * Check that mismatched passphrases cause an error.
1442 */
1443 setup_passphrases("sponge2", "sponge3", NULL);
1444 test(1, "puttygen", "-P", filename, NULL);
1445
1446 /*
1447 * Put a passphrase back on.
1448 */
1449 setup_passphrases("sponge2", "sponge2", NULL);
1450 test(0, "puttygen", "-P", filename, NULL);
1451
1452 /*
1453 * Export the private key into OpenSSH format, this time
1454 * while encrypted. For RSA1 keys, this should give an
1455 * error.
1456 */
1457 if (i == 0)
1458 setup_passphrases(NULL); /* error, hence no passphrase read */
1459 else
1460 setup_passphrases("sponge2", NULL);
1461 test((i==0), "puttygen", "-O", "private-openssh", "-o", osfilename,
1462 filename, NULL);
1463
1464 if (i) {
1465 /*
1466 * List the fingerprint of the OpenSSH-formatted key.
1467 */
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]);
1471
1472 /*
1473 * List the public half of the OpenSSH-formatted key in
1474 * OpenSSH format.
1475 */
1476 setup_passphrases("sponge2", NULL);
1477 test(0, "puttygen", "-L", osfilename, NULL);
1478
1479 /*
1480 * List the public half of the OpenSSH-formatted key in
1481 * IETF/ssh.com format.
1482 */
1483 setup_passphrases("sponge2", NULL);
1484 test(0, "puttygen", "-p", osfilename, NULL);
1485 }
1486
1487 /*
1488 * Export the private key into ssh.com format, this time
1489 * while encrypted. For RSA1 keys, this should give an
1490 * error.
1491 */
1492 if (i == 0)
1493 setup_passphrases(NULL); /* error, hence no passphrase read */
1494 else
1495 setup_passphrases("sponge2", NULL);
1496 test((i==0), "puttygen", "-O", "private-sshcom", "-o", scfilename,
1497 filename, NULL);
1498
1499 if (i) {
1500 /*
1501 * List the fingerprint of the ssh.com-formatted key.
1502 */
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]);
1506
1507 /*
1508 * List the public half of the ssh.com-formatted key in
1509 * OpenSSH format.
1510 */
1511 setup_passphrases("sponge2", NULL);
1512 test(0, "puttygen", "-L", scfilename, NULL);
1513
1514 /*
1515 * List the public half of the ssh.com-formatted key in
1516 * IETF/ssh.com format.
1517 */
1518 setup_passphrases("sponge2", NULL);
1519 test(0, "puttygen", "-p", scfilename, NULL);
1520 }
1521
1522 if (i) {
1523 /*
1524 * Convert from OpenSSH into ssh.com.
1525 */
1526 setup_passphrases("sponge2", NULL);
1527 test(0, "puttygen", osfilename, "-o", tmpfilename1,
1528 "-O", "private-sshcom", NULL);
1529
1530 /*
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.
1534 */
1535 setup_passphrases("sponge2", NULL);
1536 test(0, "puttygen", tmpfilename1, "-C", "new-comment-2",
1537 "-o", tmpfilename2, NULL);
1538
1539 /*
1540 * See if the PuTTY key thus generated is the same as
1541 * the original.
1542 */
1543 filecmp(filename, tmpfilename2,
1544 "p->o->s->p encrypted %s", keytypes[i]);
1545
1546 /*
1547 * Convert from ssh.com to OpenSSH.
1548 */
1549 setup_passphrases("sponge2", NULL);
1550 test(0, "puttygen", scfilename, "-o", tmpfilename1,
1551 "-O", "private-openssh", NULL);
1552
1553 /*
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.
1557 */
1558 setup_passphrases("sponge2", NULL);
1559 test(0, "puttygen", tmpfilename1, "-C", "new-comment-2",
1560 "-o", tmpfilename2, NULL);
1561
1562 /*
1563 * See if the PuTTY key thus generated is the same as
1564 * the original.
1565 */
1566 filecmp(filename, tmpfilename2,
1567 "p->s->o->p encrypted %s", keytypes[i]);
1568
1569 /*
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.
1573 */
1574 setup_passphrases("sponge2", NULL);
1575 test(0, "puttygen", "-O", "private-sshcom", "-o", tmpfilename1,
1576 filename, NULL);
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]);
1581 }
1582
1583 /*
1584 * Load with the wrong passphrase.
1585 */
1586 setup_passphrases("sponge8", NULL);
1587 test(1, "puttygen", "-C", "spurious-new-comment", filename, NULL);
1588
1589 /*
1590 * Load a totally bogus file.
1591 */
1592 setup_passphrases(NULL);
1593 test(1, "puttygen", "-C", "spurious-new-comment", pubfilename, NULL);
1594 }
1595 printf("%d passes, %d fails\n", passes, fails);
1596 return 0;
1597}
1598
1599#endif