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