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