Entropy fix after better statistical analysis
[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 generate some randomness by moving the mouse over the blank area.";
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, "Number of &bits in a generated key:",
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 2.5 bits of
480 * randomness in the x position, 2.5 in the y
481 * position, and 1.7 in the message time, making
482 * 5.7 bits of unpredictability per mouse movement.
483 * However, other people have told me it's far less
484 * than that, so I'm going to be stupidly cautious
485 * and knock that down to a nice round 2. With this
486 * method, we require two words per mouse movement,
487 * so with 2 bits per mouse movement we expect 2
488 * bits every 2 words.
489 */
490 state->entropy_required = (state->keysize/2) * 2;
491 state->entropy_got = 0;
492 state->entropy_size = (state->entropy_required *
493 sizeof(*state->entropy));
494 state->entropy = malloc(state->entropy_size);
495
496 SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETRANGE, 0,
497 MAKELPARAM(0, state->entropy_required));
498 SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETPOS, 0, 0);
499 }
500 break;
501 case IDC_SAVE:
502 state = (struct MainDlgState *)GetWindowLong(hwnd, GWL_USERDATA);
503 if (state->key_exists) {
504 char filename[FILENAME_MAX];
505 char passphrase[PASSPHRASE_MAXLEN];
506 char passphrase2[PASSPHRASE_MAXLEN];
507 GetDlgItemText(hwnd, IDC_PASSPHRASE1EDIT,
508 passphrase, sizeof(passphrase));
509 GetDlgItemText(hwnd, IDC_PASSPHRASE2EDIT,
510 passphrase2, sizeof(passphrase2));
511 if (strcmp(passphrase, passphrase2)) {
512 MessageBox(hwnd,
513 "The two passphrases given do not match.",
514 "PuTTYgen Error",
515 MB_OK | MB_ICONERROR);
516 break;
517 }
518 if (!*passphrase) {
519 int ret;
520 ret = MessageBox(hwnd,
521 "Are you sure you want to save this key\n"
522 "without a passphrase to protect it?",
523 "PuTTYgen Warning",
524 MB_YESNO | MB_ICONWARNING);
525 if (ret != IDYES)
526 break;
527 }
528 if (prompt_keyfile(hwnd, "Save private key as:",
529 filename, 1)) {
530 int ret;
531 FILE *fp = fopen(filename, "r");
532 if (fp) {
533 char buffer[FILENAME_MAX+80];
534 fclose(fp);
535 sprintf(buffer, "Overwrite existing file\n%.*s?",
536 FILENAME_MAX, filename);
537 ret = MessageBox(hwnd, buffer, "PuTTYgen Warning",
538 MB_YESNO | MB_ICONWARNING);
539 if (ret != IDYES)
540 break;
541 }
542 ret = saversakey(filename, &state->key, &state->aux,
543 *passphrase ? passphrase : NULL);
544 if (ret <= 0) {
545 MessageBox(hwnd, "Unable to save key file",
546 "PuTTYgen Error",
547 MB_OK | MB_ICONERROR);
548 }
549 }
550 }
551 break;
552 case IDC_LOAD:
553 state = (struct MainDlgState *)GetWindowLong(hwnd, GWL_USERDATA);
554 if (!state->generation_thread_exists) {
555 char filename[FILENAME_MAX];
556 if (prompt_keyfile(hwnd, "Load private key:",
557 filename, 0)) {
558 char passphrase[PASSPHRASE_MAXLEN];
559 int needs_pass;
560 int ret;
561 char *comment;
562 struct PassphraseProcStruct pps;
563 struct RSAKey newkey;
564 struct RSAAux newaux;
565
566 needs_pass = rsakey_encrypted(filename, &comment);
567 pps.passphrase = passphrase;
568 pps.comment = comment;
569 do {
570 if (needs_pass) {
571 int dlgret;
572 dlgret = DialogBoxParam(hinst,
573 MAKEINTRESOURCE(210),
574 NULL, PassphraseProc,
575 (LPARAM)&pps);
576 if (!dlgret) {
577 ret = -2;
578 break;
579 }
580 } else
581 *passphrase = '\0';
582 ret = loadrsakey(filename, &newkey, &newaux,
583 passphrase);
584 } while (ret == -1);
585 if (comment) free(comment);
586 if (ret == 0) {
587 MessageBox(NULL, "Couldn't load private key.",
588 "PuTTYgen Error", MB_OK | MB_ICONERROR);
589 } else if (ret == 1) {
590 state->key = newkey;
591 state->aux = newaux;
592
593 EnableWindow(GetDlgItem(hwnd, IDC_GENERATE), 1);
594 EnableWindow(GetDlgItem(hwnd, IDC_LOAD), 1);
595 EnableWindow(GetDlgItem(hwnd, IDC_SAVE), 1);
596 /*
597 * Now update the key controls with all the
598 * key data.
599 */
600 {
601 char buf[128];
602 SetDlgItemText(hwnd, IDC_PASSPHRASE1EDIT,
603 passphrase);
604 SetDlgItemText(hwnd, IDC_PASSPHRASE2EDIT,
605 passphrase);
606 SetDlgItemText(hwnd, IDC_COMMENTEDIT,
607 state->key.comment);
608 /*
609 * Set the key fingerprint.
610 */
611 {
612 char *savecomment = state->key.comment;
613 state->key.comment = NULL;
614 rsa_fingerprint(buf, sizeof(buf), &state->key);
615 state->key.comment = savecomment;
616 }
617 SetDlgItemText(hwnd, IDC_FINGERPRINT, buf);
618 /*
619 * Construct a decimal representation
620 * of the key, for pasting into
621 * .ssh/authorized_keys on a Unix box.
622 */
623 setupbigedit(hwnd, IDC_KEYDISPLAY, &state->key);
624 }
625 /*
626 * Finally, hide the progress bar and show
627 * the key data.
628 */
629 hidemany(hwnd, nokey_ids, TRUE);
630 hidemany(hwnd, generating_ids, TRUE);
631 hidemany(hwnd, gotkey_ids, FALSE);
632 state->key_exists = TRUE;
633 }
634 }
635 }
636 break;
637 }
638 return 0;
639 case WM_DONEKEY:
640 state = (struct MainDlgState *)GetWindowLong(hwnd, GWL_USERDATA);
641 state->generation_thread_exists = FALSE;
642 state->key_exists = TRUE;
643 SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETPOS, PROGRESSRANGE, 0);
644 EnableWindow(GetDlgItem(hwnd, IDC_GENERATE), 1);
645 EnableWindow(GetDlgItem(hwnd, IDC_LOAD), 1);
646 EnableWindow(GetDlgItem(hwnd, IDC_SAVE), 1);
647 /*
648 * Invent a comment for the key. We'll do this by including
649 * the date in it. This will be so horrifyingly ugly that
650 * the user will immediately want to change it, which is
651 * what we want :-)
652 */
653 state->key.comment = malloc(30);
654 {
655 time_t t;
656 struct tm *tm;
657 time(&t);
658 tm = localtime(&t);
659 strftime(state->key.comment, 30, "rsa-key-%Y%m%d", tm);
660 }
661
662 /*
663 * Now update the key controls with all the key data.
664 */
665 {
666 char buf[128];
667 /*
668 * Blank passphrase, initially. This isn't dangerous,
669 * because we will warn (Are You Sure?) before allowing
670 * the user to save an unprotected private key.
671 */
672 SetDlgItemText(hwnd, IDC_PASSPHRASE1EDIT, "");
673 SetDlgItemText(hwnd, IDC_PASSPHRASE2EDIT, "");
674 /*
675 * Set the comment.
676 */
677 SetDlgItemText(hwnd, IDC_COMMENTEDIT, state->key.comment);
678 /*
679 * Set the key fingerprint.
680 */
681 {
682 char *savecomment = state->key.comment;
683 state->key.comment = NULL;
684 rsa_fingerprint(buf, sizeof(buf), &state->key);
685 state->key.comment = savecomment;
686 }
687 SetDlgItemText(hwnd, IDC_FINGERPRINT, buf);
688 /*
689 * Construct a decimal representation of the key, for
690 * pasting into .ssh/authorized_keys on a Unix box.
691 */
692 setupbigedit(hwnd, IDC_KEYDISPLAY, &state->key);
693 }
694 /*
695 * Finally, hide the progress bar and show the key data.
696 */
697 hidemany(hwnd, nokey_ids, TRUE);
698 hidemany(hwnd, generating_ids, TRUE);
699 hidemany(hwnd, gotkey_ids, FALSE);
700 break;
701 case WM_CLOSE:
702 state = (struct MainDlgState *)GetWindowLong(hwnd, GWL_USERDATA);
703 free(state);
704 EndDialog(hwnd, 1);
705 return 0;
706 }
707 return 0;
708 }
709
710 int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show) {
711 InitCommonControls();
712 hinst = inst;
713 random_init();
714 return DialogBox(hinst, MAKEINTRESOURCE(201), NULL, MainDlgProc) != IDOK;
715 }