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