2 * PuTTY key generation front end.
13 #define PUTTY_DO_GLOBALS
19 #define WM_DONEKEY (WM_XUSER + 1)
25 * - test the generated keys for actual working-RSA-key-hood
29 /* ----------------------------------------------------------------------
30 * Progress report code. This is really horrible :-)
32 #define PHASE1TOTAL 0x10000
33 #define PHASE2TOTAL 0x10000
34 #define PHASE3TOTAL 0x04000
36 #define PHASE2START (PHASE1TOTAL)
37 #define PHASE3START (PHASE1TOTAL + PHASE2TOTAL)
38 #define TOTALTOTAL (PHASE1TOTAL + PHASE2TOTAL + PHASE3TOTAL)
39 #define PROGRESSBIGRANGE 65535
40 #define DIVISOR ((TOTALTOTAL + PROGRESSBIGRANGE - 1) / PROGRESSBIGRANGE)
41 #define PROGRESSRANGE (TOTALTOTAL / DIVISOR)
43 unsigned phase1param, phase1current, phase1n;
44 unsigned phase2param, phase2current, phase2n;
49 static void progress_update(void *param, int phase, int iprogress) {
50 struct progress *p = (struct progress *)param;
51 unsigned progress = iprogress;
56 p->phase1param = 0x10000 + progress;
57 p->phase1current = 0x10000; p->phase1n = 0;
60 p->phase2param = 0x10000 + progress;
61 p->phase2current = 0x10000; p->phase2n = 0;
64 p->phase3mult = PHASE3TOTAL / progress;
67 while (p->phase1n < progress) {
69 p->phase1current *= p->phase1param;
70 p->phase1current /= 0x10000;
72 position = PHASE1START + 0x10000 - p->phase1current;
75 while (p->phase2n < progress) {
77 p->phase2current *= p->phase2param;
78 p->phase2current /= 0x10000;
80 position = PHASE2START + 0x10000 - p->phase2current;
83 position = PHASE3START + progress * p->phase3mult;
87 SendMessage(p->progbar, PBM_SETPOS, position / DIVISOR, 0);
92 #define PASSPHRASE_MAXLEN 512
94 struct PassphraseProcStruct {
100 * Dialog-box function for the passphrase box.
102 static int CALLBACK PassphraseProc(HWND hwnd, UINT msg,
103 WPARAM wParam, LPARAM lParam) {
104 static char *passphrase;
105 struct PassphraseProcStruct *p;
109 SetForegroundWindow(hwnd);
110 SetWindowPos (hwnd, HWND_TOP, 0, 0, 0, 0,
111 SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
112 p = (struct PassphraseProcStruct *)lParam;
113 passphrase = p->passphrase;
115 SetDlgItemText(hwnd, 101, p->comment);
119 switch (LOWORD(wParam)) {
129 case 102: /* edit box */
130 if (HIWORD(wParam) == EN_CHANGE) {
131 GetDlgItemText (hwnd, 102, passphrase, PASSPHRASE_MAXLEN-1);
132 passphrase[PASSPHRASE_MAXLEN-1] = '\0';
145 * Prompt for a key file. Assumes the filename buffer is of size
148 static int prompt_keyfile(HWND hwnd, char *dlgtitle,
149 char *filename, int save) {
151 memset(&of, 0, sizeof(of));
152 #ifdef OPENFILENAME_SIZE_VERSION_400
153 of.lStructSize = OPENFILENAME_SIZE_VERSION_400;
155 of.lStructSize = sizeof(of);
158 of.lpstrFilter = "All Files\0*\0\0\0";
159 of.lpstrCustomFilter = NULL;
161 of.lpstrFile = filename; *filename = '\0';
162 of.nMaxFile = FILENAME_MAX;
163 of.lpstrFileTitle = NULL;
164 of.lpstrInitialDir = NULL;
165 of.lpstrTitle = dlgtitle;
168 return GetSaveFileName(&of);
170 return GetOpenFileName(&of);
174 * This function is needed to link with the DES code. We need not
175 * have it do anything at all.
177 void logevent(char *msg) {
181 * Dialog-box function for the Licence box.
183 static int CALLBACK LicenceProc (HWND hwnd, UINT msg,
184 WPARAM wParam, LPARAM lParam) {
189 switch (LOWORD(wParam)) {
203 * Dialog-box function for the About box.
205 static int CALLBACK AboutProc (HWND hwnd, UINT msg,
206 WPARAM wParam, LPARAM lParam) {
209 SetDlgItemText (hwnd, 100, ver);
212 switch (LOWORD(wParam)) {
217 EnableWindow(hwnd, 0);
218 DialogBox (hinst, MAKEINTRESOURCE(214), NULL, LicenceProc);
219 EnableWindow(hwnd, 1);
220 SetActiveWindow(hwnd);
232 * Thread to generate a key.
234 struct rsa_key_thread_params {
235 HWND progressbar; /* notify this with progress */
236 HWND dialog; /* notify this on completion */
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, KEYSIZE, progress_update, &prog);
248 PostMessage(params->dialog, WM_DONEKEY, 0, 0);
254 struct MainDlgState {
255 int collecting_entropy;
256 int generation_thread_exists;
258 int entropy_got, entropy_required, entropy_size;
264 static void hidemany(HWND hwnd, const int *ids, int hideit) {
266 ShowWindow(GetDlgItem(hwnd, *ids++), (hideit ? SW_HIDE : SW_SHOW));
270 static void setupbigedit(HWND hwnd, int id, struct RSAKey *key) {
274 dec1 = bignum_decimal(key->exponent);
275 dec2 = bignum_decimal(key->modulus);
276 buffer = malloc(strlen(dec1)+strlen(dec2)+
277 strlen(key->comment)+30);
278 sprintf(buffer, "%d %s %s %s",
279 ssh1_bignum_bitcount(key->modulus),
280 dec1, dec2, key->comment);
281 SetDlgItemText(hwnd, id, buffer);
288 * Dialog-box function for the main PuTTYgen dialog box.
290 static int CALLBACK MainDlgProc (HWND hwnd, UINT msg,
291 WPARAM wParam, LPARAM lParam) {
293 controlidstart = 100,
295 IDC_BOX_KEY, IDC_BOXT_KEY,
299 IDC_PKSTATIC, IDC_KEYDISPLAY,
300 IDC_FPSTATIC, IDC_FINGERPRINT,
301 IDC_COMMENTSTATIC, IDC_COMMENTEDIT,
302 IDC_PASSPHRASE1STATIC, IDC_PASSPHRASE1EDIT,
303 IDC_PASSPHRASE2STATIC, IDC_PASSPHRASE2EDIT,
304 IDC_BOX_ACTIONS, IDC_BOXT_ACTIONS,
305 IDC_GENSTATIC, IDC_GENERATE,
306 IDC_LOADSTATIC, IDC_LOAD,
307 IDC_SAVESTATIC, IDC_SAVE,
310 static const int nokey_ids[] = { IDC_NOKEY, 0 };
311 static const int generating_ids[] = { IDC_GENERATING, IDC_PROGRESS, 0 };
312 static const int gotkey_ids[] = {
313 IDC_PKSTATIC, IDC_KEYDISPLAY,
314 IDC_FPSTATIC, IDC_FINGERPRINT,
315 IDC_COMMENTSTATIC, IDC_COMMENTEDIT,
316 IDC_PASSPHRASE1STATIC, IDC_PASSPHRASE1EDIT,
317 IDC_PASSPHRASE2STATIC, IDC_PASSPHRASE2EDIT, 0 };
318 static const char generating_msg[] =
319 "Please wait while a key is generated...";
320 static const char entropy_msg[] =
321 "Please move the mouse in this window to generate randomness";
322 struct MainDlgState *state;
326 state = malloc(sizeof(*state));
327 state->generation_thread_exists = FALSE;
328 state->key_exists = FALSE;
329 SetWindowLong(hwnd, GWL_USERDATA, (LONG)state);
331 struct ctlpos cp, cp2;
333 /* Accelerators used: acglops */
335 ctlposinit(&cp, hwnd, 10, 10, 10);
336 bartitle(&cp, "Public and private key generation for PuTTY",
339 IDC_BOX_KEY, IDC_BOXT_KEY);
341 statictext(&cp2, "No key.", IDC_NOKEY);
345 progressbar(&cp2, IDC_PROGRESS);
347 "&Public key for pasting into authorized_keys file:",
348 IDC_PKSTATIC, IDC_KEYDISPLAY, 7);
349 SendDlgItemMessage(hwnd, IDC_KEYDISPLAY, EM_SETREADONLY, 1, 0);
350 staticedit(&cp, "Key fingerprint:", IDC_FPSTATIC,
351 IDC_FINGERPRINT, 70);
352 SendDlgItemMessage(hwnd, IDC_FINGERPRINT, EM_SETREADONLY, 1, 0);
353 staticedit(&cp, "Key &comment:", IDC_COMMENTSTATIC,
354 IDC_COMMENTEDIT, 70);
355 staticpassedit(&cp, "Key p&assphrase:", IDC_PASSPHRASE1STATIC,
356 IDC_PASSPHRASE1EDIT, 70);
357 staticpassedit(&cp, "C&onfirm passphrase:", IDC_PASSPHRASE2STATIC,
358 IDC_PASSPHRASE2EDIT, 70);
360 beginbox(&cp, "Actions",
361 IDC_BOX_ACTIONS, IDC_BOXT_ACTIONS);
362 staticbtn(&cp, "Generate a public/private key pair",
363 IDC_GENSTATIC, "&Generate", IDC_GENERATE);
364 staticbtn(&cp, "Load an existing private key file",
365 IDC_LOADSTATIC, "&Load", IDC_LOAD);
366 staticbtn(&cp, "Save the generated key to a new file",
367 IDC_SAVESTATIC, "&Save", IDC_SAVE);
371 * Initially, hide the progress bar and the key display,
372 * and show the no-key display. Also disable the Save
373 * button, because with no key we obviously can't save
376 hidemany(hwnd, nokey_ids, FALSE);
377 hidemany(hwnd, generating_ids, TRUE);
378 hidemany(hwnd, gotkey_ids, TRUE);
379 EnableWindow(GetDlgItem(hwnd, IDC_SAVE), 0);
383 state = (struct MainDlgState *)GetWindowLong(hwnd, GWL_USERDATA);
384 if (state->collecting_entropy) {
385 state->entropy[state->entropy_got++] = lParam;
386 state->entropy[state->entropy_got++] = GetMessageTime();
387 SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETPOS,
388 state->entropy_got, 0);
389 if (state->entropy_got >= state->entropy_required) {
390 struct rsa_key_thread_params *params;
394 * Seed the entropy pool
396 random_add_heavynoise(state->entropy, state->entropy_size);
397 memset(state->entropy, 0, state->entropy_size);
398 free(state->entropy);
400 SetDlgItemText(hwnd, IDC_GENERATING, generating_msg);
401 SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETRANGE, 0,
402 MAKELPARAM(0, PROGRESSRANGE));
403 SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETPOS, 0, 0);
405 params = malloc(sizeof(*params));
406 params->progressbar = GetDlgItem(hwnd, IDC_PROGRESS);
407 params->dialog = hwnd;
408 params->key = &state->key;
409 params->aux = &state->aux;
411 if (!CreateThread(NULL, 0, generate_rsa_key_thread,
412 params, 0, &threadid)) {
413 MessageBox(hwnd, "Out of thread resources",
414 "Key generation error",
415 MB_OK | MB_ICONERROR);
418 state->generation_thread_exists = TRUE;
419 state->collecting_entropy = FALSE;
425 switch (LOWORD(wParam)) {
426 case IDC_COMMENTEDIT:
427 if (HIWORD(wParam) == EN_CHANGE) {
428 state = (struct MainDlgState *)
429 GetWindowLong(hwnd, GWL_USERDATA);
430 if (state->key_exists) {
431 HWND editctl = GetDlgItem(hwnd, IDC_COMMENTEDIT);
432 int len = GetWindowTextLength(editctl);
433 if (state->key.comment)
434 free(state->key.comment);
435 state->key.comment = malloc(len+1);
436 GetWindowText(editctl, state->key.comment, len+1);
441 EnableWindow(hwnd, 0);
442 DialogBox (hinst, MAKEINTRESOURCE(213), NULL, AboutProc);
443 EnableWindow(hwnd, 1);
444 SetActiveWindow(hwnd);
447 state = (struct MainDlgState *)GetWindowLong(hwnd, GWL_USERDATA);
448 if (!state->generation_thread_exists) {
449 hidemany(hwnd, nokey_ids, TRUE);
450 hidemany(hwnd, generating_ids, FALSE);
451 hidemany(hwnd, gotkey_ids, TRUE);
452 EnableWindow(GetDlgItem(hwnd, IDC_GENERATE), 0);
453 EnableWindow(GetDlgItem(hwnd, IDC_LOAD), 0);
454 EnableWindow(GetDlgItem(hwnd, IDC_SAVE), 0);
455 state->key_exists = FALSE;
456 SetDlgItemText(hwnd, IDC_GENERATING, entropy_msg);
457 state->collecting_entropy = TRUE;
460 * My brief statistical tests on mouse movements
461 * suggest that there are about 5 bits of
462 * randomness in the x position, 5 in the y
463 * position, and 1.7 in the message time, making
464 * 11.7 bits of unpredictability per mouse
465 * movement. However, other people have told me
466 * it's far less than that, so I'm going to be
467 * stupidly cautious and knock that down to a nice
470 state->entropy_required = (KEYSIZE / 4) * 2;
471 state->entropy_got = 0;
472 state->entropy_size = (state->entropy_required *
473 sizeof(*state->entropy));
474 state->entropy = malloc(state->entropy_size);
476 SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETRANGE, 0,
477 MAKELPARAM(0, state->entropy_required));
478 SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETPOS, 0, 0);
482 state = (struct MainDlgState *)GetWindowLong(hwnd, GWL_USERDATA);
483 if (state->key_exists) {
484 char filename[FILENAME_MAX];
485 char passphrase[PASSPHRASE_MAXLEN];
486 char passphrase2[PASSPHRASE_MAXLEN];
487 GetDlgItemText(hwnd, IDC_PASSPHRASE1EDIT,
488 passphrase, sizeof(passphrase));
489 GetDlgItemText(hwnd, IDC_PASSPHRASE2EDIT,
490 passphrase2, sizeof(passphrase2));
491 if (strcmp(passphrase, passphrase2)) {
493 "The two passphrases given do not match.",
495 MB_OK | MB_ICONERROR);
500 ret = MessageBox(hwnd,
501 "Are you sure you want to save this key\n"
502 "without a passphrase to protect it?",
504 MB_YESNO | MB_ICONWARNING);
508 if (prompt_keyfile(hwnd, "Save private key as:",
511 FILE *fp = fopen(filename, "r");
513 char buffer[FILENAME_MAX+80];
515 sprintf(buffer, "Overwrite existing file\n%.*s?",
516 FILENAME_MAX, filename);
517 ret = MessageBox(hwnd, buffer, "PuTTYgen Warning",
518 MB_YESNO | MB_ICONWARNING);
522 ret = saversakey(filename, &state->key, &state->aux,
523 *passphrase ? passphrase : NULL);
525 MessageBox(hwnd, "Unable to save key file",
527 MB_OK | MB_ICONERROR);
533 state = (struct MainDlgState *)GetWindowLong(hwnd, GWL_USERDATA);
534 if (!state->generation_thread_exists) {
535 char filename[FILENAME_MAX];
536 if (prompt_keyfile(hwnd, "Load private key:",
538 char passphrase[PASSPHRASE_MAXLEN];
542 struct PassphraseProcStruct pps;
543 struct RSAKey newkey;
544 struct RSAAux newaux;
546 needs_pass = rsakey_encrypted(filename, &comment);
547 pps.passphrase = passphrase;
548 pps.comment = comment;
552 dlgret = DialogBoxParam(hinst,
553 MAKEINTRESOURCE(210),
554 NULL, PassphraseProc,
562 ret = loadrsakey(filename, &newkey, &newaux,
565 if (comment) free(comment);
567 MessageBox(NULL, "Couldn't load private key.",
568 "PuTTYgen Error", MB_OK | MB_ICONERROR);
569 } else if (ret == 1) {
573 EnableWindow(GetDlgItem(hwnd, IDC_GENERATE), 1);
574 EnableWindow(GetDlgItem(hwnd, IDC_LOAD), 1);
575 EnableWindow(GetDlgItem(hwnd, IDC_SAVE), 1);
577 * Now update the key controls with all the
582 SetDlgItemText(hwnd, IDC_PASSPHRASE1EDIT,
584 SetDlgItemText(hwnd, IDC_PASSPHRASE2EDIT,
586 SetDlgItemText(hwnd, IDC_COMMENTEDIT,
589 * Set the key fingerprint.
592 char *savecomment = state->key.comment;
593 state->key.comment = NULL;
594 rsa_fingerprint(buf, sizeof(buf), &state->key);
595 state->key.comment = savecomment;
597 SetDlgItemText(hwnd, IDC_FINGERPRINT, buf);
599 * Construct a decimal representation
600 * of the key, for pasting into
601 * .ssh/authorized_keys on a Unix box.
603 setupbigedit(hwnd, IDC_KEYDISPLAY, &state->key);
606 * Finally, hide the progress bar and show
609 hidemany(hwnd, nokey_ids, TRUE);
610 hidemany(hwnd, generating_ids, TRUE);
611 hidemany(hwnd, gotkey_ids, FALSE);
612 state->key_exists = TRUE;
620 state = (struct MainDlgState *)GetWindowLong(hwnd, GWL_USERDATA);
621 state->generation_thread_exists = FALSE;
622 state->key_exists = TRUE;
623 SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETPOS, PROGRESSRANGE, 0);
624 EnableWindow(GetDlgItem(hwnd, IDC_GENERATE), 1);
625 EnableWindow(GetDlgItem(hwnd, IDC_LOAD), 1);
626 EnableWindow(GetDlgItem(hwnd, IDC_SAVE), 1);
628 * Invent a comment for the key. We'll do this by including
629 * the date in it. This will be so horrifyingly ugly that
630 * the user will immediately want to change it, which is
633 state->key.comment = malloc(30);
639 strftime(state->key.comment, 30, "rsa-key-%Y%m%d", tm);
643 * Now update the key controls with all the key data.
648 * Blank passphrase, initially. This isn't dangerous,
649 * because we will warn (Are You Sure?) before allowing
650 * the user to save an unprotected private key.
652 SetDlgItemText(hwnd, IDC_PASSPHRASE1EDIT, "");
653 SetDlgItemText(hwnd, IDC_PASSPHRASE2EDIT, "");
657 SetDlgItemText(hwnd, IDC_COMMENTEDIT, state->key.comment);
659 * Set the key fingerprint.
662 char *savecomment = state->key.comment;
663 state->key.comment = NULL;
664 rsa_fingerprint(buf, sizeof(buf), &state->key);
665 state->key.comment = savecomment;
667 SetDlgItemText(hwnd, IDC_FINGERPRINT, buf);
669 * Construct a decimal representation of the key, for
670 * pasting into .ssh/authorized_keys on a Unix box.
672 setupbigedit(hwnd, IDC_KEYDISPLAY, &state->key);
675 * Finally, hide the progress bar and show the key data.
677 hidemany(hwnd, nokey_ids, TRUE);
678 hidemany(hwnd, generating_ids, TRUE);
679 hidemany(hwnd, gotkey_ids, FALSE);
682 state = (struct MainDlgState *)GetWindowLong(hwnd, GWL_USERDATA);
690 int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show) {
691 InitCommonControls();
694 return DialogBox(hinst, MAKEINTRESOURCE(201), NULL, MainDlgProc) != IDOK;