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