2 * PuTTY key generation front end.
13 #define PUTTY_DO_GLOBALS
19 #define WM_DONEKEY (WM_XUSER + 1)
21 #define DEFAULT_KEYSIZE 1024
25 * - test the generated keys for actual working-RSA-key-hood
28 /* ----------------------------------------------------------------------
29 * Progress report code. This is really horrible :-)
31 #define PHASE1TOTAL 0x10000
32 #define PHASE2TOTAL 0x10000
33 #define PHASE3TOTAL 0x04000
35 #define PHASE2START (PHASE1TOTAL)
36 #define PHASE3START (PHASE1TOTAL + PHASE2TOTAL)
37 #define TOTALTOTAL (PHASE1TOTAL + PHASE2TOTAL + PHASE3TOTAL)
38 #define PROGRESSBIGRANGE 65535
39 #define DIVISOR ((TOTALTOTAL + PROGRESSBIGRANGE - 1) / PROGRESSBIGRANGE)
40 #define PROGRESSRANGE (TOTALTOTAL / DIVISOR)
42 unsigned phase1param, phase1current, phase1n;
43 unsigned phase2param, phase2current, phase2n;
48 static void progress_update(void *param, int phase, int iprogress) {
49 struct progress *p = (struct progress *)param;
50 unsigned progress = iprogress;
55 p->phase1param = 0x10000 + progress;
56 p->phase1current = 0x10000; p->phase1n = 0;
59 p->phase2param = 0x10000 + progress;
60 p->phase2current = 0x10000; p->phase2n = 0;
63 p->phase3mult = PHASE3TOTAL / progress;
66 while (p->phase1n < progress) {
68 p->phase1current *= p->phase1param;
69 p->phase1current /= 0x10000;
71 position = PHASE1START + 0x10000 - p->phase1current;
74 while (p->phase2n < progress) {
76 p->phase2current *= p->phase2param;
77 p->phase2current /= 0x10000;
79 position = PHASE2START + 0x10000 - p->phase2current;
82 position = PHASE3START + progress * p->phase3mult;
86 SendMessage(p->progbar, PBM_SETPOS, position / DIVISOR, 0);
91 #define PASSPHRASE_MAXLEN 512
93 struct PassphraseProcStruct {
99 * Dialog-box function for the passphrase box.
101 static int CALLBACK PassphraseProc(HWND hwnd, UINT msg,
102 WPARAM wParam, LPARAM lParam) {
103 static char *passphrase;
104 struct PassphraseProcStruct *p;
108 SetForegroundWindow(hwnd);
109 SetWindowPos (hwnd, HWND_TOP, 0, 0, 0, 0,
110 SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
111 p = (struct PassphraseProcStruct *)lParam;
112 passphrase = p->passphrase;
114 SetDlgItemText(hwnd, 101, p->comment);
118 switch (LOWORD(wParam)) {
128 case 102: /* edit box */
129 if (HIWORD(wParam) == EN_CHANGE) {
130 GetDlgItemText (hwnd, 102, passphrase, PASSPHRASE_MAXLEN-1);
131 passphrase[PASSPHRASE_MAXLEN-1] = '\0';
144 * Prompt for a key file. Assumes the filename buffer is of size
147 static int prompt_keyfile(HWND hwnd, char *dlgtitle,
148 char *filename, int save) {
150 memset(&of, 0, sizeof(of));
151 #ifdef OPENFILENAME_SIZE_VERSION_400
152 of.lStructSize = OPENFILENAME_SIZE_VERSION_400;
154 of.lStructSize = sizeof(of);
157 of.lpstrFilter = "All Files\0*\0\0\0";
158 of.lpstrCustomFilter = NULL;
160 of.lpstrFile = filename; *filename = '\0';
161 of.nMaxFile = FILENAME_MAX;
162 of.lpstrFileTitle = NULL;
163 of.lpstrInitialDir = NULL;
164 of.lpstrTitle = dlgtitle;
167 return GetSaveFileName(&of);
169 return GetOpenFileName(&of);
173 * This function is needed to link with the DES code. We need not
174 * have it do anything at all.
176 void logevent(char *msg) {
180 * Dialog-box function for the Licence box.
182 static int CALLBACK LicenceProc (HWND hwnd, UINT msg,
183 WPARAM wParam, LPARAM lParam) {
188 switch (LOWORD(wParam)) {
202 * Dialog-box function for the About box.
204 static int CALLBACK AboutProc (HWND hwnd, UINT msg,
205 WPARAM wParam, LPARAM lParam) {
208 SetDlgItemText (hwnd, 100, ver);
211 switch (LOWORD(wParam)) {
216 EnableWindow(hwnd, 0);
217 DialogBox (hinst, MAKEINTRESOURCE(214), NULL, LicenceProc);
218 EnableWindow(hwnd, 1);
219 SetActiveWindow(hwnd);
231 * Thread to generate a key.
233 struct rsa_key_thread_params {
234 HWND progressbar; /* notify this with progress */
235 HWND dialog; /* notify this on completion */
236 int keysize; /* bits in key */
240 static DWORD WINAPI generate_rsa_key_thread(void *param) {
241 struct rsa_key_thread_params *params =
242 (struct rsa_key_thread_params *)param;
243 struct progress prog;
244 prog.progbar = params->progressbar;
246 rsa_generate(params->key, params->aux,
247 params->keysize, progress_update, &prog);
249 PostMessage(params->dialog, WM_DONEKEY, 0, 0);
255 struct MainDlgState {
256 int collecting_entropy;
257 int generation_thread_exists;
259 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,
310 IDC_BOX_PARAMS, IDC_BOXT_PARAMS,
311 IDC_BITSSTATIC, IDC_BITS,
314 static const int nokey_ids[] = { IDC_NOKEY, 0 };
315 static const int generating_ids[] = { IDC_GENERATING, IDC_PROGRESS, 0 };
316 static const int gotkey_ids[] = {
317 IDC_PKSTATIC, IDC_KEYDISPLAY,
318 IDC_FPSTATIC, IDC_FINGERPRINT,
319 IDC_COMMENTSTATIC, IDC_COMMENTEDIT,
320 IDC_PASSPHRASE1STATIC, IDC_PASSPHRASE1EDIT,
321 IDC_PASSPHRASE2STATIC, IDC_PASSPHRASE2EDIT, 0 };
322 static const char generating_msg[] =
323 "Please wait while a key is generated...";
324 static const char entropy_msg[] =
325 "Please generate some randomness by moving the mouse over the blank area.";
326 struct MainDlgState *state;
330 state = malloc(sizeof(*state));
331 state->generation_thread_exists = FALSE;
332 state->key_exists = FALSE;
333 SetWindowLong(hwnd, GWL_USERDATA, (LONG)state);
335 struct ctlpos cp, cp2;
337 /* Accelerators used: acglops */
339 ctlposinit(&cp, hwnd, 10, 10, 10);
340 bartitle(&cp, "Public and private key generation for PuTTY",
343 IDC_BOX_KEY, IDC_BOXT_KEY);
345 statictext(&cp2, "No key.", IDC_NOKEY);
349 progressbar(&cp2, IDC_PROGRESS);
351 "&Public key for pasting into authorized_keys file:",
352 IDC_PKSTATIC, IDC_KEYDISPLAY, 7);
353 SendDlgItemMessage(hwnd, IDC_KEYDISPLAY, EM_SETREADONLY, 1, 0);
354 staticedit(&cp, "Key fingerprint:", IDC_FPSTATIC,
355 IDC_FINGERPRINT, 70);
356 SendDlgItemMessage(hwnd, IDC_FINGERPRINT, EM_SETREADONLY, 1, 0);
357 staticedit(&cp, "Key &comment:", IDC_COMMENTSTATIC,
358 IDC_COMMENTEDIT, 70);
359 staticpassedit(&cp, "Key p&assphrase:", IDC_PASSPHRASE1STATIC,
360 IDC_PASSPHRASE1EDIT, 70);
361 staticpassedit(&cp, "C&onfirm passphrase:", IDC_PASSPHRASE2STATIC,
362 IDC_PASSPHRASE2EDIT, 70);
364 beginbox(&cp, "Actions",
365 IDC_BOX_ACTIONS, IDC_BOXT_ACTIONS);
366 staticbtn(&cp, "Generate a public/private key pair",
367 IDC_GENSTATIC, "&Generate", IDC_GENERATE);
368 staticbtn(&cp, "Load an existing private key file",
369 IDC_LOADSTATIC, "&Load", IDC_LOAD);
370 staticbtn(&cp, "Save the generated key to a new file",
371 IDC_SAVESTATIC, "&Save", IDC_SAVE);
373 beginbox(&cp, "Actions",
374 IDC_BOX_ACTIONS, IDC_BOXT_ACTIONS);
375 staticedit(&cp, "Number of &bits in a generated key:",
376 IDC_BITSSTATIC, IDC_BITS, 20);
379 SetDlgItemInt(hwnd, IDC_BITS, DEFAULT_KEYSIZE, FALSE);
382 * Initially, hide the progress bar and the key display,
383 * and show the no-key display. Also disable the Save
384 * button, because with no key we obviously can't save
387 hidemany(hwnd, nokey_ids, FALSE);
388 hidemany(hwnd, generating_ids, TRUE);
389 hidemany(hwnd, gotkey_ids, TRUE);
390 EnableWindow(GetDlgItem(hwnd, IDC_SAVE), 0);
394 state = (struct MainDlgState *)GetWindowLong(hwnd, GWL_USERDATA);
395 if (state->collecting_entropy) {
396 state->entropy[state->entropy_got++] = lParam;
397 state->entropy[state->entropy_got++] = GetMessageTime();
398 SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETPOS,
399 state->entropy_got, 0);
400 if (state->entropy_got >= state->entropy_required) {
401 struct rsa_key_thread_params *params;
405 * Seed the entropy pool
407 random_add_heavynoise(state->entropy, state->entropy_size);
408 memset(state->entropy, 0, state->entropy_size);
409 free(state->entropy);
411 SetDlgItemText(hwnd, IDC_GENERATING, generating_msg);
412 SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETRANGE, 0,
413 MAKELPARAM(0, PROGRESSRANGE));
414 SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETPOS, 0, 0);
416 params = malloc(sizeof(*params));
417 params->progressbar = GetDlgItem(hwnd, IDC_PROGRESS);
418 params->dialog = hwnd;
419 params->keysize = state->keysize;
420 params->key = &state->key;
421 params->aux = &state->aux;
423 if (!CreateThread(NULL, 0, generate_rsa_key_thread,
424 params, 0, &threadid)) {
425 MessageBox(hwnd, "Out of thread resources",
426 "Key generation error",
427 MB_OK | MB_ICONERROR);
430 state->generation_thread_exists = TRUE;
431 state->collecting_entropy = FALSE;
437 switch (LOWORD(wParam)) {
438 case IDC_COMMENTEDIT:
439 if (HIWORD(wParam) == EN_CHANGE) {
440 state = (struct MainDlgState *)
441 GetWindowLong(hwnd, GWL_USERDATA);
442 if (state->key_exists) {
443 HWND editctl = GetDlgItem(hwnd, IDC_COMMENTEDIT);
444 int len = GetWindowTextLength(editctl);
445 if (state->key.comment)
446 free(state->key.comment);
447 state->key.comment = malloc(len+1);
448 GetWindowText(editctl, state->key.comment, len+1);
453 EnableWindow(hwnd, 0);
454 DialogBox (hinst, MAKEINTRESOURCE(213), NULL, AboutProc);
455 EnableWindow(hwnd, 1);
456 SetActiveWindow(hwnd);
459 state = (struct MainDlgState *)GetWindowLong(hwnd, GWL_USERDATA);
460 if (!state->generation_thread_exists) {
461 hidemany(hwnd, nokey_ids, TRUE);
462 hidemany(hwnd, generating_ids, FALSE);
463 hidemany(hwnd, gotkey_ids, TRUE);
464 EnableWindow(GetDlgItem(hwnd, IDC_GENERATE), 0);
465 EnableWindow(GetDlgItem(hwnd, IDC_LOAD), 0);
466 EnableWindow(GetDlgItem(hwnd, IDC_SAVE), 0);
467 state->key_exists = FALSE;
468 SetDlgItemText(hwnd, IDC_GENERATING, entropy_msg);
469 state->collecting_entropy = TRUE;
472 state->keysize = GetDlgItemInt(hwnd, IDC_BITS,
474 if (!ok) state->keysize = DEFAULT_KEYSIZE;
478 * My brief statistical tests on mouse movements
479 * suggest that there are about 2.5 bits of
480 * randomness in the x position, 2.5 in the y
481 * position, and 1.7 in the message time, making
482 * 5.7 bits of unpredictability per mouse movement.
483 * However, other people have told me it's far less
484 * than that, so I'm going to be stupidly cautious
485 * and knock that down to a nice round 2. With this
486 * method, we require two words per mouse movement,
487 * so with 2 bits per mouse movement we expect 2
488 * bits every 2 words.
490 state->entropy_required = (state->keysize/2) * 2;
491 state->entropy_got = 0;
492 state->entropy_size = (state->entropy_required *
493 sizeof(*state->entropy));
494 state->entropy = malloc(state->entropy_size);
496 SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETRANGE, 0,
497 MAKELPARAM(0, state->entropy_required));
498 SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETPOS, 0, 0);
502 state = (struct MainDlgState *)GetWindowLong(hwnd, GWL_USERDATA);
503 if (state->key_exists) {
504 char filename[FILENAME_MAX];
505 char passphrase[PASSPHRASE_MAXLEN];
506 char passphrase2[PASSPHRASE_MAXLEN];
507 GetDlgItemText(hwnd, IDC_PASSPHRASE1EDIT,
508 passphrase, sizeof(passphrase));
509 GetDlgItemText(hwnd, IDC_PASSPHRASE2EDIT,
510 passphrase2, sizeof(passphrase2));
511 if (strcmp(passphrase, passphrase2)) {
513 "The two passphrases given do not match.",
515 MB_OK | MB_ICONERROR);
520 ret = MessageBox(hwnd,
521 "Are you sure you want to save this key\n"
522 "without a passphrase to protect it?",
524 MB_YESNO | MB_ICONWARNING);
528 if (prompt_keyfile(hwnd, "Save private key as:",
531 FILE *fp = fopen(filename, "r");
533 char buffer[FILENAME_MAX+80];
535 sprintf(buffer, "Overwrite existing file\n%.*s?",
536 FILENAME_MAX, filename);
537 ret = MessageBox(hwnd, buffer, "PuTTYgen Warning",
538 MB_YESNO | MB_ICONWARNING);
542 ret = saversakey(filename, &state->key, &state->aux,
543 *passphrase ? passphrase : NULL);
545 MessageBox(hwnd, "Unable to save key file",
547 MB_OK | MB_ICONERROR);
553 state = (struct MainDlgState *)GetWindowLong(hwnd, GWL_USERDATA);
554 if (!state->generation_thread_exists) {
555 char filename[FILENAME_MAX];
556 if (prompt_keyfile(hwnd, "Load private key:",
558 char passphrase[PASSPHRASE_MAXLEN];
562 struct PassphraseProcStruct pps;
563 struct RSAKey newkey;
564 struct RSAAux newaux;
566 needs_pass = rsakey_encrypted(filename, &comment);
567 pps.passphrase = passphrase;
568 pps.comment = comment;
572 dlgret = DialogBoxParam(hinst,
573 MAKEINTRESOURCE(210),
574 NULL, PassphraseProc,
582 ret = loadrsakey(filename, &newkey, &newaux,
585 if (comment) free(comment);
587 MessageBox(NULL, "Couldn't load private key.",
588 "PuTTYgen Error", MB_OK | MB_ICONERROR);
589 } else if (ret == 1) {
593 EnableWindow(GetDlgItem(hwnd, IDC_GENERATE), 1);
594 EnableWindow(GetDlgItem(hwnd, IDC_LOAD), 1);
595 EnableWindow(GetDlgItem(hwnd, IDC_SAVE), 1);
597 * Now update the key controls with all the
602 SetDlgItemText(hwnd, IDC_PASSPHRASE1EDIT,
604 SetDlgItemText(hwnd, IDC_PASSPHRASE2EDIT,
606 SetDlgItemText(hwnd, IDC_COMMENTEDIT,
609 * Set the key fingerprint.
612 char *savecomment = state->key.comment;
613 state->key.comment = NULL;
614 rsa_fingerprint(buf, sizeof(buf), &state->key);
615 state->key.comment = savecomment;
617 SetDlgItemText(hwnd, IDC_FINGERPRINT, buf);
619 * Construct a decimal representation
620 * of the key, for pasting into
621 * .ssh/authorized_keys on a Unix box.
623 setupbigedit(hwnd, IDC_KEYDISPLAY, &state->key);
626 * Finally, hide the progress bar and show
629 hidemany(hwnd, nokey_ids, TRUE);
630 hidemany(hwnd, generating_ids, TRUE);
631 hidemany(hwnd, gotkey_ids, FALSE);
632 state->key_exists = TRUE;
640 state = (struct MainDlgState *)GetWindowLong(hwnd, GWL_USERDATA);
641 state->generation_thread_exists = FALSE;
642 state->key_exists = TRUE;
643 SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETPOS, PROGRESSRANGE, 0);
644 EnableWindow(GetDlgItem(hwnd, IDC_GENERATE), 1);
645 EnableWindow(GetDlgItem(hwnd, IDC_LOAD), 1);
646 EnableWindow(GetDlgItem(hwnd, IDC_SAVE), 1);
648 * Invent a comment for the key. We'll do this by including
649 * the date in it. This will be so horrifyingly ugly that
650 * the user will immediately want to change it, which is
653 state->key.comment = malloc(30);
659 strftime(state->key.comment, 30, "rsa-key-%Y%m%d", tm);
663 * Now update the key controls with all the key data.
668 * Blank passphrase, initially. This isn't dangerous,
669 * because we will warn (Are You Sure?) before allowing
670 * the user to save an unprotected private key.
672 SetDlgItemText(hwnd, IDC_PASSPHRASE1EDIT, "");
673 SetDlgItemText(hwnd, IDC_PASSPHRASE2EDIT, "");
677 SetDlgItemText(hwnd, IDC_COMMENTEDIT, state->key.comment);
679 * Set the key fingerprint.
682 char *savecomment = state->key.comment;
683 state->key.comment = NULL;
684 rsa_fingerprint(buf, sizeof(buf), &state->key);
685 state->key.comment = savecomment;
687 SetDlgItemText(hwnd, IDC_FINGERPRINT, buf);
689 * Construct a decimal representation of the key, for
690 * pasting into .ssh/authorized_keys on a Unix box.
692 setupbigedit(hwnd, IDC_KEYDISPLAY, &state->key);
695 * Finally, hide the progress bar and show the key data.
697 hidemany(hwnd, nokey_ids, TRUE);
698 hidemany(hwnd, generating_ids, TRUE);
699 hidemany(hwnd, gotkey_ids, FALSE);
702 state = (struct MainDlgState *)GetWindowLong(hwnd, GWL_USERDATA);
710 int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show) {
711 InitCommonControls();
714 return DialogBox(hinst, MAKEINTRESOURCE(201), NULL, MainDlgProc) != IDOK;