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 |
38 | char *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 |
45 | int nprompts, promptsgot; |
46 | const char *prompts[3]; |
edd0cb8a |
47 | int 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 | |
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 | { |
bcb1823f |
121 | printf("puttygen: %s\n", ver); |
47a6b94c |
122 | } |
123 | |
e49a814b |
124 | void 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 | |
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(); |
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 | |
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 | |
b81cca98 |
214 | static 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 |
233 | static 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 | |
254 | int 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; |
47a6b94c |
766 | } else { |
962468d4 |
767 | ret = loadrsakey(infilename, ssh1key, passphrase, &error); |
47a6b94c |
768 | } |
b81cca98 |
769 | if (ret > 0) |
47a6b94c |
770 | error = NULL; |
771 | else if (!error) |
772 | error = "unknown error"; |
773 | break; |
774 | |
775 | case SSH_KEYTYPE_SSH2: |
776 | if (!load_encrypted) { |
962468d4 |
777 | ssh2blob = ssh2_userkey_loadpub(infilename, &ssh2alg, |
06897bd7 |
778 | &ssh2bloblen, NULL, &error); |
47a6b94c |
779 | ssh2algf = find_pubkey_alg(ssh2alg); |
780 | if (ssh2algf) |
781 | bits = ssh2algf->pubkey_bits(ssh2blob, ssh2bloblen); |
782 | else |
783 | bits = -1; |
784 | } else { |
962468d4 |
785 | ssh2key = ssh2_load_userkey(infilename, passphrase, &error); |
47a6b94c |
786 | } |
b81cca98 |
787 | if ((ssh2key && ssh2key != SSH2_WRONG_PASSPHRASE) || ssh2blob) |
47a6b94c |
788 | error = NULL; |
789 | else if (!error) { |
790 | if (ssh2key == SSH2_WRONG_PASSPHRASE) |
791 | error = "wrong passphrase"; |
792 | else |
793 | error = "unknown error"; |
794 | } |
795 | break; |
796 | |
797 | case SSH_KEYTYPE_OPENSSH: |
798 | case SSH_KEYTYPE_SSHCOM: |
962468d4 |
799 | ssh2key = import_ssh2(infilename, intype, passphrase, &error); |
1e87cce5 |
800 | if (ssh2key) { |
801 | if (ssh2key != SSH2_WRONG_PASSPHRASE) |
802 | error = NULL; |
47a6b94c |
803 | else |
1e87cce5 |
804 | error = "wrong passphrase"; |
805 | } else if (!error) |
806 | error = "unknown error"; |
47a6b94c |
807 | break; |
808 | |
809 | default: |
810 | assert(0); |
811 | } |
812 | |
813 | if (error) { |
814 | fprintf(stderr, "puttygen: error loading `%s': %s\n", |
815 | infile, error); |
816 | return 1; |
817 | } |
818 | } |
819 | |
820 | /* |
821 | * Change the comment if asked to. |
822 | */ |
823 | if (comment) { |
824 | if (sshver == 1) { |
825 | assert(ssh1key); |
826 | sfree(ssh1key->comment); |
827 | ssh1key->comment = dupstr(comment); |
828 | } else { |
829 | assert(ssh2key); |
830 | sfree(ssh2key->comment); |
831 | ssh2key->comment = dupstr(comment); |
832 | } |
833 | } |
834 | |
835 | /* |
836 | * Prompt for a new passphrase if we have been asked to, or if |
837 | * we have just generated a key. |
838 | */ |
839 | if (change_passphrase || keytype != NOKEYGEN) { |
edd0cb8a |
840 | prompts_t *p = new_prompts(NULL); |
841 | int ret; |
47a6b94c |
842 | |
edd0cb8a |
843 | p->to_server = FALSE; |
844 | p->name = dupstr("New SSH key passphrase"); |
b61f81bc |
845 | add_prompt(p, dupstr("Enter passphrase to save key: "), FALSE); |
846 | add_prompt(p, dupstr("Re-enter passphrase to verify: "), FALSE); |
edd0cb8a |
847 | ret = console_get_userpass_input(p, NULL, 0); |
848 | assert(ret >= 0); |
849 | if (!ret) { |
850 | free_prompts(p); |
47a6b94c |
851 | perror("puttygen: unable to read new passphrase"); |
852 | return 1; |
edd0cb8a |
853 | } else { |
854 | if (strcmp(p->prompts[0]->result, p->prompts[1]->result)) { |
855 | free_prompts(p); |
856 | fprintf(stderr, "puttygen: passphrases do not match\n"); |
857 | return 1; |
858 | } |
859 | if (passphrase) { |
dfb88efd |
860 | smemclr(passphrase, strlen(passphrase)); |
edd0cb8a |
861 | sfree(passphrase); |
862 | } |
863 | passphrase = dupstr(p->prompts[0]->result); |
864 | free_prompts(p); |
865 | if (!*passphrase) { |
866 | sfree(passphrase); |
867 | passphrase = NULL; |
868 | } |
47a6b94c |
869 | } |
870 | } |
871 | |
872 | /* |
873 | * Write output. |
874 | * |
875 | * (In the case where outfile and outfiletmp are both NULL, |
876 | * there is no semantic reason to initialise outfilename at |
877 | * all; but we have to write _something_ to it or some compiler |
878 | * will probably complain that it might be used uninitialised.) |
879 | */ |
880 | if (outfiletmp) |
881 | outfilename = filename_from_str(outfiletmp); |
882 | else |
883 | outfilename = filename_from_str(outfile ? outfile : ""); |
884 | |
885 | switch (outtype) { |
886 | int ret; |
887 | |
888 | case PRIVATE: |
889 | if (sshver == 1) { |
890 | assert(ssh1key); |
962468d4 |
891 | ret = saversakey(outfilename, ssh1key, passphrase); |
47a6b94c |
892 | if (!ret) { |
2e85c969 |
893 | fprintf(stderr, "puttygen: unable to save SSH-1 private key\n"); |
47a6b94c |
894 | return 1; |
895 | } |
896 | } else { |
897 | assert(ssh2key); |
962468d4 |
898 | ret = ssh2_save_userkey(outfilename, ssh2key, passphrase); |
47a6b94c |
899 | if (!ret) { |
2e85c969 |
900 | fprintf(stderr, "puttygen: unable to save SSH-2 private key\n"); |
47a6b94c |
901 | return 1; |
902 | } |
903 | } |
b81cca98 |
904 | if (outfiletmp) { |
905 | if (!move(outfiletmp, outfile)) |
906 | return 1; /* rename failed */ |
907 | } |
47a6b94c |
908 | break; |
909 | |
910 | case PUBLIC: |
911 | case PUBLICO: |
912 | if (sshver == 1) { |
913 | FILE *fp; |
914 | char *dec1, *dec2; |
915 | |
916 | assert(ssh1key); |
917 | |
918 | if (outfile) |
c6940f12 |
919 | fp = f_open(outfilename, "w", FALSE); |
47a6b94c |
920 | else |
921 | fp = stdout; |
922 | dec1 = bignum_decimal(ssh1key->exponent); |
923 | dec2 = bignum_decimal(ssh1key->modulus); |
924 | fprintf(fp, "%d %s %s %s\n", bignum_bitcount(ssh1key->modulus), |
925 | dec1, dec2, ssh1key->comment); |
926 | sfree(dec1); |
927 | sfree(dec2); |
928 | if (outfile) |
929 | fclose(fp); |
930 | } else if (outtype == PUBLIC) { |
931 | if (!ssh2blob) { |
932 | assert(ssh2key); |
933 | ssh2blob = ssh2key->alg->public_blob(ssh2key->data, |
934 | &ssh2bloblen); |
935 | } |
936 | save_ssh2_pubkey(outfile, ssh2key ? ssh2key->comment : origcomment, |
937 | ssh2blob, ssh2bloblen); |
938 | } else if (outtype == PUBLICO) { |
939 | char *buffer, *p; |
940 | int i; |
941 | FILE *fp; |
942 | |
943 | if (!ssh2blob) { |
944 | assert(ssh2key); |
945 | ssh2blob = ssh2key->alg->public_blob(ssh2key->data, |
946 | &ssh2bloblen); |
947 | } |
948 | if (!ssh2alg) { |
949 | assert(ssh2key); |
950 | ssh2alg = ssh2key->alg->name; |
951 | } |
952 | if (ssh2key) |
953 | comment = ssh2key->comment; |
954 | else |
955 | comment = origcomment; |
956 | |
957 | buffer = snewn(strlen(ssh2alg) + |
958 | 4 * ((ssh2bloblen+2) / 3) + |
959 | strlen(comment) + 3, char); |
960 | strcpy(buffer, ssh2alg); |
961 | p = buffer + strlen(buffer); |
962 | *p++ = ' '; |
963 | i = 0; |
964 | while (i < ssh2bloblen) { |
965 | int n = (ssh2bloblen - i < 3 ? ssh2bloblen - i : 3); |
966 | base64_encode_atom(ssh2blob + i, n, p); |
967 | i += n; |
968 | p += 4; |
969 | } |
970 | if (*comment) { |
971 | *p++ = ' '; |
972 | strcpy(p, comment); |
973 | } else |
974 | *p++ = '\0'; |
975 | |
976 | if (outfile) |
c6940f12 |
977 | fp = f_open(outfilename, "w", FALSE); |
47a6b94c |
978 | else |
979 | fp = stdout; |
980 | fprintf(fp, "%s\n", buffer); |
981 | if (outfile) |
982 | fclose(fp); |
983 | |
984 | sfree(buffer); |
985 | } |
986 | break; |
987 | |
988 | case FP: |
989 | { |
990 | FILE *fp; |
991 | char *fingerprint; |
992 | |
993 | if (sshver == 1) { |
994 | assert(ssh1key); |
995 | fingerprint = snewn(128, char); |
996 | rsa_fingerprint(fingerprint, 128, ssh1key); |
997 | } else { |
998 | if (ssh2key) { |
999 | fingerprint = ssh2key->alg->fingerprint(ssh2key->data); |
1000 | } else { |
1001 | assert(ssh2blob); |
1002 | fingerprint = blobfp(ssh2alg, bits, ssh2blob, ssh2bloblen); |
1003 | } |
1004 | } |
1005 | |
1006 | if (outfile) |
c6940f12 |
1007 | fp = f_open(outfilename, "w", FALSE); |
47a6b94c |
1008 | else |
1009 | fp = stdout; |
1010 | fprintf(fp, "%s\n", fingerprint); |
1011 | if (outfile) |
1012 | fclose(fp); |
1013 | |
1014 | sfree(fingerprint); |
1015 | } |
1016 | break; |
1017 | |
1018 | case OPENSSH: |
1019 | case SSHCOM: |
1020 | assert(sshver == 2); |
1021 | assert(ssh2key); |
962468d4 |
1022 | ret = export_ssh2(outfilename, outtype, ssh2key, passphrase); |
47a6b94c |
1023 | if (!ret) { |
1024 | fprintf(stderr, "puttygen: unable to export key\n"); |
1025 | return 1; |
1026 | } |
b81cca98 |
1027 | if (outfiletmp) { |
1028 | if (!move(outfiletmp, outfile)) |
1029 | return 1; /* rename failed */ |
1030 | } |
47a6b94c |
1031 | break; |
1032 | } |
1033 | |
1034 | if (passphrase) { |
dfb88efd |
1035 | smemclr(passphrase, strlen(passphrase)); |
47a6b94c |
1036 | sfree(passphrase); |
1037 | } |
1038 | |
1039 | if (ssh1key) |
1040 | freersakey(ssh1key); |
1041 | if (ssh2key) { |
1042 | ssh2key->alg->freekey(ssh2key->data); |
1043 | sfree(ssh2key); |
1044 | } |
1045 | |
1046 | return 0; |
1047 | } |
b81cca98 |
1048 | |
1049 | #ifdef TEST_CMDGEN |
1050 | |
1051 | #undef main |
1052 | |
1053 | #include <stdarg.h> |
1054 | |
1055 | int passes, fails; |
1056 | |
1057 | void setup_passphrases(char *first, ...) |
1058 | { |
1059 | va_list ap; |
1060 | char *next; |
1061 | |
1062 | nprompts = 0; |
1063 | if (first) { |
1064 | prompts[nprompts++] = first; |
1065 | va_start(ap, first); |
1066 | while ((next = va_arg(ap, char *)) != NULL) { |
1067 | assert(nprompts < lenof(prompts)); |
1068 | prompts[nprompts++] = next; |
1069 | } |
1070 | va_end(ap); |
1071 | } |
1072 | } |
1073 | |
1074 | void test(int retval, ...) |
1075 | { |
1076 | va_list ap; |
1077 | int i, argc, ret; |
1078 | char **argv; |
1079 | |
1080 | argc = 0; |
1081 | va_start(ap, retval); |
1082 | while (va_arg(ap, char *) != NULL) |
1083 | argc++; |
1084 | va_end(ap); |
1085 | |
1086 | argv = snewn(argc+1, char *); |
1087 | va_start(ap, retval); |
1088 | for (i = 0; i <= argc; i++) |
1089 | argv[i] = va_arg(ap, char *); |
1090 | va_end(ap); |
1091 | |
1092 | promptsgot = 0; |
1093 | ret = cmdgen_main(argc, argv); |
1094 | |
1095 | if (ret != retval) { |
1096 | printf("FAILED retval (exp %d got %d):", retval, ret); |
1097 | for (i = 0; i < argc; i++) |
1098 | printf(" %s", argv[i]); |
1099 | printf("\n"); |
1100 | fails++; |
1101 | } else if (promptsgot != nprompts) { |
1102 | printf("FAILED nprompts (exp %d got %d):", nprompts, promptsgot); |
1103 | for (i = 0; i < argc; i++) |
1104 | printf(" %s", argv[i]); |
1105 | printf("\n"); |
1106 | fails++; |
1107 | } else { |
1108 | passes++; |
1109 | } |
1110 | } |
1111 | |
1112 | void filecmp(char *file1, char *file2, char *fmt, ...) |
1113 | { |
1114 | /* |
1115 | * Ideally I should do file comparison myself, to maximise the |
1116 | * portability of this test suite once this application begins |
1117 | * running on non-Unix platforms. For the moment, though, |
1118 | * calling Unix diff is perfectly adequate. |
1119 | */ |
1120 | char *buf; |
1121 | int ret; |
1122 | |
1123 | buf = dupprintf("diff -q '%s' '%s'", file1, file2); |
1124 | ret = system(buf); |
1125 | sfree(buf); |
1126 | |
1127 | if (ret) { |
1128 | va_list ap; |
1129 | |
1130 | printf("FAILED diff (ret=%d): ", ret); |
1131 | |
1132 | va_start(ap, fmt); |
1133 | vprintf(fmt, ap); |
1134 | va_end(ap); |
1135 | |
1136 | printf("\n"); |
1137 | |
1138 | fails++; |
1139 | } else |
1140 | passes++; |
1141 | } |
1142 | |
1143 | char *cleanup_fp(char *s) |
1144 | { |
1145 | char *p; |
1146 | |
1147 | if (!strncmp(s, "ssh-", 4)) { |
1148 | s += strcspn(s, " \n\t"); |
1149 | s += strspn(s, " \n\t"); |
1150 | } |
1151 | |
1152 | p = s; |
1153 | s += strcspn(s, " \n\t"); |
1154 | s += strspn(s, " \n\t"); |
1155 | s += strcspn(s, " \n\t"); |
1156 | |
1157 | return dupprintf("%.*s", s - p, p); |
1158 | } |
1159 | |
1160 | char *get_fp(char *filename) |
1161 | { |
1162 | FILE *fp; |
1163 | char buf[256], *ret; |
1164 | |
1165 | fp = fopen(filename, "r"); |
1166 | if (!fp) |
1167 | return NULL; |
1168 | ret = fgets(buf, sizeof(buf), fp); |
1169 | fclose(fp); |
1170 | if (!ret) |
1171 | return NULL; |
1172 | return cleanup_fp(buf); |
1173 | } |
1174 | |
1175 | void check_fp(char *filename, char *fp, char *fmt, ...) |
1176 | { |
1177 | char *newfp; |
1178 | |
1179 | if (!fp) |
1180 | return; |
1181 | |
1182 | newfp = get_fp(filename); |
1183 | |
1184 | if (!strcmp(fp, newfp)) { |
1185 | passes++; |
1186 | } else { |
1187 | va_list ap; |
1188 | |
1189 | printf("FAILED check_fp ['%s' != '%s']: ", newfp, fp); |
1190 | |
1191 | va_start(ap, fmt); |
1192 | vprintf(fmt, ap); |
1193 | va_end(ap); |
1194 | |
1195 | printf("\n"); |
1196 | |
1197 | fails++; |
1198 | } |
1199 | |
1200 | sfree(newfp); |
1201 | } |
1202 | |
1203 | int main(int argc, char **argv) |
1204 | { |
1205 | int i; |
1206 | static char *const keytypes[] = { "rsa1", "dsa", "rsa" }; |
1207 | |
1208 | /* |
1209 | * Even when this thing is compiled for automatic test mode, |
1210 | * it's helpful to be able to invoke it with command-line |
1211 | * options for _manual_ tests. |
1212 | */ |
1213 | if (argc > 1) |
1214 | return cmdgen_main(argc, argv); |
1215 | |
1216 | passes = fails = 0; |
1217 | |
1218 | for (i = 0; i < lenof(keytypes); i++) { |
1219 | char filename[128], osfilename[128], scfilename[128]; |
1220 | char pubfilename[128], tmpfilename1[128], tmpfilename2[128]; |
1221 | char *fp; |
1222 | |
1223 | sprintf(filename, "test-%s.ppk", keytypes[i]); |
1224 | sprintf(pubfilename, "test-%s.pub", keytypes[i]); |
1225 | sprintf(osfilename, "test-%s.os", keytypes[i]); |
1226 | sprintf(scfilename, "test-%s.sc", keytypes[i]); |
1227 | sprintf(tmpfilename1, "test-%s.tmp1", keytypes[i]); |
1228 | sprintf(tmpfilename2, "test-%s.tmp2", keytypes[i]); |
1229 | |
1230 | /* |
1231 | * Create an encrypted key. |
1232 | */ |
1233 | setup_passphrases("sponge", "sponge", NULL); |
1234 | test(0, "puttygen", "-t", keytypes[i], "-o", filename, NULL); |
1235 | |
1236 | /* |
1237 | * List the public key in OpenSSH format. |
1238 | */ |
1239 | setup_passphrases(NULL); |
1240 | test(0, "puttygen", "-L", filename, "-o", pubfilename, NULL); |
1241 | { |
1242 | char cmdbuf[256]; |
1243 | fp = NULL; |
1244 | sprintf(cmdbuf, "ssh-keygen -l -f '%s' > '%s'", |
1245 | pubfilename, tmpfilename1); |
1246 | if (system(cmdbuf) || |
1247 | (fp = get_fp(tmpfilename1)) == NULL) { |
1248 | printf("UNABLE to test fingerprint matching against OpenSSH"); |
1249 | } |
1250 | } |
1251 | |
1252 | /* |
1253 | * List the public key in IETF/ssh.com format. |
1254 | */ |
1255 | setup_passphrases(NULL); |
1256 | test(0, "puttygen", "-p", filename, NULL); |
1257 | |
1258 | /* |
1259 | * List the fingerprint of the key. |
1260 | */ |
1261 | setup_passphrases(NULL); |
1262 | test(0, "puttygen", "-l", filename, "-o", tmpfilename1, NULL); |
1263 | if (!fp) { |
1264 | /* |
1265 | * If we can't test fingerprints against OpenSSH, we |
1266 | * can at the very least test equality of all the |
1267 | * fingerprints we generate of this key throughout |
1268 | * testing. |
1269 | */ |
1270 | fp = get_fp(tmpfilename1); |
1271 | } else { |
1272 | check_fp(tmpfilename1, fp, "%s initial fp", keytypes[i]); |
1273 | } |
1274 | |
1275 | /* |
1276 | * Change the comment of the key; this _does_ require a |
1277 | * passphrase owing to the tamperproofing. |
1278 | * |
2e85c969 |
1279 | * NOTE: In SSH-1, this only requires a passphrase because |
b81cca98 |
1280 | * of inadequacies of the loading and saving mechanisms. In |
1281 | * _principle_, it should be perfectly possible to modify |
2e85c969 |
1282 | * the comment on an SSH-1 key without requiring a |
b81cca98 |
1283 | * passphrase; the only reason I can't do it is because my |
1284 | * loading and saving mechanisms don't include a method of |
1285 | * loading all the key data without also trying to decrypt |
1286 | * the private section. |
1287 | * |
1288 | * I don't consider this to be a problem worth solving, |
1289 | * because (a) to fix it would probably end up bloating |
2e85c969 |
1290 | * PuTTY proper, and (b) SSH-1 is on the way out anyway so |
b81cca98 |
1291 | * it shouldn't be highly significant. If it seriously |
1292 | * bothers anyone then perhaps I _might_ be persuadable. |
1293 | */ |
1294 | setup_passphrases("sponge", NULL); |
1295 | test(0, "puttygen", "-C", "new-comment", filename, NULL); |
1296 | |
1297 | /* |
1298 | * Change the passphrase to nothing. |
1299 | */ |
1300 | setup_passphrases("sponge", "", "", NULL); |
1301 | test(0, "puttygen", "-P", filename, NULL); |
1302 | |
1303 | /* |
1304 | * Change the comment of the key again; this time we expect no |
1305 | * passphrase to be required. |
1306 | */ |
1307 | setup_passphrases(NULL); |
1308 | test(0, "puttygen", "-C", "new-comment-2", filename, NULL); |
1309 | |
1310 | /* |
1311 | * Export the private key into OpenSSH format; no passphrase |
1312 | * should be required since the key is currently unencrypted. |
1313 | * For RSA1 keys, this should give an error. |
1314 | */ |
1315 | setup_passphrases(NULL); |
1316 | test((i==0), "puttygen", "-O", "private-openssh", "-o", osfilename, |
1317 | filename, NULL); |
1318 | |
1319 | if (i) { |
1320 | /* |
1321 | * List the fingerprint of the OpenSSH-formatted key. |
1322 | */ |
1323 | setup_passphrases(NULL); |
1324 | test(0, "puttygen", "-l", osfilename, "-o", tmpfilename1, NULL); |
1325 | check_fp(tmpfilename1, fp, "%s openssh clear fp", keytypes[i]); |
1326 | |
1327 | /* |
1328 | * List the public half of the OpenSSH-formatted key in |
1329 | * OpenSSH format. |
1330 | */ |
1331 | setup_passphrases(NULL); |
1332 | test(0, "puttygen", "-L", osfilename, NULL); |
1333 | |
1334 | /* |
1335 | * List the public half of the OpenSSH-formatted key in |
1336 | * IETF/ssh.com format. |
1337 | */ |
1338 | setup_passphrases(NULL); |
1339 | test(0, "puttygen", "-p", osfilename, NULL); |
1340 | } |
1341 | |
1342 | /* |
1343 | * Export the private key into ssh.com format; no passphrase |
1344 | * should be required since the key is currently unencrypted. |
1345 | * For RSA1 keys, this should give an error. |
1346 | */ |
1347 | setup_passphrases(NULL); |
1348 | test((i==0), "puttygen", "-O", "private-sshcom", "-o", scfilename, |
1349 | filename, NULL); |
1350 | |
1351 | if (i) { |
1352 | /* |
1353 | * List the fingerprint of the ssh.com-formatted key. |
1354 | */ |
1355 | setup_passphrases(NULL); |
1356 | test(0, "puttygen", "-l", scfilename, "-o", tmpfilename1, NULL); |
1357 | check_fp(tmpfilename1, fp, "%s ssh.com clear fp", keytypes[i]); |
1358 | |
1359 | /* |
1360 | * List the public half of the ssh.com-formatted key in |
1361 | * OpenSSH format. |
1362 | */ |
1363 | setup_passphrases(NULL); |
1364 | test(0, "puttygen", "-L", scfilename, NULL); |
1365 | |
1366 | /* |
1367 | * List the public half of the ssh.com-formatted key in |
1368 | * IETF/ssh.com format. |
1369 | */ |
1370 | setup_passphrases(NULL); |
1371 | test(0, "puttygen", "-p", scfilename, NULL); |
1372 | } |
1373 | |
1374 | if (i) { |
1375 | /* |
1376 | * Convert from OpenSSH into ssh.com. |
1377 | */ |
1378 | setup_passphrases(NULL); |
1379 | test(0, "puttygen", osfilename, "-o", tmpfilename1, |
1380 | "-O", "private-sshcom", NULL); |
1381 | |
1382 | /* |
1383 | * Convert from ssh.com back into a PuTTY key, |
1384 | * supplying the same comment as we had before we |
1385 | * started to ensure the comparison works. |
1386 | */ |
1387 | setup_passphrases(NULL); |
1388 | test(0, "puttygen", tmpfilename1, "-C", "new-comment-2", |
1389 | "-o", tmpfilename2, NULL); |
1390 | |
1391 | /* |
1392 | * See if the PuTTY key thus generated is the same as |
1393 | * the original. |
1394 | */ |
1395 | filecmp(filename, tmpfilename2, |
1396 | "p->o->s->p clear %s", keytypes[i]); |
1397 | |
1398 | /* |
1399 | * Convert from ssh.com to OpenSSH. |
1400 | */ |
1401 | setup_passphrases(NULL); |
1402 | test(0, "puttygen", scfilename, "-o", tmpfilename1, |
1403 | "-O", "private-openssh", NULL); |
1404 | |
1405 | /* |
1406 | * Convert from OpenSSH back into a PuTTY key, |
1407 | * supplying the same comment as we had before we |
1408 | * started to ensure the comparison works. |
1409 | */ |
1410 | setup_passphrases(NULL); |
1411 | test(0, "puttygen", tmpfilename1, "-C", "new-comment-2", |
1412 | "-o", tmpfilename2, NULL); |
1413 | |
1414 | /* |
1415 | * See if the PuTTY key thus generated is the same as |
1416 | * the original. |
1417 | */ |
1418 | filecmp(filename, tmpfilename2, |
1419 | "p->s->o->p clear %s", keytypes[i]); |
1420 | |
1421 | /* |
1422 | * Finally, do a round-trip conversion between PuTTY |
1423 | * and ssh.com without involving OpenSSH, to test that |
1424 | * the key comment is preserved in that case. |
1425 | */ |
1426 | setup_passphrases(NULL); |
1427 | test(0, "puttygen", "-O", "private-sshcom", "-o", tmpfilename1, |
1428 | filename, NULL); |
1429 | setup_passphrases(NULL); |
1430 | test(0, "puttygen", tmpfilename1, "-o", tmpfilename2, NULL); |
1431 | filecmp(filename, tmpfilename2, |
1432 | "p->s->p clear %s", keytypes[i]); |
1433 | } |
1434 | |
1435 | /* |
1436 | * Check that mismatched passphrases cause an error. |
1437 | */ |
1438 | setup_passphrases("sponge2", "sponge3", NULL); |
1439 | test(1, "puttygen", "-P", filename, NULL); |
1440 | |
1441 | /* |
1442 | * Put a passphrase back on. |
1443 | */ |
1444 | setup_passphrases("sponge2", "sponge2", NULL); |
1445 | test(0, "puttygen", "-P", filename, NULL); |
1446 | |
1447 | /* |
1448 | * Export the private key into OpenSSH format, this time |
1449 | * while encrypted. For RSA1 keys, this should give an |
1450 | * error. |
1451 | */ |
1452 | if (i == 0) |
1453 | setup_passphrases(NULL); /* error, hence no passphrase read */ |
1454 | else |
1455 | setup_passphrases("sponge2", NULL); |
1456 | test((i==0), "puttygen", "-O", "private-openssh", "-o", osfilename, |
1457 | filename, NULL); |
1458 | |
1459 | if (i) { |
1460 | /* |
1461 | * List the fingerprint of the OpenSSH-formatted key. |
1462 | */ |
1463 | setup_passphrases("sponge2", NULL); |
1464 | test(0, "puttygen", "-l", osfilename, "-o", tmpfilename1, NULL); |
1465 | check_fp(tmpfilename1, fp, "%s openssh encrypted fp", keytypes[i]); |
1466 | |
1467 | /* |
1468 | * List the public half of the OpenSSH-formatted key in |
1469 | * OpenSSH format. |
1470 | */ |
1471 | setup_passphrases("sponge2", NULL); |
1472 | test(0, "puttygen", "-L", osfilename, NULL); |
1473 | |
1474 | /* |
1475 | * List the public half of the OpenSSH-formatted key in |
1476 | * IETF/ssh.com format. |
1477 | */ |
1478 | setup_passphrases("sponge2", NULL); |
1479 | test(0, "puttygen", "-p", osfilename, NULL); |
1480 | } |
1481 | |
1482 | /* |
1483 | * Export the private key into ssh.com format, this time |
1484 | * while encrypted. For RSA1 keys, this should give an |
1485 | * error. |
1486 | */ |
1487 | if (i == 0) |
1488 | setup_passphrases(NULL); /* error, hence no passphrase read */ |
1489 | else |
1490 | setup_passphrases("sponge2", NULL); |
1491 | test((i==0), "puttygen", "-O", "private-sshcom", "-o", scfilename, |
1492 | filename, NULL); |
1493 | |
1494 | if (i) { |
1495 | /* |
1496 | * List the fingerprint of the ssh.com-formatted key. |
1497 | */ |
1498 | setup_passphrases("sponge2", NULL); |
1499 | test(0, "puttygen", "-l", scfilename, "-o", tmpfilename1, NULL); |
1500 | check_fp(tmpfilename1, fp, "%s ssh.com encrypted fp", keytypes[i]); |
1501 | |
1502 | /* |
1503 | * List the public half of the ssh.com-formatted key in |
1504 | * OpenSSH format. |
1505 | */ |
1506 | setup_passphrases("sponge2", NULL); |
1507 | test(0, "puttygen", "-L", scfilename, NULL); |
1508 | |
1509 | /* |
1510 | * List the public half of the ssh.com-formatted key in |
1511 | * IETF/ssh.com format. |
1512 | */ |
1513 | setup_passphrases("sponge2", NULL); |
1514 | test(0, "puttygen", "-p", scfilename, NULL); |
1515 | } |
1516 | |
1517 | if (i) { |
1518 | /* |
1519 | * Convert from OpenSSH into ssh.com. |
1520 | */ |
1521 | setup_passphrases("sponge2", NULL); |
1522 | test(0, "puttygen", osfilename, "-o", tmpfilename1, |
1523 | "-O", "private-sshcom", NULL); |
1524 | |
1525 | /* |
1526 | * Convert from ssh.com back into a PuTTY key, |
1527 | * supplying the same comment as we had before we |
1528 | * started to ensure the comparison works. |
1529 | */ |
1530 | setup_passphrases("sponge2", NULL); |
1531 | test(0, "puttygen", tmpfilename1, "-C", "new-comment-2", |
1532 | "-o", tmpfilename2, NULL); |
1533 | |
1534 | /* |
1535 | * See if the PuTTY key thus generated is the same as |
1536 | * the original. |
1537 | */ |
1538 | filecmp(filename, tmpfilename2, |
1539 | "p->o->s->p encrypted %s", keytypes[i]); |
1540 | |
1541 | /* |
1542 | * Convert from ssh.com to OpenSSH. |
1543 | */ |
1544 | setup_passphrases("sponge2", NULL); |
1545 | test(0, "puttygen", scfilename, "-o", tmpfilename1, |
1546 | "-O", "private-openssh", NULL); |
1547 | |
1548 | /* |
1549 | * Convert from OpenSSH back into a PuTTY key, |
1550 | * supplying the same comment as we had before we |
1551 | * started to ensure the comparison works. |
1552 | */ |
1553 | setup_passphrases("sponge2", NULL); |
1554 | test(0, "puttygen", tmpfilename1, "-C", "new-comment-2", |
1555 | "-o", tmpfilename2, NULL); |
1556 | |
1557 | /* |
1558 | * See if the PuTTY key thus generated is the same as |
1559 | * the original. |
1560 | */ |
1561 | filecmp(filename, tmpfilename2, |
1562 | "p->s->o->p encrypted %s", keytypes[i]); |
1563 | |
1564 | /* |
1565 | * Finally, do a round-trip conversion between PuTTY |
1566 | * and ssh.com without involving OpenSSH, to test that |
1567 | * the key comment is preserved in that case. |
1568 | */ |
1569 | setup_passphrases("sponge2", NULL); |
1570 | test(0, "puttygen", "-O", "private-sshcom", "-o", tmpfilename1, |
1571 | filename, NULL); |
1572 | setup_passphrases("sponge2", NULL); |
1573 | test(0, "puttygen", tmpfilename1, "-o", tmpfilename2, NULL); |
1574 | filecmp(filename, tmpfilename2, |
1575 | "p->s->p encrypted %s", keytypes[i]); |
1576 | } |
1577 | |
1578 | /* |
1579 | * Load with the wrong passphrase. |
1580 | */ |
1581 | setup_passphrases("sponge8", NULL); |
1582 | test(1, "puttygen", "-C", "spurious-new-comment", filename, NULL); |
1583 | |
1584 | /* |
1585 | * Load a totally bogus file. |
1586 | */ |
1587 | setup_passphrases(NULL); |
1588 | test(1, "puttygen", "-C", "spurious-new-comment", pubfilename, NULL); |
1589 | } |
1590 | printf("%d passes, %d fails\n", passes, fails); |
1591 | return 0; |
1592 | } |
1593 | |
1594 | #endif |