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