Variable key size in PuTTYgen. Also required adding WM_VSCROLL to the
[sgt/putty] / puttygen.c
1 /*
2 * PuTTY key generation front end.
3 */
4
5 #include <windows.h>
6 #include <commctrl.h>
7 #include <time.h>
8 #ifndef NO_SECURITY
9 #include <aclapi.h>
10 #endif
11 #include <stdio.h>
12
13 #define PUTTY_DO_GLOBALS
14
15 #include "putty.h"
16 #include "ssh.h"
17 #include "winstuff.h"
18
19 #define WM_DONEKEY (WM_XUSER + 1)
20
21 #define DEFAULT_KEYSIZE 1024
22
23 /*
24 * TODO:
25 * - test the generated keys for actual working-RSA-key-hood
26 */
27
28 /* ----------------------------------------------------------------------
29 * Progress report code. This is really horrible :-)
30 */
31 #define PHASE1TOTAL 0x10000
32 #define PHASE2TOTAL 0x10000
33 #define PHASE3TOTAL 0x04000
34 #define PHASE1START 0
35 #define PHASE2START (PHASE1TOTAL)
36 #define PHASE3START (PHASE1TOTAL + PHASE2TOTAL)
37 #define TOTALTOTAL (PHASE1TOTAL + PHASE2TOTAL + PHASE3TOTAL)
38 #define PROGRESSBIGRANGE 65535
39 #define DIVISOR ((TOTALTOTAL + PROGRESSBIGRANGE - 1) / PROGRESSBIGRANGE)
40 #define PROGRESSRANGE (TOTALTOTAL / DIVISOR)
41 struct progress {
42 unsigned phase1param, phase1current, phase1n;
43 unsigned phase2param, phase2current, phase2n;
44 unsigned phase3mult;
45 HWND progbar;
46 };
47
48 static void progress_update(void *param, int phase, int iprogress) {
49 struct progress *p = (struct progress *)param;
50 unsigned progress = iprogress;
51 int position;
52
53 switch (phase) {
54 case -1:
55 p->phase1param = 0x10000 + progress;
56 p->phase1current = 0x10000; p->phase1n = 0;
57 return;
58 case -2:
59 p->phase2param = 0x10000 + progress;
60 p->phase2current = 0x10000; p->phase2n = 0;
61 return;
62 case -3:
63 p->phase3mult = PHASE3TOTAL / progress;
64 return;
65 case 1:
66 while (p->phase1n < progress) {
67 p->phase1n++;
68 p->phase1current *= p->phase1param;
69 p->phase1current /= 0x10000;
70 }
71 position = PHASE1START + 0x10000 - p->phase1current;
72 break;
73 case 2:
74 while (p->phase2n < progress) {
75 p->phase2n++;
76 p->phase2current *= p->phase2param;
77 p->phase2current /= 0x10000;
78 }
79 position = PHASE2START + 0x10000 - p->phase2current;
80 break;
81 case 3:
82 position = PHASE3START + progress * p->phase3mult;
83 break;
84 }
85
86 SendMessage(p->progbar, PBM_SETPOS, position / DIVISOR, 0);
87 }
88
89 extern char ver[];
90
91 #define PASSPHRASE_MAXLEN 512
92
93 struct PassphraseProcStruct {
94 char *passphrase;
95 char *comment;
96 };
97
98 /*
99 * Dialog-box function for the passphrase box.
100 */
101 static int CALLBACK PassphraseProc(HWND hwnd, UINT msg,
102 WPARAM wParam, LPARAM lParam) {
103 static char *passphrase;
104 struct PassphraseProcStruct *p;
105
106 switch (msg) {
107 case WM_INITDIALOG:
108 SetForegroundWindow(hwnd);
109 SetWindowPos (hwnd, HWND_TOP, 0, 0, 0, 0,
110 SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
111 p = (struct PassphraseProcStruct *)lParam;
112 passphrase = p->passphrase;
113 if (p->comment)
114 SetDlgItemText(hwnd, 101, p->comment);
115 *passphrase = 0;
116 return 0;
117 case WM_COMMAND:
118 switch (LOWORD(wParam)) {
119 case IDOK:
120 if (*passphrase)
121 EndDialog (hwnd, 1);
122 else
123 MessageBeep (0);
124 return 0;
125 case IDCANCEL:
126 EndDialog (hwnd, 0);
127 return 0;
128 case 102: /* edit box */
129 if (HIWORD(wParam) == EN_CHANGE) {
130 GetDlgItemText (hwnd, 102, passphrase, PASSPHRASE_MAXLEN-1);
131 passphrase[PASSPHRASE_MAXLEN-1] = '\0';
132 }
133 return 0;
134 }
135 return 0;
136 case WM_CLOSE:
137 EndDialog (hwnd, 0);
138 return 0;
139 }
140 return 0;
141 }
142
143 /*
144 * Prompt for a key file. Assumes the filename buffer is of size
145 * FILENAME_MAX.
146 */
147 static int prompt_keyfile(HWND hwnd, char *dlgtitle,
148 char *filename, int save) {
149 OPENFILENAME of;
150 memset(&of, 0, sizeof(of));
151 #ifdef OPENFILENAME_SIZE_VERSION_400
152 of.lStructSize = OPENFILENAME_SIZE_VERSION_400;
153 #else
154 of.lStructSize = sizeof(of);
155 #endif
156 of.hwndOwner = hwnd;
157 of.lpstrFilter = "All Files\0*\0\0\0";
158 of.lpstrCustomFilter = NULL;
159 of.nFilterIndex = 1;
160 of.lpstrFile = filename; *filename = '\0';
161 of.nMaxFile = FILENAME_MAX;
162 of.lpstrFileTitle = NULL;
163 of.lpstrInitialDir = NULL;
164 of.lpstrTitle = dlgtitle;
165 of.Flags = 0;
166 if (save)
167 return GetSaveFileName(&of);
168 else
169 return GetOpenFileName(&of);
170 }
171
172 /*
173 * This function is needed to link with the DES code. We need not
174 * have it do anything at all.
175 */
176 void logevent(char *msg) {
177 }
178
179 /*
180 * Dialog-box function for the Licence box.
181 */
182 static int CALLBACK LicenceProc (HWND hwnd, UINT msg,
183 WPARAM wParam, LPARAM lParam) {
184 switch (msg) {
185 case WM_INITDIALOG:
186 return 1;
187 case WM_COMMAND:
188 switch (LOWORD(wParam)) {
189 case IDOK:
190 EndDialog(hwnd, 1);
191 return 0;
192 }
193 return 0;
194 case WM_CLOSE:
195 EndDialog(hwnd, 1);
196 return 0;
197 }
198 return 0;
199 }
200
201 /*
202 * Dialog-box function for the About box.
203 */
204 static int CALLBACK AboutProc (HWND hwnd, UINT msg,
205 WPARAM wParam, LPARAM lParam) {
206 switch (msg) {
207 case WM_INITDIALOG:
208 SetDlgItemText (hwnd, 100, ver);
209 return 1;
210 case WM_COMMAND:
211 switch (LOWORD(wParam)) {
212 case IDOK:
213 EndDialog(hwnd, 1);
214 return 0;
215 case 101:
216 EnableWindow(hwnd, 0);
217 DialogBox (hinst, MAKEINTRESOURCE(214), NULL, LicenceProc);
218 EnableWindow(hwnd, 1);
219 SetActiveWindow(hwnd);
220 return 0;
221 }
222 return 0;
223 case WM_CLOSE:
224 EndDialog(hwnd, 1);
225 return 0;
226 }
227 return 0;
228 }
229
230 /*
231 * Thread to generate a key.
232 */
233 struct rsa_key_thread_params {
234 HWND progressbar; /* notify this with progress */
235 HWND dialog; /* notify this on completion */
236 int keysize; /* bits in key */
237 struct RSAKey *key;
238 struct RSAAux *aux;
239 };
240 static DWORD WINAPI generate_rsa_key_thread(void *param) {
241 struct rsa_key_thread_params *params =
242 (struct rsa_key_thread_params *)param;
243 struct progress prog;
244 prog.progbar = params->progressbar;
245
246 rsa_generate(params->key, params->aux,
247 params->keysize, progress_update, &prog);
248
249 PostMessage(params->dialog, WM_DONEKEY, 0, 0);
250
251 free(params);
252 return 0;
253 }
254
255 struct MainDlgState {
256 int collecting_entropy;
257 int generation_thread_exists;
258 int key_exists;
259 int entropy_got, entropy_required, entropy_size;
260 int keysize;
261 unsigned *entropy;
262 struct RSAKey key;
263 struct RSAAux aux;
264 };
265
266 static void hidemany(HWND hwnd, const int *ids, int hideit) {
267 while (*ids) {
268 ShowWindow(GetDlgItem(hwnd, *ids++), (hideit ? SW_HIDE : SW_SHOW));
269 }
270 }
271
272 static void setupbigedit(HWND hwnd, int id, struct RSAKey *key) {
273 char *buffer;
274 char *dec1, *dec2;
275
276 dec1 = bignum_decimal(key->exponent);
277 dec2 = bignum_decimal(key->modulus);
278 buffer = malloc(strlen(dec1)+strlen(dec2)+
279 strlen(key->comment)+30);
280 sprintf(buffer, "%d %s %s %s",
281 ssh1_bignum_bitcount(key->modulus),
282 dec1, dec2, key->comment);
283 SetDlgItemText(hwnd, id, buffer);
284 free(dec1);
285 free(dec2);
286 free(buffer);
287 }
288
289 /*
290 * Dialog-box function for the main PuTTYgen dialog box.
291 */
292 static int CALLBACK MainDlgProc (HWND hwnd, UINT msg,
293 WPARAM wParam, LPARAM lParam) {
294 enum {
295 controlidstart = 100,
296 IDC_TITLE,
297 IDC_BOX_KEY, IDC_BOXT_KEY,
298 IDC_NOKEY,
299 IDC_GENERATING,
300 IDC_PROGRESS,
301 IDC_PKSTATIC, IDC_KEYDISPLAY,
302 IDC_FPSTATIC, IDC_FINGERPRINT,
303 IDC_COMMENTSTATIC, IDC_COMMENTEDIT,
304 IDC_PASSPHRASE1STATIC, IDC_PASSPHRASE1EDIT,
305 IDC_PASSPHRASE2STATIC, IDC_PASSPHRASE2EDIT,
306 IDC_BOX_ACTIONS, IDC_BOXT_ACTIONS,
307 IDC_GENSTATIC, IDC_GENERATE,
308 IDC_LOADSTATIC, IDC_LOAD,
309 IDC_SAVESTATIC, IDC_SAVE,
310 IDC_BOX_PARAMS, IDC_BOXT_PARAMS,
311 IDC_BITSSTATIC, IDC_BITS,
312 IDC_ABOUT,
313 };
314 static const int nokey_ids[] = { IDC_NOKEY, 0 };
315 static const int generating_ids[] = { IDC_GENERATING, IDC_PROGRESS, 0 };
316 static const int gotkey_ids[] = {
317 IDC_PKSTATIC, IDC_KEYDISPLAY,
318 IDC_FPSTATIC, IDC_FINGERPRINT,
319 IDC_COMMENTSTATIC, IDC_COMMENTEDIT,
320 IDC_PASSPHRASE1STATIC, IDC_PASSPHRASE1EDIT,
321 IDC_PASSPHRASE2STATIC, IDC_PASSPHRASE2EDIT, 0 };
322 static const char generating_msg[] =
323 "Please wait while a key is generated...";
324 static const char entropy_msg[] =
325 "Please move the mouse in this window to generate randomness";
326 struct MainDlgState *state;
327
328 switch (msg) {
329 case WM_INITDIALOG:
330 state = malloc(sizeof(*state));
331 state->generation_thread_exists = FALSE;
332 state->key_exists = FALSE;
333 SetWindowLong(hwnd, GWL_USERDATA, (LONG)state);
334 {
335 struct ctlpos cp, cp2;
336
337 /* Accelerators used: acglops */
338
339 ctlposinit(&cp, hwnd, 10, 10, 10);
340 bartitle(&cp, "Public and private key generation for PuTTY",
341 IDC_TITLE);
342 beginbox(&cp, "Key",
343 IDC_BOX_KEY, IDC_BOXT_KEY);
344 cp2 = cp;
345 statictext(&cp2, "No key.", IDC_NOKEY);
346 cp2 = cp;
347 statictext(&cp2, "",
348 IDC_GENERATING);
349 progressbar(&cp2, IDC_PROGRESS);
350 bigeditctrl(&cp,
351 "&Public key for pasting into authorized_keys file:",
352 IDC_PKSTATIC, IDC_KEYDISPLAY, 7);
353 SendDlgItemMessage(hwnd, IDC_KEYDISPLAY, EM_SETREADONLY, 1, 0);
354 staticedit(&cp, "Key fingerprint:", IDC_FPSTATIC,
355 IDC_FINGERPRINT, 70);
356 SendDlgItemMessage(hwnd, IDC_FINGERPRINT, EM_SETREADONLY, 1, 0);
357 staticedit(&cp, "Key &comment:", IDC_COMMENTSTATIC,
358 IDC_COMMENTEDIT, 70);
359 staticpassedit(&cp, "Key p&assphrase:", IDC_PASSPHRASE1STATIC,
360 IDC_PASSPHRASE1EDIT, 70);
361 staticpassedit(&cp, "C&onfirm passphrase:", IDC_PASSPHRASE2STATIC,
362 IDC_PASSPHRASE2EDIT, 70);
363 endbox(&cp);
364 beginbox(&cp, "Actions",
365 IDC_BOX_ACTIONS, IDC_BOXT_ACTIONS);
366 staticbtn(&cp, "Generate a public/private key pair",
367 IDC_GENSTATIC, "&Generate", IDC_GENERATE);
368 staticbtn(&cp, "Load an existing private key file",
369 IDC_LOADSTATIC, "&Load", IDC_LOAD);
370 staticbtn(&cp, "Save the generated key to a new file",
371 IDC_SAVESTATIC, "&Save", IDC_SAVE);
372 endbox(&cp);
373 beginbox(&cp, "Actions",
374 IDC_BOX_ACTIONS, IDC_BOXT_ACTIONS);
375 staticedit(&cp, "Length of generated keys in &bits:",
376 IDC_BITSSTATIC, IDC_BITS, 20);
377 endbox(&cp);
378 }
379 SetDlgItemInt(hwnd, IDC_BITS, DEFAULT_KEYSIZE, FALSE);
380
381 /*
382 * Initially, hide the progress bar and the key display,
383 * and show the no-key display. Also disable the Save
384 * button, because with no key we obviously can't save
385 * anything.
386 */
387 hidemany(hwnd, nokey_ids, FALSE);
388 hidemany(hwnd, generating_ids, TRUE);
389 hidemany(hwnd, gotkey_ids, TRUE);
390 EnableWindow(GetDlgItem(hwnd, IDC_SAVE), 0);
391
392 return 1;
393 case WM_MOUSEMOVE:
394 state = (struct MainDlgState *)GetWindowLong(hwnd, GWL_USERDATA);
395 if (state->collecting_entropy) {
396 state->entropy[state->entropy_got++] = lParam;
397 state->entropy[state->entropy_got++] = GetMessageTime();
398 SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETPOS,
399 state->entropy_got, 0);
400 if (state->entropy_got >= state->entropy_required) {
401 struct rsa_key_thread_params *params;
402 DWORD threadid;
403
404 /*
405 * Seed the entropy pool
406 */
407 random_add_heavynoise(state->entropy, state->entropy_size);
408 memset(state->entropy, 0, state->entropy_size);
409 free(state->entropy);
410
411 SetDlgItemText(hwnd, IDC_GENERATING, generating_msg);
412 SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETRANGE, 0,
413 MAKELPARAM(0, PROGRESSRANGE));
414 SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETPOS, 0, 0);
415
416 params = malloc(sizeof(*params));
417 params->progressbar = GetDlgItem(hwnd, IDC_PROGRESS);
418 params->dialog = hwnd;
419 params->keysize = state->keysize;
420 params->key = &state->key;
421 params->aux = &state->aux;
422
423 if (!CreateThread(NULL, 0, generate_rsa_key_thread,
424 params, 0, &threadid)) {
425 MessageBox(hwnd, "Out of thread resources",
426 "Key generation error",
427 MB_OK | MB_ICONERROR);
428 free(params);
429 } else {
430 state->generation_thread_exists = TRUE;
431 state->collecting_entropy = FALSE;
432 }
433 }
434 }
435 break;
436 case WM_COMMAND:
437 switch (LOWORD(wParam)) {
438 case IDC_COMMENTEDIT:
439 if (HIWORD(wParam) == EN_CHANGE) {
440 state = (struct MainDlgState *)
441 GetWindowLong(hwnd, GWL_USERDATA);
442 if (state->key_exists) {
443 HWND editctl = GetDlgItem(hwnd, IDC_COMMENTEDIT);
444 int len = GetWindowTextLength(editctl);
445 if (state->key.comment)
446 free(state->key.comment);
447 state->key.comment = malloc(len+1);
448 GetWindowText(editctl, state->key.comment, len+1);
449 }
450 }
451 break;
452 case IDC_ABOUT:
453 EnableWindow(hwnd, 0);
454 DialogBox (hinst, MAKEINTRESOURCE(213), NULL, AboutProc);
455 EnableWindow(hwnd, 1);
456 SetActiveWindow(hwnd);
457 return 0;
458 case IDC_GENERATE:
459 state = (struct MainDlgState *)GetWindowLong(hwnd, GWL_USERDATA);
460 if (!state->generation_thread_exists) {
461 hidemany(hwnd, nokey_ids, TRUE);
462 hidemany(hwnd, generating_ids, FALSE);
463 hidemany(hwnd, gotkey_ids, TRUE);
464 EnableWindow(GetDlgItem(hwnd, IDC_GENERATE), 0);
465 EnableWindow(GetDlgItem(hwnd, IDC_LOAD), 0);
466 EnableWindow(GetDlgItem(hwnd, IDC_SAVE), 0);
467 state->key_exists = FALSE;
468 SetDlgItemText(hwnd, IDC_GENERATING, entropy_msg);
469 state->collecting_entropy = TRUE;
470 {
471 BOOL ok;
472 state->keysize = GetDlgItemInt(hwnd, IDC_BITS,
473 &ok, FALSE);
474 if (!ok) state->keysize = DEFAULT_KEYSIZE;
475 }
476
477 /*
478 * My brief statistical tests on mouse movements
479 * suggest that there are about 5 bits of
480 * randomness in the x position, 5 in the y
481 * position, and 1.7 in the message time, making
482 * 11.7 bits of unpredictability per mouse
483 * movement. However, other people have told me
484 * it's far less than that, so I'm going to be
485 * stupidly cautious and knock that down to a nice
486 * round 4.
487 */
488 state->entropy_required = (state->keysize / 4) * 2;
489 state->entropy_got = 0;
490 state->entropy_size = (state->entropy_required *
491 sizeof(*state->entropy));
492 state->entropy = malloc(state->entropy_size);
493
494 SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETRANGE, 0,
495 MAKELPARAM(0, state->entropy_required));
496 SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETPOS, 0, 0);
497 }
498 break;
499 case IDC_SAVE:
500 state = (struct MainDlgState *)GetWindowLong(hwnd, GWL_USERDATA);
501 if (state->key_exists) {
502 char filename[FILENAME_MAX];
503 char passphrase[PASSPHRASE_MAXLEN];
504 char passphrase2[PASSPHRASE_MAXLEN];
505 GetDlgItemText(hwnd, IDC_PASSPHRASE1EDIT,
506 passphrase, sizeof(passphrase));
507 GetDlgItemText(hwnd, IDC_PASSPHRASE2EDIT,
508 passphrase2, sizeof(passphrase2));
509 if (strcmp(passphrase, passphrase2)) {
510 MessageBox(hwnd,
511 "The two passphrases given do not match.",
512 "PuTTYgen Error",
513 MB_OK | MB_ICONERROR);
514 break;
515 }
516 if (!*passphrase) {
517 int ret;
518 ret = MessageBox(hwnd,
519 "Are you sure you want to save this key\n"
520 "without a passphrase to protect it?",
521 "PuTTYgen Warning",
522 MB_YESNO | MB_ICONWARNING);
523 if (ret != IDYES)
524 break;
525 }
526 if (prompt_keyfile(hwnd, "Save private key as:",
527 filename, 1)) {
528 int ret;
529 FILE *fp = fopen(filename, "r");
530 if (fp) {
531 char buffer[FILENAME_MAX+80];
532 fclose(fp);
533 sprintf(buffer, "Overwrite existing file\n%.*s?",
534 FILENAME_MAX, filename);
535 ret = MessageBox(hwnd, buffer, "PuTTYgen Warning",
536 MB_YESNO | MB_ICONWARNING);
537 if (ret != IDYES)
538 break;
539 }
540 ret = saversakey(filename, &state->key, &state->aux,
541 *passphrase ? passphrase : NULL);
542 if (ret <= 0) {
543 MessageBox(hwnd, "Unable to save key file",
544 "PuTTYgen Error",
545 MB_OK | MB_ICONERROR);
546 }
547 }
548 }
549 break;
550 case IDC_LOAD:
551 state = (struct MainDlgState *)GetWindowLong(hwnd, GWL_USERDATA);
552 if (!state->generation_thread_exists) {
553 char filename[FILENAME_MAX];
554 if (prompt_keyfile(hwnd, "Load private key:",
555 filename, 0)) {
556 char passphrase[PASSPHRASE_MAXLEN];
557 int needs_pass;
558 int ret;
559 char *comment;
560 struct PassphraseProcStruct pps;
561 struct RSAKey newkey;
562 struct RSAAux newaux;
563
564 needs_pass = rsakey_encrypted(filename, &comment);
565 pps.passphrase = passphrase;
566 pps.comment = comment;
567 do {
568 if (needs_pass) {
569 int dlgret;
570 dlgret = DialogBoxParam(hinst,
571 MAKEINTRESOURCE(210),
572 NULL, PassphraseProc,
573 (LPARAM)&pps);
574 if (!dlgret) {
575 ret = -2;
576 break;
577 }
578 } else
579 *passphrase = '\0';
580 ret = loadrsakey(filename, &newkey, &newaux,
581 passphrase);
582 } while (ret == -1);
583 if (comment) free(comment);
584 if (ret == 0) {
585 MessageBox(NULL, "Couldn't load private key.",
586 "PuTTYgen Error", MB_OK | MB_ICONERROR);
587 } else if (ret == 1) {
588 state->key = newkey;
589 state->aux = newaux;
590
591 EnableWindow(GetDlgItem(hwnd, IDC_GENERATE), 1);
592 EnableWindow(GetDlgItem(hwnd, IDC_LOAD), 1);
593 EnableWindow(GetDlgItem(hwnd, IDC_SAVE), 1);
594 /*
595 * Now update the key controls with all the
596 * key data.
597 */
598 {
599 char buf[128];
600 SetDlgItemText(hwnd, IDC_PASSPHRASE1EDIT,
601 passphrase);
602 SetDlgItemText(hwnd, IDC_PASSPHRASE2EDIT,
603 passphrase);
604 SetDlgItemText(hwnd, IDC_COMMENTEDIT,
605 state->key.comment);
606 /*
607 * Set the key fingerprint.
608 */
609 {
610 char *savecomment = state->key.comment;
611 state->key.comment = NULL;
612 rsa_fingerprint(buf, sizeof(buf), &state->key);
613 state->key.comment = savecomment;
614 }
615 SetDlgItemText(hwnd, IDC_FINGERPRINT, buf);
616 /*
617 * Construct a decimal representation
618 * of the key, for pasting into
619 * .ssh/authorized_keys on a Unix box.
620 */
621 setupbigedit(hwnd, IDC_KEYDISPLAY, &state->key);
622 }
623 /*
624 * Finally, hide the progress bar and show
625 * the key data.
626 */
627 hidemany(hwnd, nokey_ids, TRUE);
628 hidemany(hwnd, generating_ids, TRUE);
629 hidemany(hwnd, gotkey_ids, FALSE);
630 state->key_exists = TRUE;
631 }
632 }
633 }
634 break;
635 }
636 return 0;
637 case WM_DONEKEY:
638 state = (struct MainDlgState *)GetWindowLong(hwnd, GWL_USERDATA);
639 state->generation_thread_exists = FALSE;
640 state->key_exists = TRUE;
641 SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETPOS, PROGRESSRANGE, 0);
642 EnableWindow(GetDlgItem(hwnd, IDC_GENERATE), 1);
643 EnableWindow(GetDlgItem(hwnd, IDC_LOAD), 1);
644 EnableWindow(GetDlgItem(hwnd, IDC_SAVE), 1);
645 /*
646 * Invent a comment for the key. We'll do this by including
647 * the date in it. This will be so horrifyingly ugly that
648 * the user will immediately want to change it, which is
649 * what we want :-)
650 */
651 state->key.comment = malloc(30);
652 {
653 time_t t;
654 struct tm *tm;
655 time(&t);
656 tm = localtime(&t);
657 strftime(state->key.comment, 30, "rsa-key-%Y%m%d", tm);
658 }
659
660 /*
661 * Now update the key controls with all the key data.
662 */
663 {
664 char buf[128];
665 /*
666 * Blank passphrase, initially. This isn't dangerous,
667 * because we will warn (Are You Sure?) before allowing
668 * the user to save an unprotected private key.
669 */
670 SetDlgItemText(hwnd, IDC_PASSPHRASE1EDIT, "");
671 SetDlgItemText(hwnd, IDC_PASSPHRASE2EDIT, "");
672 /*
673 * Set the comment.
674 */
675 SetDlgItemText(hwnd, IDC_COMMENTEDIT, state->key.comment);
676 /*
677 * Set the key fingerprint.
678 */
679 {
680 char *savecomment = state->key.comment;
681 state->key.comment = NULL;
682 rsa_fingerprint(buf, sizeof(buf), &state->key);
683 state->key.comment = savecomment;
684 }
685 SetDlgItemText(hwnd, IDC_FINGERPRINT, buf);
686 /*
687 * Construct a decimal representation of the key, for
688 * pasting into .ssh/authorized_keys on a Unix box.
689 */
690 setupbigedit(hwnd, IDC_KEYDISPLAY, &state->key);
691 }
692 /*
693 * Finally, hide the progress bar and show the key data.
694 */
695 hidemany(hwnd, nokey_ids, TRUE);
696 hidemany(hwnd, generating_ids, TRUE);
697 hidemany(hwnd, gotkey_ids, FALSE);
698 break;
699 case WM_CLOSE:
700 state = (struct MainDlgState *)GetWindowLong(hwnd, GWL_USERDATA);
701 free(state);
702 EndDialog(hwnd, 1);
703 return 0;
704 }
705 return 0;
706 }
707
708 int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show) {
709 InitCommonControls();
710 hinst = inst;
711 random_init();
712 return DialogBox(hinst, MAKEINTRESOURCE(201), NULL, MainDlgProc) != IDOK;
713 }