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->key_exists = FALSE;
326 SetWindowLong(hwnd, GWL_USERDATA, (LONG)state);
328 struct ctlpos cp, cp2;
330 /* Accelerators used: acglops */
332 ctlposinit(&cp, hwnd, 10, 10, 10);
333 bartitle(&cp, "Public and private key generation for PuTTY",
336 IDC_BOX_KEY, IDC_BOXT_KEY);
338 statictext(&cp2, "No key.", IDC_NOKEY);
342 progressbar(&cp2, IDC_PROGRESS);
344 "&Public key for pasting into authorized_keys file:",
345 IDC_PKSTATIC, IDC_KEYDISPLAY, 7);
346 SendDlgItemMessage(hwnd, IDC_KEYDISPLAY, EM_SETREADONLY, 1, 0);
347 staticedit(&cp, "Key fingerprint:", IDC_FPSTATIC,
348 IDC_FINGERPRINT, 70);
349 SendDlgItemMessage(hwnd, IDC_FINGERPRINT, EM_SETREADONLY, 1, 0);
350 staticedit(&cp, "Key &comment:", IDC_COMMENTSTATIC,
351 IDC_COMMENTEDIT, 70);
352 staticpassedit(&cp, "Key p&assphrase:", IDC_PASSPHRASE1STATIC,
353 IDC_PASSPHRASE1EDIT, 70);
354 staticpassedit(&cp, "C&onfirm passphrase:", IDC_PASSPHRASE2STATIC,
355 IDC_PASSPHRASE2EDIT, 70);
357 beginbox(&cp, "Actions",
358 IDC_BOX_ACTIONS, IDC_BOXT_ACTIONS);
359 staticbtn(&cp, "Generate a public/private key pair",
360 IDC_GENSTATIC, "&Generate", IDC_GENERATE);
361 staticbtn(&cp, "Load an existing private key file",
362 IDC_LOADSTATIC, "&Load", IDC_LOAD);
363 staticbtn(&cp, "Save the generated key to a new file",
364 IDC_SAVESTATIC, "&Save", IDC_SAVE);
366 beginbox(&cp, "Parameters",
367 IDC_BOX_PARAMS, IDC_BOXT_PARAMS);
368 staticedit(&cp, "Number of &bits in a generated key:",
369 IDC_BITSSTATIC, IDC_BITS, 20);
372 SetDlgItemInt(hwnd, IDC_BITS, DEFAULT_KEYSIZE, FALSE);
375 * Initially, hide the progress bar and the key display,
376 * and show the no-key display. Also disable the Save
377 * button, because with no key we obviously can't save
380 hidemany(hwnd, nokey_ids, FALSE);
381 hidemany(hwnd, generating_ids, TRUE);
382 hidemany(hwnd, gotkey_ids, TRUE);
383 EnableWindow(GetDlgItem(hwnd, IDC_SAVE), 0);
387 state = (struct MainDlgState *)GetWindowLong(hwnd, GWL_USERDATA);
388 if (state->collecting_entropy) {
389 state->entropy[state->entropy_got++] = lParam;
390 state->entropy[state->entropy_got++] = GetMessageTime();
391 SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETPOS,
392 state->entropy_got, 0);
393 if (state->entropy_got >= state->entropy_required) {
394 struct rsa_key_thread_params *params;
398 * Seed the entropy pool
400 random_add_heavynoise(state->entropy, state->entropy_size);
401 memset(state->entropy, 0, state->entropy_size);
402 free(state->entropy);
404 SetDlgItemText(hwnd, IDC_GENERATING, generating_msg);
405 SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETRANGE, 0,
406 MAKELPARAM(0, PROGRESSRANGE));
407 SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETPOS, 0, 0);
409 params = malloc(sizeof(*params));
410 params->progressbar = GetDlgItem(hwnd, IDC_PROGRESS);
411 params->dialog = hwnd;
412 params->keysize = state->keysize;
413 params->key = &state->key;
414 params->aux = &state->aux;
416 if (!CreateThread(NULL, 0, generate_rsa_key_thread,
417 params, 0, &threadid)) {
418 MessageBox(hwnd, "Out of thread resources",
419 "Key generation error",
420 MB_OK | MB_ICONERROR);
423 state->generation_thread_exists = TRUE;
424 state->collecting_entropy = FALSE;
430 switch (LOWORD(wParam)) {
431 case IDC_COMMENTEDIT:
432 if (HIWORD(wParam) == EN_CHANGE) {
433 state = (struct MainDlgState *)
434 GetWindowLong(hwnd, GWL_USERDATA);
435 if (state->key_exists) {
436 HWND editctl = GetDlgItem(hwnd, IDC_COMMENTEDIT);
437 int len = GetWindowTextLength(editctl);
438 if (state->key.comment)
439 free(state->key.comment);
440 state->key.comment = malloc(len+1);
441 GetWindowText(editctl, state->key.comment, len+1);
446 EnableWindow(hwnd, 0);
447 DialogBox (hinst, MAKEINTRESOURCE(213), NULL, AboutProc);
448 EnableWindow(hwnd, 1);
449 SetActiveWindow(hwnd);
452 state = (struct MainDlgState *)GetWindowLong(hwnd, GWL_USERDATA);
453 if (!state->generation_thread_exists) {
454 hidemany(hwnd, nokey_ids, TRUE);
455 hidemany(hwnd, generating_ids, FALSE);
456 hidemany(hwnd, gotkey_ids, TRUE);
457 EnableWindow(GetDlgItem(hwnd, IDC_GENERATE), 0);
458 EnableWindow(GetDlgItem(hwnd, IDC_LOAD), 0);
459 EnableWindow(GetDlgItem(hwnd, IDC_SAVE), 0);
460 state->key_exists = FALSE;
461 SetDlgItemText(hwnd, IDC_GENERATING, entropy_msg);
462 state->collecting_entropy = TRUE;
465 state->keysize = GetDlgItemInt(hwnd, IDC_BITS,
467 if (!ok) state->keysize = DEFAULT_KEYSIZE;
471 * My brief statistical tests on mouse movements
472 * suggest that there are about 2.5 bits of
473 * randomness in the x position, 2.5 in the y
474 * position, and 1.7 in the message time, making
475 * 5.7 bits of unpredictability per mouse movement.
476 * However, other people have told me it's far less
477 * than that, so I'm going to be stupidly cautious
478 * and knock that down to a nice round 2. With this
479 * method, we require two words per mouse movement,
480 * so with 2 bits per mouse movement we expect 2
481 * bits every 2 words.
483 state->entropy_required = (state->keysize/2) * 2;
484 state->entropy_got = 0;
485 state->entropy_size = (state->entropy_required *
486 sizeof(*state->entropy));
487 state->entropy = malloc(state->entropy_size);
489 SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETRANGE, 0,
490 MAKELPARAM(0, state->entropy_required));
491 SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETPOS, 0, 0);
495 state = (struct MainDlgState *)GetWindowLong(hwnd, GWL_USERDATA);
496 if (state->key_exists) {
497 char filename[FILENAME_MAX];
498 char passphrase[PASSPHRASE_MAXLEN];
499 char passphrase2[PASSPHRASE_MAXLEN];
500 GetDlgItemText(hwnd, IDC_PASSPHRASE1EDIT,
501 passphrase, sizeof(passphrase));
502 GetDlgItemText(hwnd, IDC_PASSPHRASE2EDIT,
503 passphrase2, sizeof(passphrase2));
504 if (strcmp(passphrase, passphrase2)) {
506 "The two passphrases given do not match.",
508 MB_OK | MB_ICONERROR);
513 ret = MessageBox(hwnd,
514 "Are you sure you want to save this key\n"
515 "without a passphrase to protect it?",
517 MB_YESNO | MB_ICONWARNING);
521 if (prompt_keyfile(hwnd, "Save private key as:",
524 FILE *fp = fopen(filename, "r");
526 char buffer[FILENAME_MAX+80];
528 sprintf(buffer, "Overwrite existing file\n%.*s?",
529 FILENAME_MAX, filename);
530 ret = MessageBox(hwnd, buffer, "PuTTYgen Warning",
531 MB_YESNO | MB_ICONWARNING);
535 ret = saversakey(filename, &state->key, &state->aux,
536 *passphrase ? passphrase : NULL);
538 MessageBox(hwnd, "Unable to save key file",
540 MB_OK | MB_ICONERROR);
546 state = (struct MainDlgState *)GetWindowLong(hwnd, GWL_USERDATA);
547 if (!state->generation_thread_exists) {
548 char filename[FILENAME_MAX];
549 if (prompt_keyfile(hwnd, "Load private key:",
551 char passphrase[PASSPHRASE_MAXLEN];
555 struct PassphraseProcStruct pps;
556 struct RSAKey newkey;
557 struct RSAAux newaux;
559 needs_pass = rsakey_encrypted(filename, &comment);
560 pps.passphrase = passphrase;
561 pps.comment = comment;
565 dlgret = DialogBoxParam(hinst,
566 MAKEINTRESOURCE(210),
567 NULL, PassphraseProc,
575 ret = loadrsakey(filename, &newkey, &newaux,
578 if (comment) free(comment);
580 MessageBox(NULL, "Couldn't load private key.",
581 "PuTTYgen Error", MB_OK | MB_ICONERROR);
582 } else if (ret == 1) {
586 EnableWindow(GetDlgItem(hwnd, IDC_GENERATE), 1);
587 EnableWindow(GetDlgItem(hwnd, IDC_LOAD), 1);
588 EnableWindow(GetDlgItem(hwnd, IDC_SAVE), 1);
590 * Now update the key controls with all the
595 SetDlgItemText(hwnd, IDC_PASSPHRASE1EDIT,
597 SetDlgItemText(hwnd, IDC_PASSPHRASE2EDIT,
599 SetDlgItemText(hwnd, IDC_COMMENTEDIT,
602 * Set the key fingerprint.
605 char *savecomment = state->key.comment;
606 state->key.comment = NULL;
607 rsa_fingerprint(buf, sizeof(buf), &state->key);
608 state->key.comment = savecomment;
610 SetDlgItemText(hwnd, IDC_FINGERPRINT, buf);
612 * Construct a decimal representation
613 * of the key, for pasting into
614 * .ssh/authorized_keys on a Unix box.
616 setupbigedit(hwnd, IDC_KEYDISPLAY, &state->key);
619 * Finally, hide the progress bar and show
622 hidemany(hwnd, nokey_ids, TRUE);
623 hidemany(hwnd, generating_ids, TRUE);
624 hidemany(hwnd, gotkey_ids, FALSE);
625 state->key_exists = TRUE;
633 state = (struct MainDlgState *)GetWindowLong(hwnd, GWL_USERDATA);
634 state->generation_thread_exists = FALSE;
635 state->key_exists = TRUE;
636 SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETPOS, PROGRESSRANGE, 0);
637 EnableWindow(GetDlgItem(hwnd, IDC_GENERATE), 1);
638 EnableWindow(GetDlgItem(hwnd, IDC_LOAD), 1);
639 EnableWindow(GetDlgItem(hwnd, IDC_SAVE), 1);
641 * Invent a comment for the key. We'll do this by including
642 * the date in it. This will be so horrifyingly ugly that
643 * the user will immediately want to change it, which is
646 state->key.comment = malloc(30);
652 strftime(state->key.comment, 30, "rsa-key-%Y%m%d", tm);
656 * Now update the key controls with all the key data.
661 * Blank passphrase, initially. This isn't dangerous,
662 * because we will warn (Are You Sure?) before allowing
663 * the user to save an unprotected private key.
665 SetDlgItemText(hwnd, IDC_PASSPHRASE1EDIT, "");
666 SetDlgItemText(hwnd, IDC_PASSPHRASE2EDIT, "");
670 SetDlgItemText(hwnd, IDC_COMMENTEDIT, state->key.comment);
672 * Set the key fingerprint.
675 char *savecomment = state->key.comment;
676 state->key.comment = NULL;
677 rsa_fingerprint(buf, sizeof(buf), &state->key);
678 state->key.comment = savecomment;
680 SetDlgItemText(hwnd, IDC_FINGERPRINT, buf);
682 * Construct a decimal representation of the key, for
683 * pasting into .ssh/authorized_keys on a Unix box.
685 setupbigedit(hwnd, IDC_KEYDISPLAY, &state->key);
688 * Finally, hide the progress bar and show the key data.
690 hidemany(hwnd, nokey_ids, TRUE);
691 hidemany(hwnd, generating_ids, TRUE);
692 hidemany(hwnd, gotkey_ids, FALSE);
695 state = (struct MainDlgState *)GetWindowLong(hwnd, GWL_USERDATA);
703 int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show) {
704 InitCommonControls();
707 return DialogBox(hinst, MAKEINTRESOURCE(201), NULL, MainDlgProc) != IDOK;