2 * PuTTY key generation front end.
11 #define PUTTY_DO_GLOBALS
17 #define WM_DONEKEY (WM_XUSER + 1)
19 #define DEFAULT_KEYSIZE 1024
21 /* ----------------------------------------------------------------------
22 * Progress report code. This is really horrible :-)
24 #define PHASE1TOTAL 0x10000
25 #define PHASE2TOTAL 0x10000
26 #define PHASE3TOTAL 0x04000
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)
35 unsigned phase1param, phase1current, phase1n;
36 unsigned phase2param, phase2current, phase2n;
41 static void progress_update(void *param, int phase, int iprogress) {
42 struct progress *p = (struct progress *)param;
43 unsigned progress = iprogress;
48 p->phase1param = 0x10000 + progress;
49 p->phase1current = 0x10000; p->phase1n = 0;
52 p->phase2param = 0x10000 + progress;
53 p->phase2current = 0x10000; p->phase2n = 0;
56 p->phase3mult = PHASE3TOTAL / progress;
59 while (p->phase1n < progress) {
61 p->phase1current *= p->phase1param;
62 p->phase1current /= 0x10000;
64 position = PHASE1START + 0x10000 - p->phase1current;
67 while (p->phase2n < progress) {
69 p->phase2current *= p->phase2param;
70 p->phase2current /= 0x10000;
72 position = PHASE2START + 0x10000 - p->phase2current;
75 position = PHASE3START + progress * p->phase3mult;
79 SendMessage(p->progbar, PBM_SETPOS, position / DIVISOR, 0);
84 #define PASSPHRASE_MAXLEN 512
86 struct PassphraseProcStruct {
92 * Dialog-box function for the passphrase box.
94 static int CALLBACK PassphraseProc(HWND hwnd, UINT msg,
95 WPARAM wParam, LPARAM lParam) {
96 static char *passphrase;
97 struct PassphraseProcStruct *p;
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;
107 SetDlgItemText(hwnd, 101, p->comment);
111 switch (LOWORD(wParam)) {
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';
137 * Prompt for a key file. Assumes the filename buffer is of size
140 static int prompt_keyfile(HWND hwnd, char *dlgtitle,
141 char *filename, int save) {
143 memset(&of, 0, sizeof(of));
144 #ifdef OPENFILENAME_SIZE_VERSION_400
145 of.lStructSize = OPENFILENAME_SIZE_VERSION_400;
147 of.lStructSize = sizeof(of);
150 of.lpstrFilter = "All Files\0*\0\0\0";
151 of.lpstrCustomFilter = NULL;
153 of.lpstrFile = filename; *filename = '\0';
154 of.nMaxFile = FILENAME_MAX;
155 of.lpstrFileTitle = NULL;
156 of.lpstrInitialDir = NULL;
157 of.lpstrTitle = dlgtitle;
160 return GetSaveFileName(&of);
162 return GetOpenFileName(&of);
166 * This function is needed to link with the DES code. We need not
167 * have it do anything at all.
169 void logevent(char *msg) {
173 * Dialog-box function for the Licence box.
175 static int CALLBACK LicenceProc (HWND hwnd, UINT msg,
176 WPARAM wParam, LPARAM lParam) {
181 switch (LOWORD(wParam)) {
195 * Dialog-box function for the About box.
197 static int CALLBACK AboutProc (HWND hwnd, UINT msg,
198 WPARAM wParam, LPARAM lParam) {
201 SetDlgItemText (hwnd, 100, ver);
204 switch (LOWORD(wParam)) {
209 EnableWindow(hwnd, 0);
210 DialogBox (hinst, MAKEINTRESOURCE(214), NULL, LicenceProc);
211 EnableWindow(hwnd, 1);
212 SetActiveWindow(hwnd);
224 * Thread to generate a key.
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 */
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;
239 rsa_generate(params->key, params->aux,
240 params->keysize, progress_update, &prog);
242 PostMessage(params->dialog, WM_DONEKEY, 0, 0);
248 struct MainDlgState {
249 int collecting_entropy;
250 int generation_thread_exists;
252 int entropy_got, entropy_required, entropy_size;
259 static void hidemany(HWND hwnd, const int *ids, int hideit) {
261 ShowWindow(GetDlgItem(hwnd, *ids++), (hideit ? SW_HIDE : SW_SHOW));
265 static void setupbigedit(HWND hwnd, int id, struct RSAKey *key) {
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);
283 * Dialog-box function for the main PuTTYgen dialog box.
285 static int CALLBACK MainDlgProc (HWND hwnd, UINT msg,
286 WPARAM wParam, LPARAM lParam) {
288 controlidstart = 100,
290 IDC_BOX_KEY, IDC_BOXT_KEY,
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,
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;
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);
330 struct ctlpos cp, cp2;
332 /* Accelerators used: acglops */
334 ctlposinit(&cp, hwnd, 10, 10, 10);
335 bartitle(&cp, "Public and private key generation for PuTTY",
338 IDC_BOX_KEY, IDC_BOXT_KEY);
340 statictext(&cp2, "No key.", IDC_NOKEY);
344 progressbar(&cp2, IDC_PROGRESS);
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);
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);
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);
374 SetDlgItemInt(hwnd, IDC_BITS, DEFAULT_KEYSIZE, FALSE);
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
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);
389 state = (struct MainDlgState *)GetWindowLong(hwnd, GWL_USERDATA);
390 if (state->collecting_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;
402 * Seed the entropy pool
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;
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);
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;
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);
428 state->generation_thread_exists = TRUE;
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);
450 EnableWindow(hwnd, 0);
451 DialogBox (hinst, MAKEINTRESOURCE(213), NULL, AboutProc);
452 EnableWindow(hwnd, 1);
453 SetActiveWindow(hwnd);
456 state = (struct MainDlgState *)GetWindowLong(hwnd, GWL_USERDATA);
457 if (!state->generation_thread_exists) {
459 state->keysize = GetDlgItemInt(hwnd, IDC_BITS,
461 if (!ok) state->keysize = DEFAULT_KEYSIZE;
462 if (state->keysize < 256) {
463 int ret = MessageBox(hwnd,
464 "PuTTYgen will not generate a key"
465 " smaller than 256 bits.\n"
466 "Key length reset to 256. Continue?",
468 MB_ICONWARNING | MB_OKCANCEL);
471 state->keysize = 256;
472 SetDlgItemInt(hwnd, IDC_BITS, 256, FALSE);
474 hidemany(hwnd, nokey_ids, TRUE);
475 hidemany(hwnd, generating_ids, FALSE);
476 hidemany(hwnd, gotkey_ids, TRUE);
477 EnableWindow(GetDlgItem(hwnd, IDC_GENERATE), 0);
478 EnableWindow(GetDlgItem(hwnd, IDC_LOAD), 0);
479 EnableWindow(GetDlgItem(hwnd, IDC_SAVE), 0);
480 state->key_exists = FALSE;
481 SetDlgItemText(hwnd, IDC_GENERATING, entropy_msg);
482 state->collecting_entropy = TRUE;
485 * My brief statistical tests on mouse movements
486 * suggest that there are about 2.5 bits of
487 * randomness in the x position, 2.5 in the y
488 * position, and 1.7 in the message time, making
489 * 5.7 bits of unpredictability per mouse movement.
490 * However, other people have told me it's far less
491 * than that, so I'm going to be stupidly cautious
492 * and knock that down to a nice round 2. With this
493 * method, we require two words per mouse movement,
494 * so with 2 bits per mouse movement we expect 2
495 * bits every 2 words.
497 state->entropy_required = (state->keysize/2) * 2;
498 state->entropy_got = 0;
499 state->entropy_size = (state->entropy_required *
500 sizeof(*state->entropy));
501 state->entropy = malloc(state->entropy_size);
503 SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETRANGE, 0,
504 MAKELPARAM(0, state->entropy_required));
505 SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETPOS, 0, 0);
509 state = (struct MainDlgState *)GetWindowLong(hwnd, GWL_USERDATA);
510 if (state->key_exists) {
511 char filename[FILENAME_MAX];
512 char passphrase[PASSPHRASE_MAXLEN];
513 char passphrase2[PASSPHRASE_MAXLEN];
514 GetDlgItemText(hwnd, IDC_PASSPHRASE1EDIT,
515 passphrase, sizeof(passphrase));
516 GetDlgItemText(hwnd, IDC_PASSPHRASE2EDIT,
517 passphrase2, sizeof(passphrase2));
518 if (strcmp(passphrase, passphrase2)) {
520 "The two passphrases given do not match.",
522 MB_OK | MB_ICONERROR);
527 ret = MessageBox(hwnd,
528 "Are you sure you want to save this key\n"
529 "without a passphrase to protect it?",
531 MB_YESNO | MB_ICONWARNING);
535 if (prompt_keyfile(hwnd, "Save private key as:",
538 FILE *fp = fopen(filename, "r");
540 char buffer[FILENAME_MAX+80];
542 sprintf(buffer, "Overwrite existing file\n%.*s?",
543 FILENAME_MAX, filename);
544 ret = MessageBox(hwnd, buffer, "PuTTYgen Warning",
545 MB_YESNO | MB_ICONWARNING);
549 ret = saversakey(filename, &state->key, &state->aux,
550 *passphrase ? passphrase : NULL);
552 MessageBox(hwnd, "Unable to save key file",
554 MB_OK | MB_ICONERROR);
560 state = (struct MainDlgState *)GetWindowLong(hwnd, GWL_USERDATA);
561 if (!state->generation_thread_exists) {
562 char filename[FILENAME_MAX];
563 if (prompt_keyfile(hwnd, "Load private key:",
565 char passphrase[PASSPHRASE_MAXLEN];
569 struct PassphraseProcStruct pps;
570 struct RSAKey newkey;
571 struct RSAAux newaux;
573 needs_pass = rsakey_encrypted(filename, &comment);
574 pps.passphrase = passphrase;
575 pps.comment = comment;
579 dlgret = DialogBoxParam(hinst,
580 MAKEINTRESOURCE(210),
581 NULL, PassphraseProc,
589 ret = loadrsakey(filename, &newkey, &newaux,
592 if (comment) free(comment);
594 MessageBox(NULL, "Couldn't load private key.",
595 "PuTTYgen Error", MB_OK | MB_ICONERROR);
596 } else if (ret == 1) {
600 EnableWindow(GetDlgItem(hwnd, IDC_GENERATE), 1);
601 EnableWindow(GetDlgItem(hwnd, IDC_LOAD), 1);
602 EnableWindow(GetDlgItem(hwnd, IDC_SAVE), 1);
604 * Now update the key controls with all the
609 SetDlgItemText(hwnd, IDC_PASSPHRASE1EDIT,
611 SetDlgItemText(hwnd, IDC_PASSPHRASE2EDIT,
613 SetDlgItemText(hwnd, IDC_COMMENTEDIT,
616 * Set the key fingerprint.
619 char *savecomment = state->key.comment;
620 state->key.comment = NULL;
621 rsa_fingerprint(buf, sizeof(buf), &state->key);
622 state->key.comment = savecomment;
624 SetDlgItemText(hwnd, IDC_FINGERPRINT, buf);
626 * Construct a decimal representation
627 * of the key, for pasting into
628 * .ssh/authorized_keys on a Unix box.
630 setupbigedit(hwnd, IDC_KEYDISPLAY, &state->key);
633 * Finally, hide the progress bar and show
636 hidemany(hwnd, nokey_ids, TRUE);
637 hidemany(hwnd, generating_ids, TRUE);
638 hidemany(hwnd, gotkey_ids, FALSE);
639 state->key_exists = TRUE;
647 state = (struct MainDlgState *)GetWindowLong(hwnd, GWL_USERDATA);
648 state->generation_thread_exists = FALSE;
649 state->key_exists = TRUE;
650 SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETPOS, PROGRESSRANGE, 0);
651 EnableWindow(GetDlgItem(hwnd, IDC_GENERATE), 1);
652 EnableWindow(GetDlgItem(hwnd, IDC_LOAD), 1);
653 EnableWindow(GetDlgItem(hwnd, IDC_SAVE), 1);
655 * Invent a comment for the key. We'll do this by including
656 * the date in it. This will be so horrifyingly ugly that
657 * the user will immediately want to change it, which is
660 state->key.comment = malloc(30);
666 strftime(state->key.comment, 30, "rsa-key-%Y%m%d", tm);
670 * Now update the key controls with all the key data.
675 * Blank passphrase, initially. This isn't dangerous,
676 * because we will warn (Are You Sure?) before allowing
677 * the user to save an unprotected private key.
679 SetDlgItemText(hwnd, IDC_PASSPHRASE1EDIT, "");
680 SetDlgItemText(hwnd, IDC_PASSPHRASE2EDIT, "");
684 SetDlgItemText(hwnd, IDC_COMMENTEDIT, state->key.comment);
686 * Set the key fingerprint.
689 char *savecomment = state->key.comment;
690 state->key.comment = NULL;
691 rsa_fingerprint(buf, sizeof(buf), &state->key);
692 state->key.comment = savecomment;
694 SetDlgItemText(hwnd, IDC_FINGERPRINT, buf);
696 * Construct a decimal representation of the key, for
697 * pasting into .ssh/authorized_keys on a Unix box.
699 setupbigedit(hwnd, IDC_KEYDISPLAY, &state->key);
702 * Finally, hide the progress bar and show the key data.
704 hidemany(hwnd, nokey_ids, TRUE);
705 hidemany(hwnd, generating_ids, TRUE);
706 hidemany(hwnd, gotkey_ids, FALSE);
709 state = (struct MainDlgState *)GetWindowLong(hwnd, GWL_USERDATA);
717 int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show) {
718 InitCommonControls();
721 return DialogBox(hinst, MAKEINTRESOURCE(201), NULL, MainDlgProc) != IDOK;