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