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