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