Word-by-word (double-click) selection now spans line breaks if the
[sgt/putty] / puttygen.c
CommitLineData
6e522441 1/*
2 * PuTTY key generation front end.
3 */
4
5#include <windows.h>
6#include <commctrl.h>
7#include <time.h>
6e522441 8#include <stdio.h>
49bad831 9#include <stdlib.h>
6e522441 10
11#define PUTTY_DO_GLOBALS
12
13#include "putty.h"
14#include "ssh.h"
15#include "winstuff.h"
16
17#define WM_DONEKEY (WM_XUSER + 1)
18
0c2986d0 19#define DEFAULT_KEYSIZE 1024
6e522441 20
6e522441 21/* ----------------------------------------------------------------------
22 * Progress report code. This is really horrible :-)
23 */
5c72ca61 24#define PROGRESSRANGE 65535
25#define MAXPHASE 5
6e522441 26struct progress {
5c72ca61 27 int nphases;
28 struct {
29 int exponential;
30 unsigned startpoint, total;
31 unsigned param, current, n; /* if exponential */
32 unsigned mult; /* if linear */
33 } phases[MAXPHASE];
34 unsigned total, divisor, range;
6e522441 35 HWND progbar;
36};
37
5c72ca61 38static void progress_update(void *param, int action, int phase, int iprogress)
32874aea 39{
40 struct progress *p = (struct progress *) param;
6e522441 41 unsigned progress = iprogress;
42 int position;
43
5c72ca61 44 if (action < PROGFN_READY && p->nphases < phase)
45 p->nphases = phase;
46 switch (action) {
47 case PROGFN_LIN_PHASE:
48 p->phases[phase-1].exponential = 0;
49 p->phases[phase-1].mult = p->phases[phase].total / progress;
50 break;
51 case PROGFN_EXP_PHASE:
52 p->phases[phase-1].exponential = 1;
53 p->phases[phase-1].param = 0x10000 + progress;
54 p->phases[phase-1].current = p->phases[phase-1].total;
55 p->phases[phase-1].n = 0;
32874aea 56 break;
5c72ca61 57 case PROGFN_PHASE_EXTENT:
58 p->phases[phase-1].total = progress;
59 break;
60 case PROGFN_READY:
61 {
62 unsigned total = 0;
63 int i;
64 for (i = 0; i < p->nphases; i++) {
65 p->phases[i].startpoint = total;
66 total += p->phases[i].total;
67 }
68 p->total = total;
69 p->divisor = ((p->total + PROGRESSRANGE - 1) / PROGRESSRANGE);
70 p->range = p->total / p->divisor;
71 SendMessage(p->progbar, PBM_SETRANGE, 0, MAKELPARAM(0, p->range));
32874aea 72 }
32874aea 73 break;
5c72ca61 74 case PROGFN_PROGRESS:
75 if (p->phases[phase-1].exponential) {
76 while (p->phases[phase-1].n < progress) {
77 p->phases[phase-1].n++;
78 p->phases[phase-1].current *= p->phases[phase-1].param;
79 p->phases[phase-1].current /= 0x10000;
80 }
81 position = (p->phases[phase-1].startpoint +
82 p->phases[phase-1].total - p->phases[phase-1].current);
83 } else {
84 position = (p->phases[phase-1].startpoint +
85 progress * p->phases[phase-1].mult);
86 }
87 SendMessage(p->progbar, PBM_SETPOS, position / p->divisor, 0);
32874aea 88 break;
6e522441 89 }
6e522441 90}
91
92extern char ver[];
93
94#define PASSPHRASE_MAXLEN 512
95
96struct PassphraseProcStruct {
97 char *passphrase;
98 char *comment;
99};
100
101/*
102 * Dialog-box function for the passphrase box.
103 */
104static int CALLBACK PassphraseProc(HWND hwnd, UINT msg,
32874aea 105 WPARAM wParam, LPARAM lParam)
106{
8e1feb27 107 static char *passphrase = NULL;
6e522441 108 struct PassphraseProcStruct *p;
109
110 switch (msg) {
111 case WM_INITDIALOG:
32874aea 112 SetForegroundWindow(hwnd);
113 SetWindowPos(hwnd, HWND_TOP, 0, 0, 0, 0,
114 SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
65a22376 115
116 /*
117 * Centre the window.
118 */
119 { /* centre the window */
120 RECT rs, rd;
121 HWND hw;
122
123 hw = GetDesktopWindow();
32874aea 124 if (GetWindowRect(hw, &rs) && GetWindowRect(hwnd, &rd))
125 MoveWindow(hwnd,
126 (rs.right + rs.left + rd.left - rd.right) / 2,
127 (rs.bottom + rs.top + rd.top - rd.bottom) / 2,
128 rd.right - rd.left, rd.bottom - rd.top, TRUE);
65a22376 129 }
130
32874aea 131 p = (struct PassphraseProcStruct *) lParam;
132 passphrase = p->passphrase;
133 if (p->comment)
134 SetDlgItemText(hwnd, 101, p->comment);
135 *passphrase = 0;
136 SetDlgItemText(hwnd, 102, passphrase);
137 return 0;
6e522441 138 case WM_COMMAND:
139 switch (LOWORD(wParam)) {
140 case IDOK:
141 if (*passphrase)
32874aea 142 EndDialog(hwnd, 1);
6e522441 143 else
32874aea 144 MessageBeep(0);
6e522441 145 return 0;
146 case IDCANCEL:
32874aea 147 EndDialog(hwnd, 0);
6e522441 148 return 0;
32874aea 149 case 102: /* edit box */
8e1feb27 150 if ((HIWORD(wParam) == EN_CHANGE) && passphrase) {
32874aea 151 GetDlgItemText(hwnd, 102, passphrase,
152 PASSPHRASE_MAXLEN - 1);
153 passphrase[PASSPHRASE_MAXLEN - 1] = '\0';
154 }
155 return 0;
6e522441 156 }
157 return 0;
158 case WM_CLOSE:
32874aea 159 EndDialog(hwnd, 0);
6e522441 160 return 0;
161 }
162 return 0;
163}
164
165/*
166 * Prompt for a key file. Assumes the filename buffer is of size
167 * FILENAME_MAX.
168 */
169static int prompt_keyfile(HWND hwnd, char *dlgtitle,
32874aea 170 char *filename, int save)
171{
6e522441 172 OPENFILENAME of;
173 memset(&of, 0, sizeof(of));
174#ifdef OPENFILENAME_SIZE_VERSION_400
175 of.lStructSize = OPENFILENAME_SIZE_VERSION_400;
176#else
177 of.lStructSize = sizeof(of);
178#endif
179 of.hwndOwner = hwnd;
180 of.lpstrFilter = "All Files\0*\0\0\0";
181 of.lpstrCustomFilter = NULL;
182 of.nFilterIndex = 1;
32874aea 183 of.lpstrFile = filename;
184 *filename = '\0';
6e522441 185 of.nMaxFile = FILENAME_MAX;
186 of.lpstrFileTitle = NULL;
187 of.lpstrInitialDir = NULL;
188 of.lpstrTitle = dlgtitle;
189 of.Flags = 0;
190 if (save)
32874aea 191 return GetSaveFileName(&of);
6e522441 192 else
32874aea 193 return GetOpenFileName(&of);
6e522441 194}
195
196/*
197 * This function is needed to link with the DES code. We need not
198 * have it do anything at all.
199 */
32874aea 200void logevent(char *msg)
201{
6e522441 202}
203
204/*
205 * Dialog-box function for the Licence box.
206 */
32874aea 207static int CALLBACK LicenceProc(HWND hwnd, UINT msg,
208 WPARAM wParam, LPARAM lParam)
209{
6e522441 210 switch (msg) {
211 case WM_INITDIALOG:
65a22376 212 /*
213 * Centre the window.
214 */
215 { /* centre the window */
216 RECT rs, rd;
217 HWND hw;
218
219 hw = GetDesktopWindow();
32874aea 220 if (GetWindowRect(hw, &rs) && GetWindowRect(hwnd, &rd))
221 MoveWindow(hwnd,
222 (rs.right + rs.left + rd.left - rd.right) / 2,
223 (rs.bottom + rs.top + rd.top - rd.bottom) / 2,
224 rd.right - rd.left, rd.bottom - rd.top, TRUE);
65a22376 225 }
226
6e522441 227 return 1;
228 case WM_COMMAND:
229 switch (LOWORD(wParam)) {
230 case IDOK:
32874aea 231 EndDialog(hwnd, 1);
6e522441 232 return 0;
233 }
234 return 0;
235 case WM_CLOSE:
236 EndDialog(hwnd, 1);
237 return 0;
238 }
239 return 0;
240}
241
242/*
243 * Dialog-box function for the About box.
244 */
32874aea 245static int CALLBACK AboutProc(HWND hwnd, UINT msg,
246 WPARAM wParam, LPARAM lParam)
247{
6e522441 248 switch (msg) {
249 case WM_INITDIALOG:
65a22376 250 /*
251 * Centre the window.
252 */
253 { /* centre the window */
254 RECT rs, rd;
255 HWND hw;
256
257 hw = GetDesktopWindow();
32874aea 258 if (GetWindowRect(hw, &rs) && GetWindowRect(hwnd, &rd))
259 MoveWindow(hwnd,
260 (rs.right + rs.left + rd.left - rd.right) / 2,
261 (rs.bottom + rs.top + rd.top - rd.bottom) / 2,
262 rd.right - rd.left, rd.bottom - rd.top, TRUE);
65a22376 263 }
264
32874aea 265 SetDlgItemText(hwnd, 100, ver);
6e522441 266 return 1;
267 case WM_COMMAND:
268 switch (LOWORD(wParam)) {
269 case IDOK:
32874aea 270 EndDialog(hwnd, 1);
6e522441 271 return 0;
272 case 101:
273 EnableWindow(hwnd, 0);
32874aea 274 DialogBox(hinst, MAKEINTRESOURCE(214), NULL, LicenceProc);
6e522441 275 EnableWindow(hwnd, 1);
32874aea 276 SetActiveWindow(hwnd);
6e522441 277 return 0;
278 }
279 return 0;
280 case WM_CLOSE:
32874aea 281 EndDialog(hwnd, 1);
6e522441 282 return 0;
283 }
284 return 0;
285}
286
287/*
288 * Thread to generate a key.
289 */
290struct rsa_key_thread_params {
32874aea 291 HWND progressbar; /* notify this with progress */
292 HWND dialog; /* notify this on completion */
0c2986d0 293 int keysize; /* bits in key */
5c72ca61 294 int is_dsa;
6e522441 295 struct RSAKey *key;
5c72ca61 296 struct dss_key *dsskey;
6e522441 297};
32874aea 298static DWORD WINAPI generate_rsa_key_thread(void *param)
299{
6e522441 300 struct rsa_key_thread_params *params =
32874aea 301 (struct rsa_key_thread_params *) param;
6e522441 302 struct progress prog;
303 prog.progbar = params->progressbar;
304
5c72ca61 305 if (params->is_dsa)
306 dsa_generate(params->dsskey, params->keysize, progress_update, &prog);
307 else
308 rsa_generate(params->key, params->keysize, progress_update, &prog);
6e522441 309
310 PostMessage(params->dialog, WM_DONEKEY, 0, 0);
311
dcbde236 312 sfree(params);
6e522441 313 return 0;
314}
315
316struct MainDlgState {
317 int collecting_entropy;
318 int generation_thread_exists;
319 int key_exists;
320 int entropy_got, entropy_required, entropy_size;
0c2986d0 321 int keysize;
5c72ca61 322 int ssh2, is_dsa;
65a22376 323 char **commentptr; /* points to key.comment or ssh2key.comment */
324 struct ssh2_userkey ssh2key;
6e522441 325 unsigned *entropy;
326 struct RSAKey key;
5c72ca61 327 struct dss_key dsskey;
6e522441 328};
329
32874aea 330static void hidemany(HWND hwnd, const int *ids, int hideit)
331{
6e522441 332 while (*ids) {
32874aea 333 ShowWindow(GetDlgItem(hwnd, *ids++), (hideit ? SW_HIDE : SW_SHOW));
6e522441 334 }
335}
336
af282e3b 337static void setupbigedit1(HWND hwnd, int id, int idstatic, struct RSAKey *key)
32874aea 338{
6e522441 339 char *buffer;
340 char *dec1, *dec2;
341
342 dec1 = bignum_decimal(key->exponent);
343 dec2 = bignum_decimal(key->modulus);
32874aea 344 buffer = smalloc(strlen(dec1) + strlen(dec2) +
345 strlen(key->comment) + 30);
6e522441 346 sprintf(buffer, "%d %s %s %s",
32874aea 347 bignum_bitcount(key->modulus), dec1, dec2, key->comment);
6e522441 348 SetDlgItemText(hwnd, id, buffer);
af282e3b 349 SetDlgItemText(hwnd, idstatic,
350 "&Public key for pasting into authorized_keys file:");
dcbde236 351 sfree(dec1);
352 sfree(dec2);
353 sfree(buffer);
6e522441 354}
355
af282e3b 356static void setupbigedit2(HWND hwnd, int id, int idstatic,
357 struct ssh2_userkey *key)
32874aea 358{
65a22376 359 unsigned char *pub_blob;
360 char *buffer, *p;
44fb739e 361 int pub_len;
65a22376 362 int i;
363
364 pub_blob = key->alg->public_blob(key->data, &pub_len);
32874aea 365 buffer = smalloc(strlen(key->alg->name) + 4 * ((pub_len + 2) / 3) +
65a22376 366 strlen(key->comment) + 3);
367 strcpy(buffer, key->alg->name);
368 p = buffer + strlen(buffer);
369 *p++ = ' ';
370 i = 0;
371 while (i < pub_len) {
32874aea 372 int n = (pub_len - i < 3 ? pub_len - i : 3);
373 base64_encode_atom(pub_blob + i, n, p);
65a22376 374 i += n;
375 p += 4;
376 }
377 *p++ = ' ';
378 strcpy(p, key->comment);
379 SetDlgItemText(hwnd, id, buffer);
af282e3b 380 SetDlgItemText(hwnd, idstatic, "&Public key for pasting into "
381 "OpenSSH authorized_keys2 file:");
65a22376 382 sfree(pub_blob);
32874aea 383 sfree(buffer);
65a22376 384}
385
af282e3b 386static int save_ssh1_pubkey(char *filename, struct RSAKey *key)
387{
388 char *dec1, *dec2;
389 FILE *fp;
390
391 dec1 = bignum_decimal(key->exponent);
392 dec2 = bignum_decimal(key->modulus);
393 fp = fopen(filename, "wb");
394 if (!fp)
395 return 0;
396 fprintf(fp, "%d %s %s %s\n",
397 bignum_bitcount(key->modulus), dec1, dec2, key->comment);
398 fclose(fp);
399 sfree(dec1);
400 sfree(dec2);
401 return 1;
402}
403
404static int save_ssh2_pubkey(char *filename, struct ssh2_userkey *key)
405{
406 unsigned char *pub_blob;
407 char *p;
408 int pub_len;
409 int i, column;
410 FILE *fp;
411
412 pub_blob = key->alg->public_blob(key->data, &pub_len);
413
414 fp = fopen(filename, "wb");
415 if (!fp)
416 return 0;
417
418 fprintf(fp, "---- BEGIN SSH2 PUBLIC KEY ----\n");
419
420 fprintf(fp, "Comment: \"");
421 for (p = key->comment; *p; p++) {
422 if (*p == '\\' || *p == '\"')
423 fputc('\\', fp);
424 fputc(*p, fp);
425 }
426 fprintf(fp, "\"\n");
427
428 i = 0;
429 column = 0;
430 while (i < pub_len) {
431 char buf[5];
432 int n = (pub_len - i < 3 ? pub_len - i : 3);
433 base64_encode_atom(pub_blob + i, n, buf);
434 i += n;
435 buf[4] = '\0';
436 fputs(buf, fp);
437 if (++column >= 16) {
438 fputc('\n', fp);
439 column = 0;
440 }
441 }
442 if (column > 0)
443 fputc('\n', fp);
444
445 fprintf(fp, "---- END SSH2 PUBLIC KEY ----\n");
446 fclose(fp);
447 sfree(pub_blob);
448 return 1;
449}
450
6e522441 451/*
452 * Dialog-box function for the main PuTTYgen dialog box.
453 */
32874aea 454static int CALLBACK MainDlgProc(HWND hwnd, UINT msg,
455 WPARAM wParam, LPARAM lParam)
456{
6e522441 457 enum {
32874aea 458 controlidstart = 100,
459 IDC_TITLE,
460 IDC_BOX_KEY,
461 IDC_NOKEY,
462 IDC_GENERATING,
463 IDC_PROGRESS,
464 IDC_PKSTATIC, IDC_KEYDISPLAY,
465 IDC_FPSTATIC, IDC_FINGERPRINT,
466 IDC_COMMENTSTATIC, IDC_COMMENTEDIT,
467 IDC_PASSPHRASE1STATIC, IDC_PASSPHRASE1EDIT,
468 IDC_PASSPHRASE2STATIC, IDC_PASSPHRASE2EDIT,
469 IDC_BOX_ACTIONS,
470 IDC_GENSTATIC, IDC_GENERATE,
471 IDC_LOADSTATIC, IDC_LOAD,
af282e3b 472 IDC_SAVESTATIC, IDC_SAVE, IDC_SAVEPUB,
32874aea 473 IDC_BOX_PARAMS,
5c72ca61 474 IDC_TYPESTATIC, IDC_KEYSSH1, IDC_KEYSSH2RSA, IDC_KEYSSH2DSA,
32874aea 475 IDC_BITSSTATIC, IDC_BITS,
476 IDC_ABOUT,
6e522441 477 };
478 static const int nokey_ids[] = { IDC_NOKEY, 0 };
32874aea 479 static const int generating_ids[] =
480 { IDC_GENERATING, IDC_PROGRESS, 0 };
6e522441 481 static const int gotkey_ids[] = {
32874aea 482 IDC_PKSTATIC, IDC_KEYDISPLAY,
483 IDC_FPSTATIC, IDC_FINGERPRINT,
484 IDC_COMMENTSTATIC, IDC_COMMENTEDIT,
485 IDC_PASSPHRASE1STATIC, IDC_PASSPHRASE1EDIT,
486 IDC_PASSPHRASE2STATIC, IDC_PASSPHRASE2EDIT, 0
487 };
6e522441 488 static const char generating_msg[] =
32874aea 489 "Please wait while a key is generated...";
6e522441 490 static const char entropy_msg[] =
32874aea 491 "Please generate some randomness by moving the mouse over the blank area.";
6e522441 492 struct MainDlgState *state;
493
494 switch (msg) {
495 case WM_INITDIALOG:
65a22376 496 /*
497 * Centre the window.
498 */
499 { /* centre the window */
500 RECT rs, rd;
501 HWND hw;
502
503 hw = GetDesktopWindow();
32874aea 504 if (GetWindowRect(hw, &rs) && GetWindowRect(hwnd, &rd))
505 MoveWindow(hwnd,
506 (rs.right + rs.left + rd.left - rd.right) / 2,
507 (rs.bottom + rs.top + rd.top - rd.bottom) / 2,
508 rd.right - rd.left, rd.bottom - rd.top, TRUE);
65a22376 509 }
510
32874aea 511 state = smalloc(sizeof(*state));
512 state->generation_thread_exists = FALSE;
513 state->collecting_entropy = FALSE;
514 state->entropy = NULL;
515 state->key_exists = FALSE;
516 SetWindowLong(hwnd, GWL_USERDATA, (LONG) state);
517 {
518 struct ctlpos cp, cp2;
6e522441 519
5c72ca61 520 /* Accelerators used: acglops1rbd */
a5470c60 521
32874aea 522 ctlposinit(&cp, hwnd, 10, 10, 10);
523 bartitle(&cp, "Public and private key generation for PuTTY",
524 IDC_TITLE);
525 beginbox(&cp, "Key", IDC_BOX_KEY);
526 cp2 = cp;
ca68ebca 527 statictext(&cp2, "No key.", 1, IDC_NOKEY);
32874aea 528 cp2 = cp;
ca68ebca 529 statictext(&cp2, "", 1, IDC_GENERATING);
32874aea 530 progressbar(&cp2, IDC_PROGRESS);
531 bigeditctrl(&cp,
532 "&Public key for pasting into authorized_keys file:",
533 IDC_PKSTATIC, IDC_KEYDISPLAY, 7);
534 SendDlgItemMessage(hwnd, IDC_KEYDISPLAY, EM_SETREADONLY, 1, 0);
535 staticedit(&cp, "Key fingerprint:", IDC_FPSTATIC,
536 IDC_FINGERPRINT, 75);
537 SendDlgItemMessage(hwnd, IDC_FINGERPRINT, EM_SETREADONLY, 1,
538 0);
539 staticedit(&cp, "Key &comment:", IDC_COMMENTSTATIC,
540 IDC_COMMENTEDIT, 75);
541 staticpassedit(&cp, "Key p&assphrase:", IDC_PASSPHRASE1STATIC,
542 IDC_PASSPHRASE1EDIT, 75);
543 staticpassedit(&cp, "C&onfirm passphrase:",
544 IDC_PASSPHRASE2STATIC, IDC_PASSPHRASE2EDIT, 75);
545 endbox(&cp);
546 beginbox(&cp, "Actions", IDC_BOX_ACTIONS);
547 staticbtn(&cp, "Generate a public/private key pair",
548 IDC_GENSTATIC, "&Generate", IDC_GENERATE);
549 staticbtn(&cp, "Load an existing private key file",
550 IDC_LOADSTATIC, "&Load", IDC_LOAD);
af282e3b 551 static2btn(&cp, "Save the generated key", IDC_SAVESTATIC,
552 "Save p&ublic key", IDC_SAVEPUB,
553 "&Save private key", IDC_SAVE);
32874aea 554 endbox(&cp);
555 beginbox(&cp, "Parameters", IDC_BOX_PARAMS);
5c72ca61 556 radioline(&cp, "Type of key to generate:", IDC_TYPESTATIC, 3,
32874aea 557 "SSH&1 (RSA)", IDC_KEYSSH1,
5c72ca61 558 "SSH2 &RSA", IDC_KEYSSH2RSA,
559 "SSH2 &DSA", IDC_KEYSSH2DSA, NULL);
32874aea 560 staticedit(&cp, "Number of &bits in a generated key:",
0c2986d0 561 IDC_BITSSTATIC, IDC_BITS, 20);
32874aea 562 endbox(&cp);
563 }
00060c72 564 CheckRadioButton(hwnd, IDC_KEYSSH1, IDC_KEYSSH2DSA, IDC_KEYSSH1);
0c2986d0 565 SetDlgItemInt(hwnd, IDC_BITS, DEFAULT_KEYSIZE, FALSE);
566
32874aea 567 /*
568 * Initially, hide the progress bar and the key display,
569 * and show the no-key display. Also disable the Save
af282e3b 570 * buttons, because with no key we obviously can't save
32874aea 571 * anything.
572 */
573 hidemany(hwnd, nokey_ids, FALSE);
574 hidemany(hwnd, generating_ids, TRUE);
575 hidemany(hwnd, gotkey_ids, TRUE);
576 EnableWindow(GetDlgItem(hwnd, IDC_SAVE), 0);
af282e3b 577 EnableWindow(GetDlgItem(hwnd, IDC_SAVEPUB), 0);
6e522441 578
579 return 1;
580 case WM_MOUSEMOVE:
32874aea 581 state = (struct MainDlgState *) GetWindowLong(hwnd, GWL_USERDATA);
582 if (state->collecting_entropy &&
583 state->entropy && state->entropy_got < state->entropy_required) {
584 state->entropy[state->entropy_got++] = lParam;
585 state->entropy[state->entropy_got++] = GetMessageTime();
586 SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETPOS,
587 state->entropy_got, 0);
588 if (state->entropy_got >= state->entropy_required) {
589 struct rsa_key_thread_params *params;
590 DWORD threadid;
591
592 /*
593 * Seed the entropy pool
594 */
595 random_add_heavynoise(state->entropy, state->entropy_size);
596 memset(state->entropy, 0, state->entropy_size);
597 sfree(state->entropy);
598 state->collecting_entropy = FALSE;
599
600 SetDlgItemText(hwnd, IDC_GENERATING, generating_msg);
601 SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETRANGE, 0,
602 MAKELPARAM(0, PROGRESSRANGE));
603 SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETPOS, 0, 0);
604
605 params = smalloc(sizeof(*params));
606 params->progressbar = GetDlgItem(hwnd, IDC_PROGRESS);
607 params->dialog = hwnd;
0c2986d0 608 params->keysize = state->keysize;
5c72ca61 609 params->is_dsa = state->is_dsa;
32874aea 610 params->key = &state->key;
5c72ca61 611 params->dsskey = &state->dsskey;
32874aea 612
613 if (!CreateThread(NULL, 0, generate_rsa_key_thread,
614 params, 0, &threadid)) {
615 MessageBox(hwnd, "Out of thread resources",
616 "Key generation error",
617 MB_OK | MB_ICONERROR);
618 sfree(params);
619 } else {
620 state->generation_thread_exists = TRUE;
621 }
622 }
623 }
624 break;
6e522441 625 case WM_COMMAND:
626 switch (LOWORD(wParam)) {
627 case IDC_COMMENTEDIT:
628 if (HIWORD(wParam) == EN_CHANGE) {
32874aea 629 state = (struct MainDlgState *)
630 GetWindowLong(hwnd, GWL_USERDATA);
631 if (state->key_exists) {
632 HWND editctl = GetDlgItem(hwnd, IDC_COMMENTEDIT);
633 int len = GetWindowTextLength(editctl);
634 if (*state->commentptr)
635 sfree(*state->commentptr);
636 *state->commentptr = smalloc(len + 1);
637 GetWindowText(editctl, *state->commentptr, len + 1);
2e8324fc 638 if (state->ssh2) {
af282e3b 639 setupbigedit2(hwnd, IDC_KEYDISPLAY, IDC_PKSTATIC,
32874aea 640 &state->ssh2key);
2e8324fc 641 } else {
af282e3b 642 setupbigedit1(hwnd, IDC_KEYDISPLAY, IDC_PKSTATIC,
643 &state->key);
2e8324fc 644 }
645 }
646 }
6e522441 647 break;
648 case IDC_ABOUT:
649 EnableWindow(hwnd, 0);
32874aea 650 DialogBox(hinst, MAKEINTRESOURCE(213), NULL, AboutProc);
6e522441 651 EnableWindow(hwnd, 1);
32874aea 652 SetActiveWindow(hwnd);
6e522441 653 return 0;
32874aea 654 case IDC_GENERATE:
655 state =
656 (struct MainDlgState *) GetWindowLong(hwnd, GWL_USERDATA);
657 if (!state->generation_thread_exists) {
658 BOOL ok;
659 state->keysize = GetDlgItemInt(hwnd, IDC_BITS, &ok, FALSE);
660 if (!ok)
661 state->keysize = DEFAULT_KEYSIZE;
65a22376 662 /* If we ever introduce a new key type, check it here! */
663 state->ssh2 = !IsDlgButtonChecked(hwnd, IDC_KEYSSH1);
5c72ca61 664 state->is_dsa = IsDlgButtonChecked(hwnd, IDC_KEYSSH2DSA);
32874aea 665 if (state->keysize < 256) {
666 int ret = MessageBox(hwnd,
667 "PuTTYgen will not generate a key"
668 " smaller than 256 bits.\n"
669 "Key length reset to 256. Continue?",
670 "PuTTYgen Warning",
671 MB_ICONWARNING | MB_OKCANCEL);
672 if (ret != IDOK)
673 break;
674 state->keysize = 256;
675 SetDlgItemInt(hwnd, IDC_BITS, 256, FALSE);
676 }
677 hidemany(hwnd, nokey_ids, TRUE);
678 hidemany(hwnd, generating_ids, FALSE);
679 hidemany(hwnd, gotkey_ids, TRUE);
680 EnableWindow(GetDlgItem(hwnd, IDC_GENERATE), 0);
681 EnableWindow(GetDlgItem(hwnd, IDC_LOAD), 0);
682 EnableWindow(GetDlgItem(hwnd, IDC_SAVE), 0);
af282e3b 683 EnableWindow(GetDlgItem(hwnd, IDC_SAVEPUB), 0);
684 EnableWindow(GetDlgItem(hwnd, IDC_KEYSSH1), 0);
685 EnableWindow(GetDlgItem(hwnd, IDC_KEYSSH2RSA), 0);
00060c72 686 EnableWindow(GetDlgItem(hwnd, IDC_KEYSSH2DSA), 0);
af282e3b 687 EnableWindow(GetDlgItem(hwnd, IDC_BITS), 0);
32874aea 688 state->key_exists = FALSE;
689 SetDlgItemText(hwnd, IDC_GENERATING, entropy_msg);
690 state->collecting_entropy = TRUE;
691
692 /*
693 * My brief statistical tests on mouse movements
694 * suggest that there are about 2.5 bits of
695 * randomness in the x position, 2.5 in the y
696 * position, and 1.7 in the message time, making
697 * 5.7 bits of unpredictability per mouse movement.
698 * However, other people have told me it's far less
699 * than that, so I'm going to be stupidly cautious
700 * and knock that down to a nice round 2. With this
701 * method, we require two words per mouse movement,
702 * so with 2 bits per mouse movement we expect 2
703 * bits every 2 words.
704 */
705 state->entropy_required = (state->keysize / 2) * 2;
706 state->entropy_got = 0;
707 state->entropy_size = (state->entropy_required *
708 sizeof(*state->entropy));
709 state->entropy = smalloc(state->entropy_size);
710
711 SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETRANGE, 0,
712 MAKELPARAM(0, state->entropy_required));
713 SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETPOS, 0, 0);
714 }
715 break;
716 case IDC_SAVE:
717 state =
718 (struct MainDlgState *) GetWindowLong(hwnd, GWL_USERDATA);
719 if (state->key_exists) {
720 char filename[FILENAME_MAX];
721 char passphrase[PASSPHRASE_MAXLEN];
722 char passphrase2[PASSPHRASE_MAXLEN];
723 GetDlgItemText(hwnd, IDC_PASSPHRASE1EDIT,
724 passphrase, sizeof(passphrase));
725 GetDlgItemText(hwnd, IDC_PASSPHRASE2EDIT,
726 passphrase2, sizeof(passphrase2));
a5470c60 727 if (strcmp(passphrase, passphrase2)) {
32874aea 728 MessageBox(hwnd,
a5470c60 729 "The two passphrases given do not match.",
32874aea 730 "PuTTYgen Error", MB_OK | MB_ICONERROR);
a5470c60 731 break;
732 }
32874aea 733 if (!*passphrase) {
734 int ret;
735 ret = MessageBox(hwnd,
736 "Are you sure you want to save this key\n"
737 "without a passphrase to protect it?",
738 "PuTTYgen Warning",
739 MB_YESNO | MB_ICONWARNING);
740 if (ret != IDYES)
741 break;
742 }
743 if (prompt_keyfile(hwnd, "Save private key as:",
744 filename, 1)) {
bdea0743 745 int ret;
18790478 746 FILE *fp = fopen(filename, "r");
747 if (fp) {
32874aea 748 char buffer[FILENAME_MAX + 80];
18790478 749 fclose(fp);
750 sprintf(buffer, "Overwrite existing file\n%.*s?",
751 FILENAME_MAX, filename);
752 ret = MessageBox(hwnd, buffer, "PuTTYgen Warning",
753 MB_YESNO | MB_ICONWARNING);
754 if (ret != IDYES)
755 break;
756 }
65a22376 757 if (state->ssh2) {
758 ret = ssh2_save_userkey(filename, &state->ssh2key,
32874aea 759 *passphrase ? passphrase :
760 NULL);
65a22376 761 } else {
762 ret = saversakey(filename, &state->key,
763 *passphrase ? passphrase : NULL);
764 }
bdea0743 765 if (ret <= 0) {
766 MessageBox(hwnd, "Unable to save key file",
32874aea 767 "PuTTYgen Error", MB_OK | MB_ICONERROR);
bdea0743 768 }
32874aea 769 }
770 }
771 break;
af282e3b 772 case IDC_SAVEPUB:
773 state =
774 (struct MainDlgState *) GetWindowLong(hwnd, GWL_USERDATA);
775 if (state->key_exists) {
776 char filename[FILENAME_MAX];
777 if (prompt_keyfile(hwnd, "Save public key as:",
778 filename, 1)) {
779 int ret;
780 FILE *fp = fopen(filename, "r");
781 if (fp) {
782 char buffer[FILENAME_MAX + 80];
783 fclose(fp);
784 sprintf(buffer, "Overwrite existing file\n%.*s?",
785 FILENAME_MAX, filename);
786 ret = MessageBox(hwnd, buffer, "PuTTYgen Warning",
787 MB_YESNO | MB_ICONWARNING);
788 if (ret != IDYES)
789 break;
790 }
791 if (state->ssh2) {
792 ret = save_ssh2_pubkey(filename, &state->ssh2key);
793 } else {
794 ret = save_ssh1_pubkey(filename, &state->key);
795 }
796 if (ret <= 0) {
797 MessageBox(hwnd, "Unable to save key file",
798 "PuTTYgen Error", MB_OK | MB_ICONERROR);
799 }
800 }
801 }
802 break;
32874aea 803 case IDC_LOAD:
804 state =
805 (struct MainDlgState *) GetWindowLong(hwnd, GWL_USERDATA);
806 if (!state->generation_thread_exists) {
807 char filename[FILENAME_MAX];
808 if (prompt_keyfile(hwnd, "Load private key:", filename, 0)) {
809 char passphrase[PASSPHRASE_MAXLEN];
810 int needs_pass;
65a22376 811 int ver;
32874aea 812 int ret;
813 char *comment;
814 struct PassphraseProcStruct pps;
815 struct RSAKey newkey1;
2d466ffd 816 struct ssh2_userkey *newkey2 = NULL;
65a22376 817
818 ver = keyfile_version(filename);
819 if (ver == 0) {
32874aea 820 MessageBox(NULL, "Couldn't load private key.",
821 "PuTTYgen Error", MB_OK | MB_ICONERROR);
65a22376 822 break;
823 }
6e522441 824
65a22376 825 comment = NULL;
826 if (ver == 1)
827 needs_pass = rsakey_encrypted(filename, &comment);
828 else
32874aea 829 needs_pass =
830 ssh2_userkey_encrypted(filename, &comment);
831 pps.passphrase = passphrase;
832 pps.comment = comment;
833 do {
834 if (needs_pass) {
835 int dlgret;
836 dlgret = DialogBoxParam(hinst,
837 MAKEINTRESOURCE(210),
838 NULL, PassphraseProc,
839 (LPARAM) & pps);
840 if (!dlgret) {
841 ret = -2;
842 break;
843 }
844 } else
845 *passphrase = '\0';
65a22376 846 if (ver == 1)
32874aea 847 ret =
848 loadrsakey(filename, &newkey1, passphrase);
65a22376 849 else {
32874aea 850 newkey2 =
851 ssh2_load_userkey(filename, passphrase);
65a22376 852 if (newkey2 == SSH2_WRONG_PASSPHRASE)
853 ret = -1;
854 else if (!newkey2)
855 ret = 0;
856 else
857 ret = 1;
858 }
32874aea 859 } while (ret == -1);
860 if (comment)
861 sfree(comment);
862 if (ret == 0) {
863 MessageBox(NULL, "Couldn't load private key.",
864 "PuTTYgen Error", MB_OK | MB_ICONERROR);
865 } else if (ret == 1) {
866 EnableWindow(GetDlgItem(hwnd, IDC_GENERATE), 1);
867 EnableWindow(GetDlgItem(hwnd, IDC_LOAD), 1);
868 EnableWindow(GetDlgItem(hwnd, IDC_SAVE), 1);
af282e3b 869 EnableWindow(GetDlgItem(hwnd, IDC_SAVEPUB), 1);
870 EnableWindow(GetDlgItem(hwnd, IDC_KEYSSH1), 1);
871 EnableWindow(GetDlgItem(hwnd, IDC_KEYSSH2RSA), 1);
00060c72 872 EnableWindow(GetDlgItem(hwnd, IDC_KEYSSH2DSA), 1);
af282e3b 873 EnableWindow(GetDlgItem(hwnd, IDC_BITS), 1);
32874aea 874 /*
875 * Now update the key controls with all the
876 * key data.
877 */
878 {
879 SetDlgItemText(hwnd, IDC_PASSPHRASE1EDIT,
880 passphrase);
881 SetDlgItemText(hwnd, IDC_PASSPHRASE2EDIT,
882 passphrase);
65a22376 883 if (ver == 1) {
884 char buf[128];
885 char *savecomment;
886
887 state->ssh2 = FALSE;
888 state->commentptr = &state->key.comment;
889 state->key = newkey1;
890
891 /*
892 * Set the key fingerprint.
893 */
894 savecomment = state->key.comment;
32874aea 895 state->key.comment = NULL;
896 rsa_fingerprint(buf, sizeof(buf),
897 &state->key);
898 state->key.comment = savecomment;
65a22376 899
900 SetDlgItemText(hwnd, IDC_FINGERPRINT, buf);
901 /*
902 * Construct a decimal representation
903 * of the key, for pasting into
904 * .ssh/authorized_keys on a Unix box.
905 */
32874aea 906 setupbigedit1(hwnd, IDC_KEYDISPLAY,
af282e3b 907 IDC_PKSTATIC, &state->key);
65a22376 908 } else {
909 char *fp;
910 char *savecomment;
911
912 state->ssh2 = TRUE;
32874aea 913 state->commentptr =
914 &state->ssh2key.comment;
915 state->ssh2key = *newkey2; /* structure copy */
65a22376 916 sfree(newkey2);
917
918 savecomment = state->ssh2key.comment;
32874aea 919 state->ssh2key.comment = NULL;
920 fp =
921 state->ssh2key.alg->
922 fingerprint(state->ssh2key.data);
923 state->ssh2key.comment = savecomment;
65a22376 924
925 SetDlgItemText(hwnd, IDC_FINGERPRINT, fp);
926 sfree(fp);
927
32874aea 928 setupbigedit2(hwnd, IDC_KEYDISPLAY,
af282e3b 929 IDC_PKSTATIC, &state->ssh2key);
65a22376 930 }
32874aea 931 SetDlgItemText(hwnd, IDC_COMMENTEDIT,
932 *state->commentptr);
933 }
934 /*
935 * Finally, hide the progress bar and show
936 * the key data.
937 */
938 hidemany(hwnd, nokey_ids, TRUE);
939 hidemany(hwnd, generating_ids, TRUE);
940 hidemany(hwnd, gotkey_ids, FALSE);
941 state->key_exists = TRUE;
942 }
943 }
944 }
945 break;
6e522441 946 }
947 return 0;
948 case WM_DONEKEY:
32874aea 949 state = (struct MainDlgState *) GetWindowLong(hwnd, GWL_USERDATA);
950 state->generation_thread_exists = FALSE;
951 state->key_exists = TRUE;
5c72ca61 952 SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETRANGE, 0,
953 MAKELPARAM(0, PROGRESSRANGE));
954 SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETPOS, PROGRESSRANGE, 0);
32874aea 955 EnableWindow(GetDlgItem(hwnd, IDC_GENERATE), 1);
956 EnableWindow(GetDlgItem(hwnd, IDC_LOAD), 1);
957 EnableWindow(GetDlgItem(hwnd, IDC_SAVE), 1);
af282e3b 958 EnableWindow(GetDlgItem(hwnd, IDC_SAVEPUB), 1);
959 EnableWindow(GetDlgItem(hwnd, IDC_KEYSSH1), 1);
960 EnableWindow(GetDlgItem(hwnd, IDC_KEYSSH2RSA), 1);
00060c72 961 EnableWindow(GetDlgItem(hwnd, IDC_KEYSSH2DSA), 1);
af282e3b 962 EnableWindow(GetDlgItem(hwnd, IDC_BITS), 1);
3b4c5899 963 if (state->ssh2) {
5c72ca61 964 if (state->is_dsa) {
965 state->ssh2key.data = &state->dsskey;
966 state->ssh2key.alg = &ssh_dss;
967 } else {
968 state->ssh2key.data = &state->key;
969 state->ssh2key.alg = &ssh_rsa;
970 }
65a22376 971 state->commentptr = &state->ssh2key.comment;
3b4c5899 972 } else {
65a22376 973 state->commentptr = &state->key.comment;
3b4c5899 974 }
32874aea 975 /*
976 * Invent a comment for the key. We'll do this by including
977 * the date in it. This will be so horrifyingly ugly that
978 * the user will immediately want to change it, which is
979 * what we want :-)
980 */
981 *state->commentptr = smalloc(30);
982 {
983 time_t t;
984 struct tm *tm;
985 time(&t);
986 tm = localtime(&t);
5c72ca61 987 if (state->is_dsa)
988 strftime(*state->commentptr, 30, "dsa-key-%Y%m%d", tm);
989 else
990 strftime(*state->commentptr, 30, "rsa-key-%Y%m%d", tm);
32874aea 991 }
992
993 /*
994 * Now update the key controls with all the key data.
995 */
996 {
65a22376 997 char *savecomment;
32874aea 998 /*
999 * Blank passphrase, initially. This isn't dangerous,
1000 * because we will warn (Are You Sure?) before allowing
1001 * the user to save an unprotected private key.
1002 */
1003 SetDlgItemText(hwnd, IDC_PASSPHRASE1EDIT, "");
1004 SetDlgItemText(hwnd, IDC_PASSPHRASE2EDIT, "");
1005 /*
1006 * Set the comment.
1007 */
1008 SetDlgItemText(hwnd, IDC_COMMENTEDIT, *state->commentptr);
1009 /*
1010 * Set the key fingerprint.
1011 */
65a22376 1012 savecomment = *state->commentptr;
1013 *state->commentptr = NULL;
32874aea 1014 if (state->ssh2) {
65a22376 1015 char *fp;
32874aea 1016 fp = state->ssh2key.alg->fingerprint(state->ssh2key.data);
65a22376 1017 SetDlgItemText(hwnd, IDC_FINGERPRINT, fp);
1018 sfree(fp);
1019 } else {
1020 char buf[128];
32874aea 1021 rsa_fingerprint(buf, sizeof(buf), &state->key);
65a22376 1022 SetDlgItemText(hwnd, IDC_FINGERPRINT, buf);
32874aea 1023 }
65a22376 1024 *state->commentptr = savecomment;
32874aea 1025 /*
1026 * Construct a decimal representation of the key, for
af282e3b 1027 * pasting into .ssh/authorized_keys or
1028 * .ssh/authorized_keys2 on a Unix box.
32874aea 1029 */
65a22376 1030 if (state->ssh2) {
af282e3b 1031 setupbigedit2(hwnd, IDC_KEYDISPLAY,
1032 IDC_PKSTATIC, &state->ssh2key);
65a22376 1033 } else {
af282e3b 1034 setupbigedit1(hwnd, IDC_KEYDISPLAY,
1035 IDC_PKSTATIC, &state->key);
65a22376 1036 }
32874aea 1037 }
1038 /*
1039 * Finally, hide the progress bar and show the key data.
1040 */
1041 hidemany(hwnd, nokey_ids, TRUE);
1042 hidemany(hwnd, generating_ids, TRUE);
1043 hidemany(hwnd, gotkey_ids, FALSE);
1044 break;
6e522441 1045 case WM_CLOSE:
32874aea 1046 state = (struct MainDlgState *) GetWindowLong(hwnd, GWL_USERDATA);
1047 sfree(state);
1048 EndDialog(hwnd, 1);
6e522441 1049 return 0;
1050 }
1051 return 0;
1052}
1053
32874aea 1054int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
1055{
8b8cf871 1056 InitCommonControls();
6e522441 1057 hinst = inst;
1058 random_init();
32874aea 1059 return DialogBox(hinst, MAKEINTRESOURCE(201), NULL,
1060 MainDlgProc) != IDOK;
6e522441 1061}