2 * PuTTY key generation front end (Windows).
9 #define PUTTY_DO_GLOBALS
20 #define WM_DONEKEY (WM_XUSER + 1)
22 #define DEFAULT_KEYSIZE 1024
24 static char *cmdline_keyfile = NULL;
27 * Print a modal (Really Bad) message box and perform a fatal exit.
29 void modalfatalbox(char *fmt, ...)
35 stuff = dupvprintf(fmt, ap);
37 MessageBox(NULL, stuff, "PuTTYgen Fatal Error",
38 MB_SYSTEMMODAL | MB_ICONERROR | MB_OK);
43 /* ----------------------------------------------------------------------
44 * Progress report code. This is really horrible :-)
46 #define PROGRESSRANGE 65535
52 unsigned startpoint, total;
53 unsigned param, current, n; /* if exponential */
54 unsigned mult; /* if linear */
56 unsigned total, divisor, range;
60 static void progress_update(void *param, int action, int phase, int iprogress)
62 struct progress *p = (struct progress *) param;
63 unsigned progress = iprogress;
66 if (action < PROGFN_READY && p->nphases < phase)
69 case PROGFN_INITIALISE:
72 case PROGFN_LIN_PHASE:
73 p->phases[phase-1].exponential = 0;
74 p->phases[phase-1].mult = p->phases[phase].total / progress;
76 case PROGFN_EXP_PHASE:
77 p->phases[phase-1].exponential = 1;
78 p->phases[phase-1].param = 0x10000 + progress;
79 p->phases[phase-1].current = p->phases[phase-1].total;
80 p->phases[phase-1].n = 0;
82 case PROGFN_PHASE_EXTENT:
83 p->phases[phase-1].total = progress;
89 for (i = 0; i < p->nphases; i++) {
90 p->phases[i].startpoint = total;
91 total += p->phases[i].total;
94 p->divisor = ((p->total + PROGRESSRANGE - 1) / PROGRESSRANGE);
95 p->range = p->total / p->divisor;
96 SendMessage(p->progbar, PBM_SETRANGE, 0, MAKELPARAM(0, p->range));
100 if (p->phases[phase-1].exponential) {
101 while (p->phases[phase-1].n < progress) {
102 p->phases[phase-1].n++;
103 p->phases[phase-1].current *= p->phases[phase-1].param;
104 p->phases[phase-1].current /= 0x10000;
106 position = (p->phases[phase-1].startpoint +
107 p->phases[phase-1].total - p->phases[phase-1].current);
109 position = (p->phases[phase-1].startpoint +
110 progress * p->phases[phase-1].mult);
112 SendMessage(p->progbar, PBM_SETPOS, position / p->divisor, 0);
119 #define PASSPHRASE_MAXLEN 512
121 struct PassphraseProcStruct {
127 * Dialog-box function for the passphrase box.
129 static int CALLBACK PassphraseProc(HWND hwnd, UINT msg,
130 WPARAM wParam, LPARAM lParam)
132 static char *passphrase = NULL;
133 struct PassphraseProcStruct *p;
137 SetForegroundWindow(hwnd);
138 SetWindowPos(hwnd, HWND_TOP, 0, 0, 0, 0,
139 SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
144 { /* centre the window */
148 hw = GetDesktopWindow();
149 if (GetWindowRect(hw, &rs) && GetWindowRect(hwnd, &rd))
151 (rs.right + rs.left + rd.left - rd.right) / 2,
152 (rs.bottom + rs.top + rd.top - rd.bottom) / 2,
153 rd.right - rd.left, rd.bottom - rd.top, TRUE);
156 p = (struct PassphraseProcStruct *) lParam;
157 passphrase = p->passphrase;
159 SetDlgItemText(hwnd, 101, p->comment);
161 SetDlgItemText(hwnd, 102, passphrase);
164 switch (LOWORD(wParam)) {
174 case 102: /* edit box */
175 if ((HIWORD(wParam) == EN_CHANGE) && passphrase) {
176 GetDlgItemText(hwnd, 102, passphrase,
177 PASSPHRASE_MAXLEN - 1);
178 passphrase[PASSPHRASE_MAXLEN - 1] = '\0';
191 * Prompt for a key file. Assumes the filename buffer is of size
194 static int prompt_keyfile(HWND hwnd, char *dlgtitle,
195 char *filename, int save, int ppk)
198 memset(&of, 0, sizeof(of));
201 of.lpstrFilter = "PuTTY Private Key Files (*.ppk)\0*.ppk\0"
202 "All Files (*.*)\0*\0\0\0";
203 of.lpstrDefExt = ".ppk";
205 of.lpstrFilter = "All Files (*.*)\0*\0\0\0";
207 of.lpstrCustomFilter = NULL;
209 of.lpstrFile = filename;
211 of.nMaxFile = FILENAME_MAX;
212 of.lpstrFileTitle = NULL;
213 of.lpstrTitle = dlgtitle;
215 return request_file(NULL, &of, FALSE, save);
219 * Dialog-box function for the Licence box.
221 static int CALLBACK LicenceProc(HWND hwnd, UINT msg,
222 WPARAM wParam, LPARAM lParam)
229 { /* centre the window */
233 hw = GetDesktopWindow();
234 if (GetWindowRect(hw, &rs) && GetWindowRect(hwnd, &rd))
236 (rs.right + rs.left + rd.left - rd.right) / 2,
237 (rs.bottom + rs.top + rd.top - rd.bottom) / 2,
238 rd.right - rd.left, rd.bottom - rd.top, TRUE);
243 switch (LOWORD(wParam)) {
258 * Dialog-box function for the About box.
260 static int CALLBACK AboutProc(HWND hwnd, UINT msg,
261 WPARAM wParam, LPARAM lParam)
268 { /* centre the window */
272 hw = GetDesktopWindow();
273 if (GetWindowRect(hw, &rs) && GetWindowRect(hwnd, &rd))
275 (rs.right + rs.left + rd.left - rd.right) / 2,
276 (rs.bottom + rs.top + rd.top - rd.bottom) / 2,
277 rd.right - rd.left, rd.bottom - rd.top, TRUE);
280 SetDlgItemText(hwnd, 100, ver);
283 switch (LOWORD(wParam)) {
289 EnableWindow(hwnd, 0);
290 DialogBox(hinst, MAKEINTRESOURCE(214), hwnd, LicenceProc);
291 EnableWindow(hwnd, 1);
292 SetActiveWindow(hwnd);
304 * Thread to generate a key.
306 struct rsa_key_thread_params {
307 HWND progressbar; /* notify this with progress */
308 HWND dialog; /* notify this on completion */
309 int keysize; /* bits in key */
312 struct dss_key *dsskey;
314 static DWORD WINAPI generate_rsa_key_thread(void *param)
316 struct rsa_key_thread_params *params =
317 (struct rsa_key_thread_params *) param;
318 struct progress prog;
319 prog.progbar = params->progressbar;
321 progress_update(&prog, PROGFN_INITIALISE, 0, 0);
324 dsa_generate(params->dsskey, params->keysize, progress_update, &prog);
326 rsa_generate(params->key, params->keysize, progress_update, &prog);
328 PostMessage(params->dialog, WM_DONEKEY, 0, 0);
334 struct MainDlgState {
335 int collecting_entropy;
336 int generation_thread_exists;
338 int entropy_got, entropy_required, entropy_size;
341 char **commentptr; /* points to key.comment or ssh2key.comment */
342 struct ssh2_userkey ssh2key;
345 struct dss_key dsskey;
346 HMENU filemenu, keymenu, cvtmenu;
349 static void hidemany(HWND hwnd, const int *ids, int hideit)
352 ShowWindow(GetDlgItem(hwnd, *ids++), (hideit ? SW_HIDE : SW_SHOW));
356 static void setupbigedit1(HWND hwnd, int id, int idstatic, struct RSAKey *key)
361 dec1 = bignum_decimal(key->exponent);
362 dec2 = bignum_decimal(key->modulus);
363 buffer = dupprintf("%d %s %s %s", bignum_bitcount(key->modulus),
364 dec1, dec2, key->comment);
365 SetDlgItemText(hwnd, id, buffer);
366 SetDlgItemText(hwnd, idstatic,
367 "&Public key for pasting into authorized_keys file:");
373 static void setupbigedit2(HWND hwnd, int id, int idstatic,
374 struct ssh2_userkey *key)
376 unsigned char *pub_blob;
381 pub_blob = key->alg->public_blob(key->data, &pub_len);
382 buffer = snewn(strlen(key->alg->name) + 4 * ((pub_len + 2) / 3) +
383 strlen(key->comment) + 3, char);
384 strcpy(buffer, key->alg->name);
385 p = buffer + strlen(buffer);
388 while (i < pub_len) {
389 int n = (pub_len - i < 3 ? pub_len - i : 3);
390 base64_encode_atom(pub_blob + i, n, p);
395 strcpy(p, key->comment);
396 SetDlgItemText(hwnd, id, buffer);
397 SetDlgItemText(hwnd, idstatic, "&Public key for pasting into "
398 "OpenSSH authorized_keys file:");
403 static int save_ssh1_pubkey(char *filename, struct RSAKey *key)
408 dec1 = bignum_decimal(key->exponent);
409 dec2 = bignum_decimal(key->modulus);
410 fp = fopen(filename, "wb");
413 fprintf(fp, "%d %s %s %s\n",
414 bignum_bitcount(key->modulus), dec1, dec2, key->comment);
422 * Warn about the obsolescent key file format.
424 void old_keyfile_warning(void)
426 static const char mbtitle[] = "PuTTY Key File Warning";
427 static const char message[] =
428 "You are loading an SSH-2 private key which has an\n"
429 "old version of the file format. This means your key\n"
430 "file is not fully tamperproof. Future versions of\n"
431 "PuTTY may stop supporting this private key format,\n"
432 "so we recommend you convert your key to the new\n"
435 "Once the key is loaded into PuTTYgen, you can perform\n"
436 "this conversion simply by saving it again.";
438 MessageBox(NULL, message, mbtitle, MB_OK);
441 static int save_ssh2_pubkey(char *filename, struct ssh2_userkey *key)
443 unsigned char *pub_blob;
449 pub_blob = key->alg->public_blob(key->data, &pub_len);
451 fp = fopen(filename, "wb");
455 fprintf(fp, "---- BEGIN SSH2 PUBLIC KEY ----\n");
457 fprintf(fp, "Comment: \"");
458 for (p = key->comment; *p; p++) {
459 if (*p == '\\' || *p == '\"')
467 while (i < pub_len) {
469 int n = (pub_len - i < 3 ? pub_len - i : 3);
470 base64_encode_atom(pub_blob + i, n, buf);
474 if (++column >= 16) {
482 fprintf(fp, "---- END SSH2 PUBLIC KEY ----\n");
489 controlidstart = 100,
496 IDC_PKSTATIC, IDC_KEYDISPLAY,
497 IDC_FPSTATIC, IDC_FINGERPRINT,
498 IDC_COMMENTSTATIC, IDC_COMMENTEDIT,
499 IDC_PASSPHRASE1STATIC, IDC_PASSPHRASE1EDIT,
500 IDC_PASSPHRASE2STATIC, IDC_PASSPHRASE2EDIT,
502 IDC_GENSTATIC, IDC_GENERATE,
503 IDC_LOADSTATIC, IDC_LOAD,
504 IDC_SAVESTATIC, IDC_SAVE, IDC_SAVEPUB,
506 IDC_TYPESTATIC, IDC_KEYSSH1, IDC_KEYSSH2RSA, IDC_KEYSSH2DSA,
507 IDC_BITSSTATIC, IDC_BITS,
510 IDC_IMPORT, IDC_EXPORT_OPENSSH, IDC_EXPORT_SSHCOM
513 static const int nokey_ids[] = { IDC_NOKEY, 0 };
514 static const int generating_ids[] = { IDC_GENERATING, IDC_PROGRESS, 0 };
515 static const int gotkey_ids[] = {
516 IDC_PKSTATIC, IDC_KEYDISPLAY,
517 IDC_FPSTATIC, IDC_FINGERPRINT,
518 IDC_COMMENTSTATIC, IDC_COMMENTEDIT,
519 IDC_PASSPHRASE1STATIC, IDC_PASSPHRASE1EDIT,
520 IDC_PASSPHRASE2STATIC, IDC_PASSPHRASE2EDIT, 0
524 * Small UI helper function to switch the state of the main dialog
525 * by enabling and disabling controls and menu items.
527 void ui_set_state(HWND hwnd, struct MainDlgState *state, int status)
533 hidemany(hwnd, nokey_ids, FALSE);
534 hidemany(hwnd, generating_ids, TRUE);
535 hidemany(hwnd, gotkey_ids, TRUE);
536 EnableWindow(GetDlgItem(hwnd, IDC_GENERATE), 1);
537 EnableWindow(GetDlgItem(hwnd, IDC_LOAD), 1);
538 EnableWindow(GetDlgItem(hwnd, IDC_SAVE), 0);
539 EnableWindow(GetDlgItem(hwnd, IDC_SAVEPUB), 0);
540 EnableWindow(GetDlgItem(hwnd, IDC_KEYSSH1), 1);
541 EnableWindow(GetDlgItem(hwnd, IDC_KEYSSH2RSA), 1);
542 EnableWindow(GetDlgItem(hwnd, IDC_KEYSSH2DSA), 1);
543 EnableWindow(GetDlgItem(hwnd, IDC_BITS), 1);
544 EnableMenuItem(state->filemenu, IDC_LOAD, MF_ENABLED|MF_BYCOMMAND);
545 EnableMenuItem(state->filemenu, IDC_SAVE, MF_GRAYED|MF_BYCOMMAND);
546 EnableMenuItem(state->filemenu, IDC_SAVEPUB, MF_GRAYED|MF_BYCOMMAND);
547 EnableMenuItem(state->keymenu, IDC_GENERATE, MF_ENABLED|MF_BYCOMMAND);
548 EnableMenuItem(state->keymenu, IDC_KEYSSH1, MF_ENABLED|MF_BYCOMMAND);
549 EnableMenuItem(state->keymenu, IDC_KEYSSH2RSA, MF_ENABLED|MF_BYCOMMAND);
550 EnableMenuItem(state->keymenu, IDC_KEYSSH2DSA, MF_ENABLED|MF_BYCOMMAND);
551 EnableMenuItem(state->cvtmenu, IDC_IMPORT, MF_ENABLED|MF_BYCOMMAND);
552 EnableMenuItem(state->cvtmenu, IDC_EXPORT_OPENSSH,
553 MF_GRAYED|MF_BYCOMMAND);
554 EnableMenuItem(state->cvtmenu, IDC_EXPORT_SSHCOM,
555 MF_GRAYED|MF_BYCOMMAND);
557 case 1: /* generating key */
558 hidemany(hwnd, nokey_ids, TRUE);
559 hidemany(hwnd, generating_ids, FALSE);
560 hidemany(hwnd, gotkey_ids, TRUE);
561 EnableWindow(GetDlgItem(hwnd, IDC_GENERATE), 0);
562 EnableWindow(GetDlgItem(hwnd, IDC_LOAD), 0);
563 EnableWindow(GetDlgItem(hwnd, IDC_SAVE), 0);
564 EnableWindow(GetDlgItem(hwnd, IDC_SAVEPUB), 0);
565 EnableWindow(GetDlgItem(hwnd, IDC_KEYSSH1), 0);
566 EnableWindow(GetDlgItem(hwnd, IDC_KEYSSH2RSA), 0);
567 EnableWindow(GetDlgItem(hwnd, IDC_KEYSSH2DSA), 0);
568 EnableWindow(GetDlgItem(hwnd, IDC_BITS), 0);
569 EnableMenuItem(state->filemenu, IDC_LOAD, MF_GRAYED|MF_BYCOMMAND);
570 EnableMenuItem(state->filemenu, IDC_SAVE, MF_GRAYED|MF_BYCOMMAND);
571 EnableMenuItem(state->filemenu, IDC_SAVEPUB, MF_GRAYED|MF_BYCOMMAND);
572 EnableMenuItem(state->keymenu, IDC_GENERATE, MF_GRAYED|MF_BYCOMMAND);
573 EnableMenuItem(state->keymenu, IDC_KEYSSH1, MF_GRAYED|MF_BYCOMMAND);
574 EnableMenuItem(state->keymenu, IDC_KEYSSH2RSA, MF_GRAYED|MF_BYCOMMAND);
575 EnableMenuItem(state->keymenu, IDC_KEYSSH2DSA, MF_GRAYED|MF_BYCOMMAND);
576 EnableMenuItem(state->cvtmenu, IDC_IMPORT, MF_GRAYED|MF_BYCOMMAND);
577 EnableMenuItem(state->cvtmenu, IDC_EXPORT_OPENSSH,
578 MF_GRAYED|MF_BYCOMMAND);
579 EnableMenuItem(state->cvtmenu, IDC_EXPORT_SSHCOM,
580 MF_GRAYED|MF_BYCOMMAND);
583 hidemany(hwnd, nokey_ids, TRUE);
584 hidemany(hwnd, generating_ids, TRUE);
585 hidemany(hwnd, gotkey_ids, FALSE);
586 EnableWindow(GetDlgItem(hwnd, IDC_GENERATE), 1);
587 EnableWindow(GetDlgItem(hwnd, IDC_LOAD), 1);
588 EnableWindow(GetDlgItem(hwnd, IDC_SAVE), 1);
589 EnableWindow(GetDlgItem(hwnd, IDC_SAVEPUB), 1);
590 EnableWindow(GetDlgItem(hwnd, IDC_KEYSSH1), 1);
591 EnableWindow(GetDlgItem(hwnd, IDC_KEYSSH2RSA), 1);
592 EnableWindow(GetDlgItem(hwnd, IDC_KEYSSH2DSA), 1);
593 EnableWindow(GetDlgItem(hwnd, IDC_BITS), 1);
594 EnableMenuItem(state->filemenu, IDC_LOAD, MF_ENABLED|MF_BYCOMMAND);
595 EnableMenuItem(state->filemenu, IDC_SAVE, MF_ENABLED|MF_BYCOMMAND);
596 EnableMenuItem(state->filemenu, IDC_SAVEPUB, MF_ENABLED|MF_BYCOMMAND);
597 EnableMenuItem(state->keymenu, IDC_GENERATE, MF_ENABLED|MF_BYCOMMAND);
598 EnableMenuItem(state->keymenu, IDC_KEYSSH1, MF_ENABLED|MF_BYCOMMAND);
599 EnableMenuItem(state->keymenu, IDC_KEYSSH2RSA,MF_ENABLED|MF_BYCOMMAND);
600 EnableMenuItem(state->keymenu, IDC_KEYSSH2DSA,MF_ENABLED|MF_BYCOMMAND);
601 EnableMenuItem(state->cvtmenu, IDC_IMPORT, MF_ENABLED|MF_BYCOMMAND);
603 * Enable export menu items if and only if the key type
604 * supports this kind of export.
606 type = state->ssh2 ? SSH_KEYTYPE_SSH2 : SSH_KEYTYPE_SSH1;
607 #define do_export_menuitem(x,y) \
608 EnableMenuItem(state->cvtmenu, x, MF_BYCOMMAND | \
609 (import_target_type(y)==type?MF_ENABLED:MF_GRAYED))
610 do_export_menuitem(IDC_EXPORT_OPENSSH, SSH_KEYTYPE_OPENSSH);
611 do_export_menuitem(IDC_EXPORT_SSHCOM, SSH_KEYTYPE_SSHCOM);
612 #undef do_export_menuitem
617 void load_key_file(HWND hwnd, struct MainDlgState *state,
618 Filename filename, int was_import_cmd)
620 char passphrase[PASSPHRASE_MAXLEN];
624 const char *errmsg = NULL;
626 struct PassphraseProcStruct pps;
627 struct RSAKey newkey1;
628 struct ssh2_userkey *newkey2 = NULL;
630 type = realtype = key_type(&filename);
631 if (type != SSH_KEYTYPE_SSH1 &&
632 type != SSH_KEYTYPE_SSH2 &&
633 !import_possible(type)) {
634 char *msg = dupprintf("Couldn't load private key (%s)",
635 key_type_to_str(type));
636 message_box(msg, "PuTTYgen Error", MB_OK | MB_ICONERROR,
637 HELPCTXID(errors_cantloadkey));
642 if (type != SSH_KEYTYPE_SSH1 &&
643 type != SSH_KEYTYPE_SSH2) {
645 type = import_target_type(type);
649 if (realtype == SSH_KEYTYPE_SSH1)
650 needs_pass = rsakey_encrypted(&filename, &comment);
651 else if (realtype == SSH_KEYTYPE_SSH2)
653 ssh2_userkey_encrypted(&filename, &comment);
655 needs_pass = import_encrypted(&filename, realtype,
657 pps.passphrase = passphrase;
658 pps.comment = comment;
662 dlgret = DialogBoxParam(hinst,
663 MAKEINTRESOURCE(210),
664 NULL, PassphraseProc,
672 if (type == SSH_KEYTYPE_SSH1) {
673 if (realtype == type)
674 ret = loadrsakey(&filename, &newkey1,
675 passphrase, &errmsg);
677 ret = import_ssh1(&filename, realtype,
678 &newkey1, passphrase, &errmsg);
680 if (realtype == type)
681 newkey2 = ssh2_load_userkey(&filename,
682 passphrase, &errmsg);
684 newkey2 = import_ssh2(&filename, realtype,
685 passphrase, &errmsg);
686 if (newkey2 == SSH2_WRONG_PASSPHRASE)
697 char *msg = dupprintf("Couldn't load private key (%s)", errmsg);
698 message_box(msg, "PuTTYgen Error", MB_OK | MB_ICONERROR,
699 HELPCTXID(errors_cantloadkey));
701 } else if (ret == 1) {
703 * Now update the key controls with all the
707 SetDlgItemText(hwnd, IDC_PASSPHRASE1EDIT,
709 SetDlgItemText(hwnd, IDC_PASSPHRASE2EDIT,
711 if (type == SSH_KEYTYPE_SSH1) {
716 state->commentptr = &state->key.comment;
717 state->key = newkey1;
720 * Set the key fingerprint.
722 savecomment = state->key.comment;
723 state->key.comment = NULL;
724 rsa_fingerprint(buf, sizeof(buf),
726 state->key.comment = savecomment;
728 SetDlgItemText(hwnd, IDC_FINGERPRINT, buf);
730 * Construct a decimal representation
731 * of the key, for pasting into
732 * .ssh/authorized_keys on a Unix box.
734 setupbigedit1(hwnd, IDC_KEYDISPLAY,
735 IDC_PKSTATIC, &state->key);
742 &state->ssh2key.comment;
743 state->ssh2key = *newkey2; /* structure copy */
746 savecomment = state->ssh2key.comment;
747 state->ssh2key.comment = NULL;
750 fingerprint(state->ssh2key.data);
751 state->ssh2key.comment = savecomment;
753 SetDlgItemText(hwnd, IDC_FINGERPRINT, fp);
756 setupbigedit2(hwnd, IDC_KEYDISPLAY,
757 IDC_PKSTATIC, &state->ssh2key);
759 SetDlgItemText(hwnd, IDC_COMMENTEDIT,
763 * Finally, hide the progress bar and show
766 ui_set_state(hwnd, state, 2);
767 state->key_exists = TRUE;
770 * If the user has imported a foreign key
771 * using the Load command, let them know.
772 * If they've used the Import command, be
775 if (realtype != type && !was_import_cmd) {
777 sprintf(msg, "Successfully imported foreign key\n"
779 "To use this key with PuTTY, you need to\n"
780 "use the \"Save private key\" command to\n"
781 "save it in PuTTY's own format.",
782 key_type_to_str(realtype));
783 MessageBox(NULL, msg, "PuTTYgen Notice",
784 MB_OK | MB_ICONINFORMATION);
790 * Dialog-box function for the main PuTTYgen dialog box.
792 static int CALLBACK MainDlgProc(HWND hwnd, UINT msg,
793 WPARAM wParam, LPARAM lParam)
795 static const char generating_msg[] =
796 "Please wait while a key is generated...";
797 static const char entropy_msg[] =
798 "Please generate some randomness by moving the mouse over the blank area.";
799 struct MainDlgState *state;
804 SetWindowLong(hwnd, GWL_EXSTYLE,
805 GetWindowLong(hwnd, GWL_EXSTYLE) | WS_EX_CONTEXTHELP);
808 * If we add a Help button, this is where we destroy it
809 * if the help file isn't present.
812 requested_help = FALSE;
813 SendMessage(hwnd, WM_SETICON, (WPARAM) ICON_BIG,
814 (LPARAM) LoadIcon(hinst, MAKEINTRESOURCE(200)));
816 state = snew(struct MainDlgState);
817 state->generation_thread_exists = FALSE;
818 state->collecting_entropy = FALSE;
819 state->entropy = NULL;
820 state->key_exists = FALSE;
821 SetWindowLong(hwnd, GWL_USERDATA, (LONG) state);
827 menu1 = CreateMenu();
828 AppendMenu(menu1, MF_ENABLED, IDC_LOAD, "&Load private key");
829 AppendMenu(menu1, MF_ENABLED, IDC_SAVEPUB, "Save p&ublic key");
830 AppendMenu(menu1, MF_ENABLED, IDC_SAVE, "&Save private key");
831 AppendMenu(menu1, MF_SEPARATOR, 0, 0);
832 AppendMenu(menu1, MF_ENABLED, IDC_QUIT, "E&xit");
833 AppendMenu(menu, MF_POPUP | MF_ENABLED, (UINT) menu1, "&File");
834 state->filemenu = menu1;
836 menu1 = CreateMenu();
837 AppendMenu(menu1, MF_ENABLED, IDC_GENERATE, "&Generate key pair");
838 AppendMenu(menu1, MF_SEPARATOR, 0, 0);
839 AppendMenu(menu1, MF_ENABLED, IDC_KEYSSH1, "SSH-&1 key (RSA)");
840 AppendMenu(menu1, MF_ENABLED, IDC_KEYSSH2RSA, "SSH-2 &RSA key");
841 AppendMenu(menu1, MF_ENABLED, IDC_KEYSSH2DSA, "SSH-2 &DSA key");
842 AppendMenu(menu, MF_POPUP | MF_ENABLED, (UINT) menu1, "&Key");
843 state->keymenu = menu1;
845 menu1 = CreateMenu();
846 AppendMenu(menu1, MF_ENABLED, IDC_IMPORT, "&Import key");
847 AppendMenu(menu1, MF_SEPARATOR, 0, 0);
848 AppendMenu(menu1, MF_ENABLED, IDC_EXPORT_OPENSSH,
849 "Export &OpenSSH key");
850 AppendMenu(menu1, MF_ENABLED, IDC_EXPORT_SSHCOM,
851 "Export &ssh.com key");
852 AppendMenu(menu, MF_POPUP | MF_ENABLED, (UINT) menu1,
854 state->cvtmenu = menu1;
856 menu1 = CreateMenu();
857 AppendMenu(menu1, MF_ENABLED, IDC_ABOUT, "&About");
859 AppendMenu(menu1, MF_ENABLED, IDC_GIVEHELP, "&Help");
860 AppendMenu(menu, MF_POPUP | MF_ENABLED, (UINT) menu1, "&Help");
868 { /* centre the window */
872 hw = GetDesktopWindow();
873 if (GetWindowRect(hw, &rs) && GetWindowRect(hwnd, &rd))
875 (rs.right + rs.left + rd.left - rd.right) / 2,
876 (rs.bottom + rs.top + rd.top - rd.bottom) / 2,
877 rd.right - rd.left, rd.bottom - rd.top, TRUE);
881 struct ctlpos cp, cp2;
883 /* Accelerators used: acglops1rbd */
885 ctlposinit(&cp, hwnd, 4, 4, 4);
886 beginbox(&cp, "Key", IDC_BOX_KEY);
888 statictext(&cp2, "No key.", 1, IDC_NOKEY);
890 statictext(&cp2, "", 1, IDC_GENERATING);
891 progressbar(&cp2, IDC_PROGRESS);
893 "&Public key for pasting into authorized_keys file:",
894 IDC_PKSTATIC, IDC_KEYDISPLAY, 5);
895 SendDlgItemMessage(hwnd, IDC_KEYDISPLAY, EM_SETREADONLY, 1, 0);
896 staticedit(&cp, "Key f&ingerprint:", IDC_FPSTATIC,
897 IDC_FINGERPRINT, 75);
898 SendDlgItemMessage(hwnd, IDC_FINGERPRINT, EM_SETREADONLY, 1,
900 staticedit(&cp, "Key &comment:", IDC_COMMENTSTATIC,
901 IDC_COMMENTEDIT, 75);
902 staticpassedit(&cp, "Key p&assphrase:", IDC_PASSPHRASE1STATIC,
903 IDC_PASSPHRASE1EDIT, 75);
904 staticpassedit(&cp, "C&onfirm passphrase:",
905 IDC_PASSPHRASE2STATIC, IDC_PASSPHRASE2EDIT, 75);
907 beginbox(&cp, "Actions", IDC_BOX_ACTIONS);
908 staticbtn(&cp, "Generate a public/private key pair",
909 IDC_GENSTATIC, "&Generate", IDC_GENERATE);
910 staticbtn(&cp, "Load an existing private key file",
911 IDC_LOADSTATIC, "&Load", IDC_LOAD);
912 static2btn(&cp, "Save the generated key", IDC_SAVESTATIC,
913 "Save p&ublic key", IDC_SAVEPUB,
914 "&Save private key", IDC_SAVE);
916 beginbox(&cp, "Parameters", IDC_BOX_PARAMS);
917 radioline(&cp, "Type of key to generate:", IDC_TYPESTATIC, 3,
918 "SSH-&1 (RSA)", IDC_KEYSSH1,
919 "SSH-2 &RSA", IDC_KEYSSH2RSA,
920 "SSH-2 &DSA", IDC_KEYSSH2DSA, NULL);
921 staticedit(&cp, "Number of &bits in a generated key:",
922 IDC_BITSSTATIC, IDC_BITS, 20);
925 CheckRadioButton(hwnd, IDC_KEYSSH1, IDC_KEYSSH2DSA, IDC_KEYSSH2RSA);
926 CheckMenuRadioItem(state->keymenu, IDC_KEYSSH1, IDC_KEYSSH2DSA,
927 IDC_KEYSSH2RSA, MF_BYCOMMAND);
928 SetDlgItemInt(hwnd, IDC_BITS, DEFAULT_KEYSIZE, FALSE);
931 * Initially, hide the progress bar and the key display,
932 * and show the no-key display. Also disable the Save
933 * buttons, because with no key we obviously can't save
936 ui_set_state(hwnd, state, 0);
939 * Load a key file if one was provided on the command line.
942 load_key_file(hwnd, state, filename_from_str(cmdline_keyfile), 0);
946 state = (struct MainDlgState *) GetWindowLong(hwnd, GWL_USERDATA);
947 if (state->collecting_entropy &&
948 state->entropy && state->entropy_got < state->entropy_required) {
949 state->entropy[state->entropy_got++] = lParam;
950 state->entropy[state->entropy_got++] = GetMessageTime();
951 SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETPOS,
952 state->entropy_got, 0);
953 if (state->entropy_got >= state->entropy_required) {
954 struct rsa_key_thread_params *params;
958 * Seed the entropy pool
960 random_add_heavynoise(state->entropy, state->entropy_size);
961 memset(state->entropy, 0, state->entropy_size);
962 sfree(state->entropy);
963 state->collecting_entropy = FALSE;
965 SetDlgItemText(hwnd, IDC_GENERATING, generating_msg);
966 SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETRANGE, 0,
967 MAKELPARAM(0, PROGRESSRANGE));
968 SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETPOS, 0, 0);
970 params = snew(struct rsa_key_thread_params);
971 params->progressbar = GetDlgItem(hwnd, IDC_PROGRESS);
972 params->dialog = hwnd;
973 params->keysize = state->keysize;
974 params->is_dsa = state->is_dsa;
975 params->key = &state->key;
976 params->dsskey = &state->dsskey;
978 if (!CreateThread(NULL, 0, generate_rsa_key_thread,
979 params, 0, &threadid)) {
980 MessageBox(hwnd, "Out of thread resources",
981 "Key generation error",
982 MB_OK | MB_ICONERROR);
985 state->generation_thread_exists = TRUE;
991 switch (LOWORD(wParam)) {
996 state = (struct MainDlgState *)
997 GetWindowLong(hwnd, GWL_USERDATA);
998 if (!IsDlgButtonChecked(hwnd, LOWORD(wParam)))
999 CheckRadioButton(hwnd, IDC_KEYSSH1, IDC_KEYSSH2DSA,
1001 CheckMenuRadioItem(state->keymenu, IDC_KEYSSH1, IDC_KEYSSH2DSA,
1002 LOWORD(wParam), MF_BYCOMMAND);
1006 PostMessage(hwnd, WM_CLOSE, 0, 0);
1008 case IDC_COMMENTEDIT:
1009 if (HIWORD(wParam) == EN_CHANGE) {
1010 state = (struct MainDlgState *)
1011 GetWindowLong(hwnd, GWL_USERDATA);
1012 if (state->key_exists) {
1013 HWND editctl = GetDlgItem(hwnd, IDC_COMMENTEDIT);
1014 int len = GetWindowTextLength(editctl);
1015 if (*state->commentptr)
1016 sfree(*state->commentptr);
1017 *state->commentptr = snewn(len + 1, char);
1018 GetWindowText(editctl, *state->commentptr, len + 1);
1020 setupbigedit2(hwnd, IDC_KEYDISPLAY, IDC_PKSTATIC,
1023 setupbigedit1(hwnd, IDC_KEYDISPLAY, IDC_PKSTATIC,
1030 EnableWindow(hwnd, 0);
1031 DialogBox(hinst, MAKEINTRESOURCE(213), hwnd, AboutProc);
1032 EnableWindow(hwnd, 1);
1033 SetActiveWindow(hwnd);
1036 if (HIWORD(wParam) == BN_CLICKED ||
1037 HIWORD(wParam) == BN_DOUBLECLICKED) {
1039 WinHelp(hwnd, help_path, HELP_COMMAND,
1040 (DWORD)"JI(`',`puttygen.general')");
1041 requested_help = TRUE;
1046 if (HIWORD(wParam) != BN_CLICKED &&
1047 HIWORD(wParam) != BN_DOUBLECLICKED)
1050 (struct MainDlgState *) GetWindowLong(hwnd, GWL_USERDATA);
1051 if (!state->generation_thread_exists) {
1053 state->keysize = GetDlgItemInt(hwnd, IDC_BITS, &ok, FALSE);
1055 state->keysize = DEFAULT_KEYSIZE;
1056 /* If we ever introduce a new key type, check it here! */
1057 state->ssh2 = !IsDlgButtonChecked(hwnd, IDC_KEYSSH1);
1058 state->is_dsa = IsDlgButtonChecked(hwnd, IDC_KEYSSH2DSA);
1059 if (state->keysize < 256) {
1060 int ret = MessageBox(hwnd,
1061 "PuTTYgen will not generate a key"
1062 " smaller than 256 bits.\n"
1063 "Key length reset to 256. Continue?",
1065 MB_ICONWARNING | MB_OKCANCEL);
1068 state->keysize = 256;
1069 SetDlgItemInt(hwnd, IDC_BITS, 256, FALSE);
1071 ui_set_state(hwnd, state, 1);
1072 SetDlgItemText(hwnd, IDC_GENERATING, entropy_msg);
1073 state->key_exists = FALSE;
1074 state->collecting_entropy = TRUE;
1077 * My brief statistical tests on mouse movements
1078 * suggest that there are about 2.5 bits of
1079 * randomness in the x position, 2.5 in the y
1080 * position, and 1.7 in the message time, making
1081 * 5.7 bits of unpredictability per mouse movement.
1082 * However, other people have told me it's far less
1083 * than that, so I'm going to be stupidly cautious
1084 * and knock that down to a nice round 2. With this
1085 * method, we require two words per mouse movement,
1086 * so with 2 bits per mouse movement we expect 2
1087 * bits every 2 words.
1089 state->entropy_required = (state->keysize / 2) * 2;
1090 state->entropy_got = 0;
1091 state->entropy_size = (state->entropy_required *
1093 state->entropy = snewn(state->entropy_required, unsigned);
1095 SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETRANGE, 0,
1096 MAKELPARAM(0, state->entropy_required));
1097 SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETPOS, 0, 0);
1101 case IDC_EXPORT_OPENSSH:
1102 case IDC_EXPORT_SSHCOM:
1103 if (HIWORD(wParam) != BN_CLICKED)
1106 (struct MainDlgState *) GetWindowLong(hwnd, GWL_USERDATA);
1107 if (state->key_exists) {
1108 char filename[FILENAME_MAX];
1109 char passphrase[PASSPHRASE_MAXLEN];
1110 char passphrase2[PASSPHRASE_MAXLEN];
1114 realtype = SSH_KEYTYPE_SSH2;
1116 realtype = SSH_KEYTYPE_SSH1;
1118 if (LOWORD(wParam) == IDC_EXPORT_OPENSSH)
1119 type = SSH_KEYTYPE_OPENSSH;
1120 else if (LOWORD(wParam) == IDC_EXPORT_SSHCOM)
1121 type = SSH_KEYTYPE_SSHCOM;
1125 if (type != realtype &&
1126 import_target_type(type) != realtype) {
1128 sprintf(msg, "Cannot export an SSH-%d key in an SSH-%d"
1129 " format", (state->ssh2 ? 2 : 1),
1130 (state->ssh2 ? 1 : 2));
1131 MessageBox(hwnd, msg,
1132 "PuTTYgen Error", MB_OK | MB_ICONERROR);
1136 GetDlgItemText(hwnd, IDC_PASSPHRASE1EDIT,
1137 passphrase, sizeof(passphrase));
1138 GetDlgItemText(hwnd, IDC_PASSPHRASE2EDIT,
1139 passphrase2, sizeof(passphrase2));
1140 if (strcmp(passphrase, passphrase2)) {
1142 "The two passphrases given do not match.",
1143 "PuTTYgen Error", MB_OK | MB_ICONERROR);
1148 ret = MessageBox(hwnd,
1149 "Are you sure you want to save this key\n"
1150 "without a passphrase to protect it?",
1152 MB_YESNO | MB_ICONWARNING);
1156 if (prompt_keyfile(hwnd, "Save private key as:",
1157 filename, 1, (type == realtype))) {
1159 FILE *fp = fopen(filename, "r");
1163 buffer = dupprintf("Overwrite existing file\n%s?",
1165 ret = MessageBox(hwnd, buffer, "PuTTYgen Warning",
1166 MB_YESNO | MB_ICONWARNING);
1173 Filename fn = filename_from_str(filename);
1174 if (type != realtype)
1175 ret = export_ssh2(&fn, type, &state->ssh2key,
1176 *passphrase ? passphrase : NULL);
1178 ret = ssh2_save_userkey(&fn, &state->ssh2key,
1179 *passphrase ? passphrase :
1182 Filename fn = filename_from_str(filename);
1183 if (type != realtype)
1184 ret = export_ssh1(&fn, type, &state->key,
1185 *passphrase ? passphrase : NULL);
1187 ret = saversakey(&fn, &state->key,
1188 *passphrase ? passphrase : NULL);
1191 MessageBox(hwnd, "Unable to save key file",
1192 "PuTTYgen Error", MB_OK | MB_ICONERROR);
1198 if (HIWORD(wParam) != BN_CLICKED)
1201 (struct MainDlgState *) GetWindowLong(hwnd, GWL_USERDATA);
1202 if (state->key_exists) {
1203 char filename[FILENAME_MAX];
1204 if (prompt_keyfile(hwnd, "Save public key as:",
1207 FILE *fp = fopen(filename, "r");
1211 buffer = dupprintf("Overwrite existing file\n%s?",
1213 ret = MessageBox(hwnd, buffer, "PuTTYgen Warning",
1214 MB_YESNO | MB_ICONWARNING);
1220 ret = save_ssh2_pubkey(filename, &state->ssh2key);
1222 ret = save_ssh1_pubkey(filename, &state->key);
1225 MessageBox(hwnd, "Unable to save key file",
1226 "PuTTYgen Error", MB_OK | MB_ICONERROR);
1233 if (HIWORD(wParam) != BN_CLICKED)
1236 (struct MainDlgState *) GetWindowLong(hwnd, GWL_USERDATA);
1237 if (!state->generation_thread_exists) {
1238 char filename[FILENAME_MAX];
1239 if (prompt_keyfile(hwnd, "Load private key:",
1240 filename, 0, LOWORD(wParam)==IDC_LOAD))
1241 load_key_file(hwnd, state, filename_from_str(filename),
1242 LOWORD(wParam) != IDC_LOAD);
1248 state = (struct MainDlgState *) GetWindowLong(hwnd, GWL_USERDATA);
1249 state->generation_thread_exists = FALSE;
1250 state->key_exists = TRUE;
1251 SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETRANGE, 0,
1252 MAKELPARAM(0, PROGRESSRANGE));
1253 SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETPOS, PROGRESSRANGE, 0);
1255 if (state->is_dsa) {
1256 state->ssh2key.data = &state->dsskey;
1257 state->ssh2key.alg = &ssh_dss;
1259 state->ssh2key.data = &state->key;
1260 state->ssh2key.alg = &ssh_rsa;
1262 state->commentptr = &state->ssh2key.comment;
1264 state->commentptr = &state->key.comment;
1267 * Invent a comment for the key. We'll do this by including
1268 * the date in it. This will be so horrifyingly ugly that
1269 * the user will immediately want to change it, which is
1272 *state->commentptr = snewn(30, char);
1277 strftime(*state->commentptr, 30, "dsa-key-%Y%m%d", &tm);
1279 strftime(*state->commentptr, 30, "rsa-key-%Y%m%d", &tm);
1283 * Now update the key controls with all the key data.
1288 * Blank passphrase, initially. This isn't dangerous,
1289 * because we will warn (Are You Sure?) before allowing
1290 * the user to save an unprotected private key.
1292 SetDlgItemText(hwnd, IDC_PASSPHRASE1EDIT, "");
1293 SetDlgItemText(hwnd, IDC_PASSPHRASE2EDIT, "");
1297 SetDlgItemText(hwnd, IDC_COMMENTEDIT, *state->commentptr);
1299 * Set the key fingerprint.
1301 savecomment = *state->commentptr;
1302 *state->commentptr = NULL;
1305 fp = state->ssh2key.alg->fingerprint(state->ssh2key.data);
1306 SetDlgItemText(hwnd, IDC_FINGERPRINT, fp);
1310 rsa_fingerprint(buf, sizeof(buf), &state->key);
1311 SetDlgItemText(hwnd, IDC_FINGERPRINT, buf);
1313 *state->commentptr = savecomment;
1315 * Construct a decimal representation of the key, for
1316 * pasting into .ssh/authorized_keys or
1317 * .ssh/authorized_keys2 on a Unix box.
1320 setupbigedit2(hwnd, IDC_KEYDISPLAY,
1321 IDC_PKSTATIC, &state->ssh2key);
1323 setupbigedit1(hwnd, IDC_KEYDISPLAY,
1324 IDC_PKSTATIC, &state->key);
1328 * Finally, hide the progress bar and show the key data.
1330 ui_set_state(hwnd, state, 2);
1334 int id = ((LPHELPINFO)lParam)->iCtrlId;
1337 case IDC_GENERATING:
1341 topic = "puttygen.generate"; break;
1343 case IDC_KEYDISPLAY:
1344 topic = "puttygen.pastekey"; break;
1346 case IDC_FINGERPRINT:
1347 topic = "puttygen.fingerprint"; break;
1348 case IDC_COMMENTSTATIC:
1349 case IDC_COMMENTEDIT:
1350 topic = "puttygen.comment"; break;
1351 case IDC_PASSPHRASE1STATIC:
1352 case IDC_PASSPHRASE1EDIT:
1353 case IDC_PASSPHRASE2STATIC:
1354 case IDC_PASSPHRASE2EDIT:
1355 topic = "puttygen.passphrase"; break;
1356 case IDC_LOADSTATIC:
1358 topic = "puttygen.load"; break;
1359 case IDC_SAVESTATIC:
1361 topic = "puttygen.savepriv"; break;
1363 topic = "puttygen.savepub"; break;
1364 case IDC_TYPESTATIC:
1366 case IDC_KEYSSH2RSA:
1367 case IDC_KEYSSH2DSA:
1368 topic = "puttygen.keytype"; break;
1369 case IDC_BITSSTATIC:
1371 topic = "puttygen.bits"; break;
1373 case IDC_EXPORT_OPENSSH:
1374 case IDC_EXPORT_SSHCOM:
1375 topic = "puttygen.conversions"; break;
1378 char *cmd = dupprintf("JI(`',`%s')", topic);
1379 WinHelp(hwnd, help_path, HELP_COMMAND, (DWORD)cmd);
1381 requested_help = TRUE;
1388 state = (struct MainDlgState *) GetWindowLong(hwnd, GWL_USERDATA);
1390 if (requested_help) {
1391 WinHelp(hwnd, help_path, HELP_QUIT, 0);
1392 requested_help = FALSE;
1400 void cleanup_exit(int code) { exit(code); }
1402 int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
1407 InitCommonControls();
1412 * See if we can find our Help file.
1415 char b[2048], *p, *q, *r;
1417 GetModuleFileName(NULL, b, sizeof(b) - 1);
1419 p = strrchr(b, '\\');
1420 if (p && p >= r) r = p+1;
1421 q = strrchr(b, ':');
1422 if (q && q >= r) r = q+1;
1423 strcpy(r, PUTTY_HELP_FILE);
1424 if ( (fp = fopen(b, "r")) != NULL) {
1425 help_path = dupstr(b);
1431 split_into_argv(cmdline, &argc, &argv, NULL);
1434 if (!strcmp(argv[0], "-pgpfp")) {
1439 * Assume the first argument to be a private key file, and
1440 * attempt to load it.
1442 cmdline_keyfile = argv[0];
1447 return DialogBox(hinst, MAKEINTRESOURCE(201), NULL,
1448 MainDlgProc) != IDOK;