2 * PuTTY key generation front end.
13 #define PUTTY_DO_GLOBALS
19 #define WM_DONEKEY (WM_XUSER + 1)
25 * - prompt before overwriting an existing file
26 * - check the return value from saversakey()
27 * - test the generated keys for actual working-RSA-key-hood
31 /* ----------------------------------------------------------------------
32 * Progress report code. This is really horrible :-)
34 #define PHASE1TOTAL 0x10000
35 #define PHASE2TOTAL 0x10000
36 #define PHASE3TOTAL 0x04000
38 #define PHASE2START (PHASE1TOTAL)
39 #define PHASE3START (PHASE1TOTAL + PHASE2TOTAL)
40 #define TOTALTOTAL (PHASE1TOTAL + PHASE2TOTAL + PHASE3TOTAL)
41 #define PROGRESSBIGRANGE 65535
42 #define DIVISOR ((TOTALTOTAL + PROGRESSBIGRANGE - 1) / PROGRESSBIGRANGE)
43 #define PROGRESSRANGE (TOTALTOTAL / DIVISOR)
45 unsigned phase1param, phase1current, phase1n;
46 unsigned phase2param, phase2current, phase2n;
51 static void progress_update(void *param, int phase, int iprogress) {
52 struct progress *p = (struct progress *)param;
53 unsigned progress = iprogress;
58 p->phase1param = 0x10000 + progress;
59 p->phase1current = 0x10000; p->phase1n = 0;
62 p->phase2param = 0x10000 + progress;
63 p->phase2current = 0x10000; p->phase2n = 0;
66 p->phase3mult = PHASE3TOTAL / progress;
69 while (p->phase1n < progress) {
71 p->phase1current *= p->phase1param;
72 p->phase1current /= 0x10000;
74 position = PHASE1START + 0x10000 - p->phase1current;
77 while (p->phase2n < progress) {
79 p->phase2current *= p->phase2param;
80 p->phase2current /= 0x10000;
82 position = PHASE2START + 0x10000 - p->phase2current;
85 position = PHASE3START + progress * p->phase3mult;
89 SendMessage(p->progbar, PBM_SETPOS, position / DIVISOR, 0);
94 #define PASSPHRASE_MAXLEN 512
96 struct PassphraseProcStruct {
102 * Dialog-box function for the passphrase box.
104 static int CALLBACK PassphraseProc(HWND hwnd, UINT msg,
105 WPARAM wParam, LPARAM lParam) {
106 static char *passphrase;
107 struct PassphraseProcStruct *p;
111 SetForegroundWindow(hwnd);
112 SetWindowPos (hwnd, HWND_TOP, 0, 0, 0, 0,
113 SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
114 p = (struct PassphraseProcStruct *)lParam;
115 passphrase = p->passphrase;
117 SetDlgItemText(hwnd, 101, p->comment);
121 switch (LOWORD(wParam)) {
131 case 102: /* edit box */
132 if (HIWORD(wParam) == EN_CHANGE) {
133 GetDlgItemText (hwnd, 102, passphrase, PASSPHRASE_MAXLEN-1);
134 passphrase[PASSPHRASE_MAXLEN-1] = '\0';
147 * Prompt for a key file. Assumes the filename buffer is of size
150 static int prompt_keyfile(HWND hwnd, char *dlgtitle,
151 char *filename, int save) {
153 memset(&of, 0, sizeof(of));
154 #ifdef OPENFILENAME_SIZE_VERSION_400
155 of.lStructSize = OPENFILENAME_SIZE_VERSION_400;
157 of.lStructSize = sizeof(of);
160 of.lpstrFilter = "All Files\0*\0\0\0";
161 of.lpstrCustomFilter = NULL;
163 of.lpstrFile = filename; *filename = '\0';
164 of.nMaxFile = FILENAME_MAX;
165 of.lpstrFileTitle = NULL;
166 of.lpstrInitialDir = NULL;
167 of.lpstrTitle = dlgtitle;
170 return GetSaveFileName(&of);
172 return GetOpenFileName(&of);
176 * This function is needed to link with the DES code. We need not
177 * have it do anything at all.
179 void logevent(char *msg) {
183 * Dialog-box function for the Licence box.
185 static int CALLBACK LicenceProc (HWND hwnd, UINT msg,
186 WPARAM wParam, LPARAM lParam) {
191 switch (LOWORD(wParam)) {
205 * Dialog-box function for the About box.
207 static int CALLBACK AboutProc (HWND hwnd, UINT msg,
208 WPARAM wParam, LPARAM lParam) {
211 SetDlgItemText (hwnd, 100, ver);
214 switch (LOWORD(wParam)) {
219 EnableWindow(hwnd, 0);
220 DialogBox (hinst, MAKEINTRESOURCE(214), NULL, LicenceProc);
221 EnableWindow(hwnd, 1);
222 SetActiveWindow(hwnd);
234 * Thread to generate a key.
236 struct rsa_key_thread_params {
237 HWND progressbar; /* notify this with progress */
238 HWND dialog; /* notify this on completion */
242 static DWORD WINAPI generate_rsa_key_thread(void *param) {
243 struct rsa_key_thread_params *params =
244 (struct rsa_key_thread_params *)param;
245 struct progress prog;
246 prog.progbar = params->progressbar;
248 rsa_generate(params->key, params->aux, KEYSIZE, progress_update, &prog);
250 PostMessage(params->dialog, WM_DONEKEY, 0, 0);
256 struct MainDlgState {
257 int collecting_entropy;
258 int generation_thread_exists;
260 int entropy_got, entropy_required, entropy_size;
266 static void hidemany(HWND hwnd, const int *ids, int hideit) {
268 ShowWindow(GetDlgItem(hwnd, *ids++), (hideit ? SW_HIDE : SW_SHOW));
272 static void setupbigedit(HWND hwnd, int id, struct RSAKey *key) {
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);
290 * Dialog-box function for the main PuTTYgen dialog box.
292 static int CALLBACK MainDlgProc (HWND hwnd, UINT msg,
293 WPARAM wParam, LPARAM lParam) {
295 controlidstart = 100,
297 IDC_BOX_KEY, IDC_BOXT_KEY,
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,
312 static const int nokey_ids[] = { IDC_NOKEY, 0 };
313 static const int generating_ids[] = { IDC_GENERATING, IDC_PROGRESS, 0 };
314 static const int gotkey_ids[] = {
315 IDC_PKSTATIC, IDC_KEYDISPLAY,
316 IDC_FPSTATIC, IDC_FINGERPRINT,
317 IDC_COMMENTSTATIC, IDC_COMMENTEDIT,
318 IDC_PASSPHRASE1STATIC, IDC_PASSPHRASE1EDIT,
319 IDC_PASSPHRASE2STATIC, IDC_PASSPHRASE2EDIT, 0 };
320 static const char generating_msg[] =
321 "Please wait while a key is generated...";
322 static const char entropy_msg[] =
323 "Please move the mouse in this window to generate randomness";
324 struct MainDlgState *state;
328 state = malloc(sizeof(*state));
329 state->generation_thread_exists = FALSE;
330 state->key_exists = FALSE;
331 SetWindowLong(hwnd, GWL_USERDATA, (LONG)state);
333 struct ctlpos cp, cp2;
335 /* Accelerators used: acglops */
337 ctlposinit(&cp, hwnd, 10, 10, 10);
338 bartitle(&cp, "Public and private key generation for PuTTY",
341 IDC_BOX_KEY, IDC_BOXT_KEY);
343 statictext(&cp2, "No key.", IDC_NOKEY);
347 progressbar(&cp2, IDC_PROGRESS);
349 "&Public key for pasting into authorized_keys file:",
350 IDC_PKSTATIC, IDC_KEYDISPLAY, 7);
351 SendDlgItemMessage(hwnd, IDC_KEYDISPLAY, EM_SETREADONLY, 1, 0);
352 staticedit(&cp, "Key fingerprint:", IDC_FPSTATIC,
353 IDC_FINGERPRINT, 70);
354 SendDlgItemMessage(hwnd, IDC_FINGERPRINT, EM_SETREADONLY, 1, 0);
355 staticedit(&cp, "Key &comment:", IDC_COMMENTSTATIC,
356 IDC_COMMENTEDIT, 70);
357 staticpassedit(&cp, "Key p&assphrase:", IDC_PASSPHRASE1STATIC,
358 IDC_PASSPHRASE1EDIT, 70);
359 staticpassedit(&cp, "C&onfirm passphrase:", IDC_PASSPHRASE2STATIC,
360 IDC_PASSPHRASE2EDIT, 70);
362 beginbox(&cp, "Actions",
363 IDC_BOX_ACTIONS, IDC_BOXT_ACTIONS);
364 staticbtn(&cp, "Generate a public/private key pair",
365 IDC_GENSTATIC, "&Generate", IDC_GENERATE);
366 staticbtn(&cp, "Load an existing private key file",
367 IDC_LOADSTATIC, "&Load", IDC_LOAD);
368 staticbtn(&cp, "Save the generated key to a new file",
369 IDC_SAVESTATIC, "&Save", IDC_SAVE);
373 * Initially, hide the progress bar and the key display,
374 * and show the no-key display. Also disable the Save
375 * button, because with no key we obviously can't save
378 hidemany(hwnd, nokey_ids, FALSE);
379 hidemany(hwnd, generating_ids, TRUE);
380 hidemany(hwnd, gotkey_ids, TRUE);
381 EnableWindow(GetDlgItem(hwnd, IDC_SAVE), 0);
385 state = (struct MainDlgState *)GetWindowLong(hwnd, GWL_USERDATA);
386 if (state->collecting_entropy) {
387 state->entropy[state->entropy_got++] = lParam;
388 state->entropy[state->entropy_got++] = GetMessageTime();
389 SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETPOS,
390 state->entropy_got, 0);
391 if (state->entropy_got >= state->entropy_required) {
392 struct rsa_key_thread_params *params;
396 * Seed the entropy pool
398 random_add_heavynoise(state->entropy, state->entropy_size);
399 memset(state->entropy, 0, state->entropy_size);
400 free(state->entropy);
402 SetDlgItemText(hwnd, IDC_GENERATING, generating_msg);
403 SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETRANGE, 0,
404 MAKELPARAM(0, PROGRESSRANGE));
405 SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETPOS, 0, 0);
407 params = malloc(sizeof(*params));
408 params->progressbar = GetDlgItem(hwnd, IDC_PROGRESS);
409 params->dialog = hwnd;
410 params->key = &state->key;
411 params->aux = &state->aux;
413 if (!CreateThread(NULL, 0, generate_rsa_key_thread,
414 params, 0, &threadid)) {
415 MessageBox(hwnd, "Out of thread resources",
416 "Key generation error",
417 MB_OK | MB_ICONERROR);
420 state->generation_thread_exists = TRUE;
421 state->collecting_entropy = FALSE;
427 switch (LOWORD(wParam)) {
428 case IDC_COMMENTEDIT:
429 if (HIWORD(wParam) == EN_CHANGE) {
430 state = (struct MainDlgState *)
431 GetWindowLong(hwnd, GWL_USERDATA);
432 if (state->key_exists) {
433 HWND editctl = GetDlgItem(hwnd, IDC_COMMENTEDIT);
434 int len = GetWindowTextLength(editctl);
435 if (state->key.comment)
436 free(state->key.comment);
437 state->key.comment = malloc(len+1);
438 GetWindowText(editctl, state->key.comment, len+1);
443 EnableWindow(hwnd, 0);
444 DialogBox (hinst, MAKEINTRESOURCE(213), NULL, AboutProc);
445 EnableWindow(hwnd, 1);
446 SetActiveWindow(hwnd);
449 state = (struct MainDlgState *)GetWindowLong(hwnd, GWL_USERDATA);
450 if (!state->generation_thread_exists) {
451 hidemany(hwnd, nokey_ids, TRUE);
452 hidemany(hwnd, generating_ids, FALSE);
453 hidemany(hwnd, gotkey_ids, TRUE);
454 EnableWindow(GetDlgItem(hwnd, IDC_GENERATE), 0);
455 EnableWindow(GetDlgItem(hwnd, IDC_LOAD), 0);
456 EnableWindow(GetDlgItem(hwnd, IDC_SAVE), 0);
457 state->key_exists = FALSE;
458 SetDlgItemText(hwnd, IDC_GENERATING, entropy_msg);
459 state->collecting_entropy = TRUE;
462 * My brief statistical tests on mouse movements
463 * suggest that there are about 5 bits of
464 * randomness in the x position, 5 in the y
465 * position, and 1.7 in the message time, making
466 * 11.7 bits of unpredictability per mouse
467 * movement. However, other people have told me
468 * it's far less than that, so I'm going to be
469 * stupidly cautious and knock that down to a nice
472 state->entropy_required = (KEYSIZE / 4) * 2;
473 state->entropy_got = 0;
474 state->entropy_size = (state->entropy_required *
475 sizeof(*state->entropy));
476 state->entropy = malloc(state->entropy_size);
478 SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETRANGE, 0,
479 MAKELPARAM(0, state->entropy_required));
480 SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETPOS, 0, 0);
484 state = (struct MainDlgState *)GetWindowLong(hwnd, GWL_USERDATA);
485 if (state->key_exists) {
486 char filename[FILENAME_MAX];
487 char passphrase[PASSPHRASE_MAXLEN];
488 char passphrase2[PASSPHRASE_MAXLEN];
489 GetDlgItemText(hwnd, IDC_PASSPHRASE1EDIT,
490 passphrase, sizeof(passphrase));
491 GetDlgItemText(hwnd, IDC_PASSPHRASE2EDIT,
492 passphrase2, sizeof(passphrase2));
493 if (strcmp(passphrase, passphrase2)) {
495 "The two passphrases given do not match.",
497 MB_OK | MB_ICONERROR);
502 ret = MessageBox(hwnd,
503 "Are you sure you want to save this key\n"
504 "without a passphrase to protect it?",
506 MB_YESNO | MB_ICONWARNING);
510 if (prompt_keyfile(hwnd, "Save private key as:",
512 /* FIXME: prompt before overwriting */
513 saversakey(filename, &state->key, &state->aux,
514 *passphrase ? passphrase : NULL);
515 /* FIXME: check return value */
520 state = (struct MainDlgState *)GetWindowLong(hwnd, GWL_USERDATA);
521 if (!state->generation_thread_exists) {
522 char filename[FILENAME_MAX];
523 if (prompt_keyfile(hwnd, "Load private key:",
525 char passphrase[PASSPHRASE_MAXLEN];
529 struct PassphraseProcStruct pps;
530 struct RSAKey newkey;
531 struct RSAAux newaux;
533 needs_pass = rsakey_encrypted(filename, &comment);
534 pps.passphrase = passphrase;
535 pps.comment = comment;
539 dlgret = DialogBoxParam(hinst,
540 MAKEINTRESOURCE(210),
541 NULL, PassphraseProc,
549 ret = loadrsakey(filename, &newkey, &newaux,
552 if (comment) free(comment);
554 MessageBox(NULL, "Couldn't load private key.",
555 "PuTTYgen Error", MB_OK | MB_ICONERROR);
556 } else if (ret == 1) {
560 EnableWindow(GetDlgItem(hwnd, IDC_GENERATE), 1);
561 EnableWindow(GetDlgItem(hwnd, IDC_LOAD), 1);
562 EnableWindow(GetDlgItem(hwnd, IDC_SAVE), 1);
564 * Now update the key controls with all the
569 SetDlgItemText(hwnd, IDC_PASSPHRASE1EDIT,
571 SetDlgItemText(hwnd, IDC_PASSPHRASE2EDIT,
573 SetDlgItemText(hwnd, IDC_COMMENTEDIT,
576 * Set the key fingerprint.
579 char *savecomment = state->key.comment;
580 state->key.comment = NULL;
581 rsa_fingerprint(buf, sizeof(buf), &state->key);
582 state->key.comment = savecomment;
584 SetDlgItemText(hwnd, IDC_FINGERPRINT, buf);
586 * Construct a decimal representation
587 * of the key, for pasting into
588 * .ssh/authorized_keys on a Unix box.
590 setupbigedit(hwnd, IDC_KEYDISPLAY, &state->key);
593 * Finally, hide the progress bar and show
596 hidemany(hwnd, nokey_ids, TRUE);
597 hidemany(hwnd, generating_ids, TRUE);
598 hidemany(hwnd, gotkey_ids, FALSE);
599 state->key_exists = TRUE;
607 state = (struct MainDlgState *)GetWindowLong(hwnd, GWL_USERDATA);
608 state->generation_thread_exists = FALSE;
609 state->key_exists = TRUE;
610 SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETPOS, PROGRESSRANGE, 0);
611 EnableWindow(GetDlgItem(hwnd, IDC_GENERATE), 1);
612 EnableWindow(GetDlgItem(hwnd, IDC_LOAD), 1);
613 EnableWindow(GetDlgItem(hwnd, IDC_SAVE), 1);
615 * Invent a comment for the key. We'll do this by including
616 * the date in it. This will be so horrifyingly ugly that
617 * the user will immediately want to change it, which is
620 state->key.comment = malloc(30);
626 strftime(state->key.comment, 30, "rsa-key-%Y%m%d", tm);
630 * Now update the key controls with all the key data.
635 * Blank passphrase, initially. This isn't dangerous,
636 * because we will warn (Are You Sure?) before allowing
637 * the user to save an unprotected private key.
639 SetDlgItemText(hwnd, IDC_PASSPHRASE1EDIT, "");
640 SetDlgItemText(hwnd, IDC_PASSPHRASE2EDIT, "");
644 SetDlgItemText(hwnd, IDC_COMMENTEDIT, state->key.comment);
646 * Set the key fingerprint.
649 char *savecomment = state->key.comment;
650 state->key.comment = NULL;
651 rsa_fingerprint(buf, sizeof(buf), &state->key);
652 state->key.comment = savecomment;
654 SetDlgItemText(hwnd, IDC_FINGERPRINT, buf);
656 * Construct a decimal representation of the key, for
657 * pasting into .ssh/authorized_keys on a Unix box.
659 setupbigedit(hwnd, IDC_KEYDISPLAY, &state->key);
662 * Finally, hide the progress bar and show the key data.
664 hidemany(hwnd, nokey_ids, TRUE);
665 hidemany(hwnd, generating_ids, TRUE);
666 hidemany(hwnd, gotkey_ids, FALSE);
669 state = (struct MainDlgState *)GetWindowLong(hwnd, GWL_USERDATA);
677 int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show) {
678 InitCommonControls();
681 return DialogBox(hinst, MAKEINTRESOURCE(201), NULL, MainDlgProc) != IDOK;