]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - puttygen.c
Fix miscellaneous compiler warnings. Thanks to Jacob Nevins
[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->key_exists = FALSE;
326         SetWindowLong(hwnd, GWL_USERDATA, (LONG)state);
327         {
328             struct ctlpos cp, cp2;
329
330             /* Accelerators used: acglops */
331
332             ctlposinit(&cp, hwnd, 10, 10, 10);
333             bartitle(&cp, "Public and private key generation for PuTTY",
334                     IDC_TITLE);
335             beginbox(&cp, "Key",
336                      IDC_BOX_KEY, IDC_BOXT_KEY);
337             cp2 = cp;
338             statictext(&cp2, "No key.", IDC_NOKEY);
339             cp2 = cp;
340             statictext(&cp2, "",
341                        IDC_GENERATING);
342             progressbar(&cp2, IDC_PROGRESS);
343             bigeditctrl(&cp,
344                         "&Public key for pasting into authorized_keys file:",
345                         IDC_PKSTATIC, IDC_KEYDISPLAY, 7);
346             SendDlgItemMessage(hwnd, IDC_KEYDISPLAY, EM_SETREADONLY, 1, 0);
347             staticedit(&cp, "Key fingerprint:", IDC_FPSTATIC,
348                        IDC_FINGERPRINT, 70);
349             SendDlgItemMessage(hwnd, IDC_FINGERPRINT, EM_SETREADONLY, 1, 0);
350             staticedit(&cp, "Key &comment:", IDC_COMMENTSTATIC,
351                        IDC_COMMENTEDIT, 70);
352             staticpassedit(&cp, "Key p&assphrase:", IDC_PASSPHRASE1STATIC,
353                            IDC_PASSPHRASE1EDIT, 70);
354             staticpassedit(&cp, "C&onfirm passphrase:", IDC_PASSPHRASE2STATIC,
355                            IDC_PASSPHRASE2EDIT, 70);
356             endbox(&cp);
357             beginbox(&cp, "Actions",
358                      IDC_BOX_ACTIONS, IDC_BOXT_ACTIONS);
359             staticbtn(&cp, "Generate a public/private key pair",
360                       IDC_GENSTATIC, "&Generate", IDC_GENERATE);
361             staticbtn(&cp, "Load an existing private key file",
362                       IDC_LOADSTATIC, "&Load", IDC_LOAD);
363             staticbtn(&cp, "Save the generated key to a new file",
364                       IDC_SAVESTATIC, "&Save", IDC_SAVE);
365             endbox(&cp);
366             beginbox(&cp, "Parameters",
367                      IDC_BOX_PARAMS, IDC_BOXT_PARAMS);
368             staticedit(&cp, "Number of &bits in a generated key:",
369                        IDC_BITSSTATIC, IDC_BITS, 20);
370             endbox(&cp);
371         }
372         SetDlgItemInt(hwnd, IDC_BITS, DEFAULT_KEYSIZE, FALSE);
373
374         /*
375          * Initially, hide the progress bar and the key display,
376          * and show the no-key display. Also disable the Save
377          * button, because with no key we obviously can't save
378          * anything.
379          */
380         hidemany(hwnd, nokey_ids, FALSE);
381         hidemany(hwnd, generating_ids, TRUE);
382         hidemany(hwnd, gotkey_ids, TRUE);
383         EnableWindow(GetDlgItem(hwnd, IDC_SAVE), 0);
384
385         return 1;
386       case WM_MOUSEMOVE:
387         state = (struct MainDlgState *)GetWindowLong(hwnd, GWL_USERDATA);
388         if (state->collecting_entropy) {
389             state->entropy[state->entropy_got++] = lParam;
390             state->entropy[state->entropy_got++] = GetMessageTime();
391             SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETPOS,
392                                state->entropy_got, 0);
393             if (state->entropy_got >= state->entropy_required) {
394                 struct rsa_key_thread_params *params;
395                 DWORD threadid;
396
397                 /*
398                  * Seed the entropy pool
399                  */
400                 random_add_heavynoise(state->entropy, state->entropy_size);
401                 memset(state->entropy, 0, state->entropy_size);
402                 free(state->entropy);
403
404                 SetDlgItemText(hwnd, IDC_GENERATING, generating_msg);
405                 SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETRANGE, 0,
406                                    MAKELPARAM(0, PROGRESSRANGE));
407                 SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETPOS, 0, 0);
408
409                 params = malloc(sizeof(*params));
410                 params->progressbar = GetDlgItem(hwnd, IDC_PROGRESS);
411                 params->dialog = hwnd;
412                 params->keysize = state->keysize;
413                 params->key = &state->key;
414                 params->aux = &state->aux;
415
416                 if (!CreateThread(NULL, 0, generate_rsa_key_thread,
417                                   params, 0, &threadid)) {
418                     MessageBox(hwnd, "Out of thread resources",
419                                "Key generation error",
420                                MB_OK | MB_ICONERROR);
421                     free(params);
422                 } else {
423                     state->generation_thread_exists = TRUE;
424                     state->collecting_entropy = FALSE;
425                 }
426             }
427         }
428         break;
429       case WM_COMMAND:
430         switch (LOWORD(wParam)) {
431           case IDC_COMMENTEDIT:
432             if (HIWORD(wParam) == EN_CHANGE) {
433                 state = (struct MainDlgState *)
434                     GetWindowLong(hwnd, GWL_USERDATA);
435                 if (state->key_exists) {
436                     HWND editctl = GetDlgItem(hwnd, IDC_COMMENTEDIT);
437                     int len = GetWindowTextLength(editctl);
438                     if (state->key.comment)
439                         free(state->key.comment);
440                     state->key.comment = malloc(len+1);
441                     GetWindowText(editctl, state->key.comment, len+1);
442                 }                
443             }
444             break;
445           case IDC_ABOUT:
446             EnableWindow(hwnd, 0);
447             DialogBox (hinst, MAKEINTRESOURCE(213), NULL, AboutProc);
448             EnableWindow(hwnd, 1);
449             SetActiveWindow(hwnd);
450             return 0;
451           case IDC_GENERATE:
452             state = (struct MainDlgState *)GetWindowLong(hwnd, GWL_USERDATA);
453             if (!state->generation_thread_exists) {
454                 hidemany(hwnd, nokey_ids, TRUE);
455                 hidemany(hwnd, generating_ids, FALSE);
456                 hidemany(hwnd, gotkey_ids, TRUE);
457                 EnableWindow(GetDlgItem(hwnd, IDC_GENERATE), 0);
458                 EnableWindow(GetDlgItem(hwnd, IDC_LOAD), 0);
459                 EnableWindow(GetDlgItem(hwnd, IDC_SAVE), 0);
460                 state->key_exists = FALSE;
461                 SetDlgItemText(hwnd, IDC_GENERATING, entropy_msg);
462                 state->collecting_entropy = TRUE;
463                 {
464                     BOOL ok;
465                     state->keysize = GetDlgItemInt(hwnd, IDC_BITS,
466                                                    &ok, FALSE);
467                     if (!ok) state->keysize = DEFAULT_KEYSIZE;
468                 }
469
470                 /*
471                  * My brief statistical tests on mouse movements
472                  * suggest that there are about 2.5 bits of
473                  * randomness in the x position, 2.5 in the y
474                  * position, and 1.7 in the message time, making
475                  * 5.7 bits of unpredictability per mouse movement.
476                  * However, other people have told me it's far less
477                  * than that, so I'm going to be stupidly cautious
478                  * and knock that down to a nice round 2. With this
479                  * method, we require two words per mouse movement,
480                  * so with 2 bits per mouse movement we expect 2
481                  * bits every 2 words.
482                  */
483                 state->entropy_required = (state->keysize/2) * 2;
484                 state->entropy_got = 0;
485                 state->entropy_size = (state->entropy_required *
486                                        sizeof(*state->entropy));
487                 state->entropy = malloc(state->entropy_size);
488
489                 SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETRANGE, 0,
490                                    MAKELPARAM(0, state->entropy_required));
491                 SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETPOS, 0, 0);
492             }
493             break;
494           case IDC_SAVE:
495             state = (struct MainDlgState *)GetWindowLong(hwnd, GWL_USERDATA);
496             if (state->key_exists) {
497                 char filename[FILENAME_MAX];
498                 char passphrase[PASSPHRASE_MAXLEN];
499                 char passphrase2[PASSPHRASE_MAXLEN];
500                 GetDlgItemText(hwnd, IDC_PASSPHRASE1EDIT,
501                                passphrase, sizeof(passphrase));
502                 GetDlgItemText(hwnd, IDC_PASSPHRASE2EDIT,
503                                passphrase2, sizeof(passphrase2));
504                 if (strcmp(passphrase, passphrase2)) {
505                     MessageBox(hwnd,
506                                "The two passphrases given do not match.",
507                                "PuTTYgen Error",
508                                MB_OK | MB_ICONERROR);
509                     break;
510                 }
511                 if (!*passphrase) {
512                     int ret;
513                     ret = MessageBox(hwnd,
514                                      "Are you sure you want to save this key\n"
515                                      "without a passphrase to protect it?",
516                                      "PuTTYgen Warning",
517                                      MB_YESNO | MB_ICONWARNING);
518                     if (ret != IDYES)
519                         break;
520                 }
521                 if (prompt_keyfile(hwnd, "Save private key as:",
522                                    filename, 1)) {
523                     int ret;
524                     FILE *fp = fopen(filename, "r");
525                     if (fp) {
526                         char buffer[FILENAME_MAX+80];
527                         fclose(fp);
528                         sprintf(buffer, "Overwrite existing file\n%.*s?",
529                                 FILENAME_MAX, filename);
530                         ret = MessageBox(hwnd, buffer, "PuTTYgen Warning",
531                                          MB_YESNO | MB_ICONWARNING);
532                         if (ret != IDYES)
533                             break;
534                     }
535                     ret = saversakey(filename, &state->key, &state->aux,
536                                      *passphrase ? passphrase : NULL);
537                     if (ret <= 0) {
538                         MessageBox(hwnd, "Unable to save key file",
539                                    "PuTTYgen Error",
540                                    MB_OK | MB_ICONERROR);
541                     }
542                 }
543             }
544             break;
545           case IDC_LOAD:
546             state = (struct MainDlgState *)GetWindowLong(hwnd, GWL_USERDATA);
547             if (!state->generation_thread_exists) {
548                 char filename[FILENAME_MAX];
549                 if (prompt_keyfile(hwnd, "Load private key:",
550                                    filename, 0)) {
551                     char passphrase[PASSPHRASE_MAXLEN];
552                     int needs_pass;
553                     int ret;
554                     char *comment;
555                     struct PassphraseProcStruct pps;
556                     struct RSAKey newkey;
557                     struct RSAAux newaux;
558
559                     needs_pass = rsakey_encrypted(filename, &comment);
560                     pps.passphrase = passphrase;
561                     pps.comment = comment;
562                     do {
563                         if (needs_pass) {
564                             int dlgret;
565                             dlgret = DialogBoxParam(hinst,
566                                                     MAKEINTRESOURCE(210),
567                                                     NULL, PassphraseProc,
568                                                     (LPARAM)&pps);
569                             if (!dlgret) {
570                                 ret = -2;
571                                 break;
572                             }
573                         } else
574                             *passphrase = '\0';
575                         ret = loadrsakey(filename, &newkey, &newaux,
576                                          passphrase);
577                     } while (ret == -1);
578                     if (comment) free(comment);
579                     if (ret == 0) {
580                         MessageBox(NULL, "Couldn't load private key.",
581                                    "PuTTYgen Error", MB_OK | MB_ICONERROR);
582                     } else if (ret == 1) {
583                         state->key = newkey;
584                         state->aux = newaux;
585
586                         EnableWindow(GetDlgItem(hwnd, IDC_GENERATE), 1);
587                         EnableWindow(GetDlgItem(hwnd, IDC_LOAD), 1);
588                         EnableWindow(GetDlgItem(hwnd, IDC_SAVE), 1);
589                         /*
590                          * Now update the key controls with all the
591                          * key data.
592                          */
593                         {
594                             char buf[128];
595                             SetDlgItemText(hwnd, IDC_PASSPHRASE1EDIT,
596                                            passphrase);
597                             SetDlgItemText(hwnd, IDC_PASSPHRASE2EDIT,
598                                            passphrase);
599                             SetDlgItemText(hwnd, IDC_COMMENTEDIT,
600                                            state->key.comment);
601                             /*
602                              * Set the key fingerprint.
603                              */
604                             {
605                                 char *savecomment = state->key.comment;
606                                 state->key.comment = NULL;
607                                 rsa_fingerprint(buf, sizeof(buf), &state->key);
608                                 state->key.comment = savecomment;
609                             }
610                             SetDlgItemText(hwnd, IDC_FINGERPRINT, buf);
611                             /*
612                              * Construct a decimal representation
613                              * of the key, for pasting into
614                              * .ssh/authorized_keys on a Unix box.
615                              */
616                             setupbigedit(hwnd, IDC_KEYDISPLAY, &state->key);
617                         }
618                         /*
619                          * Finally, hide the progress bar and show
620                          * the key data.
621                          */
622                         hidemany(hwnd, nokey_ids, TRUE);
623                         hidemany(hwnd, generating_ids, TRUE);
624                         hidemany(hwnd, gotkey_ids, FALSE);
625                         state->key_exists = TRUE;
626                     }
627                 }
628             }
629             break;
630         }
631         return 0;
632       case WM_DONEKEY:
633         state = (struct MainDlgState *)GetWindowLong(hwnd, GWL_USERDATA);
634         state->generation_thread_exists = FALSE;
635         state->key_exists = TRUE;
636         SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETPOS, PROGRESSRANGE, 0);
637         EnableWindow(GetDlgItem(hwnd, IDC_GENERATE), 1);
638         EnableWindow(GetDlgItem(hwnd, IDC_LOAD), 1);
639         EnableWindow(GetDlgItem(hwnd, IDC_SAVE), 1);
640         /*
641          * Invent a comment for the key. We'll do this by including
642          * the date in it. This will be so horrifyingly ugly that
643          * the user will immediately want to change it, which is
644          * what we want :-)
645          */
646         state->key.comment = malloc(30);
647         {
648             time_t t;
649             struct tm *tm;
650             time(&t);
651             tm = localtime(&t);
652             strftime(state->key.comment, 30, "rsa-key-%Y%m%d", tm);
653         }
654             
655         /*
656          * Now update the key controls with all the key data.
657          */
658         {
659             char buf[128];
660             /*
661              * Blank passphrase, initially. This isn't dangerous,
662              * because we will warn (Are You Sure?) before allowing
663              * the user to save an unprotected private key.
664              */
665             SetDlgItemText(hwnd, IDC_PASSPHRASE1EDIT, "");
666             SetDlgItemText(hwnd, IDC_PASSPHRASE2EDIT, "");
667             /*
668              * Set the comment.
669              */
670             SetDlgItemText(hwnd, IDC_COMMENTEDIT, state->key.comment);
671             /*
672              * Set the key fingerprint.
673              */
674             {
675                 char *savecomment = state->key.comment;
676                 state->key.comment = NULL;
677                 rsa_fingerprint(buf, sizeof(buf), &state->key);
678                 state->key.comment = savecomment;
679             }
680             SetDlgItemText(hwnd, IDC_FINGERPRINT, buf);
681             /*
682              * Construct a decimal representation of the key, for
683              * pasting into .ssh/authorized_keys on a Unix box.
684              */
685             setupbigedit(hwnd, IDC_KEYDISPLAY, &state->key);
686         }
687         /*
688          * Finally, hide the progress bar and show the key data.
689          */
690         hidemany(hwnd, nokey_ids, TRUE);
691         hidemany(hwnd, generating_ids, TRUE);
692         hidemany(hwnd, gotkey_ids, FALSE);
693         break;
694       case WM_CLOSE:
695         state = (struct MainDlgState *)GetWindowLong(hwnd, GWL_USERDATA);
696         free(state);
697         EndDialog(hwnd, 1);
698         return 0;
699     }
700     return 0;
701 }
702
703 int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show) {
704     InitCommonControls();
705     hinst = inst;
706     random_init();
707     return DialogBox(hinst, MAKEINTRESOURCE(201), NULL, MainDlgProc) != IDOK;
708 }