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