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