| 1 | /* |
| 2 | * PuTTY key generation front end. |
| 3 | */ |
| 4 | |
| 5 | #include <windows.h> |
| 6 | #include <commctrl.h> |
| 7 | #include <time.h> |
| 8 | #include <stdio.h> |
| 9 | #include <stdlib.h> |
| 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 | |
| 19 | #define DEFAULT_KEYSIZE 1024 |
| 20 | |
| 21 | /* ---------------------------------------------------------------------- |
| 22 | * Progress report code. This is really horrible :-) |
| 23 | */ |
| 24 | #define PHASE1TOTAL 0x10000 |
| 25 | #define PHASE2TOTAL 0x10000 |
| 26 | #define PHASE3TOTAL 0x04000 |
| 27 | #define PHASE1START 0 |
| 28 | #define PHASE2START (PHASE1TOTAL) |
| 29 | #define PHASE3START (PHASE1TOTAL + PHASE2TOTAL) |
| 30 | #define TOTALTOTAL (PHASE1TOTAL + PHASE2TOTAL + PHASE3TOTAL) |
| 31 | #define PROGRESSBIGRANGE 65535 |
| 32 | #define DIVISOR ((TOTALTOTAL + PROGRESSBIGRANGE - 1) / PROGRESSBIGRANGE) |
| 33 | #define PROGRESSRANGE (TOTALTOTAL / DIVISOR) |
| 34 | struct progress { |
| 35 | unsigned phase1param, phase1current, phase1n; |
| 36 | unsigned phase2param, phase2current, phase2n; |
| 37 | unsigned phase3mult; |
| 38 | HWND progbar; |
| 39 | }; |
| 40 | |
| 41 | static void progress_update(void *param, int phase, int iprogress) |
| 42 | { |
| 43 | struct progress *p = (struct progress *) param; |
| 44 | unsigned progress = iprogress; |
| 45 | int position; |
| 46 | |
| 47 | switch (phase) { |
| 48 | case -1: |
| 49 | p->phase1param = 0x10000 + progress; |
| 50 | p->phase1current = 0x10000; |
| 51 | p->phase1n = 0; |
| 52 | return; |
| 53 | case -2: |
| 54 | p->phase2param = 0x10000 + progress; |
| 55 | p->phase2current = 0x10000; |
| 56 | p->phase2n = 0; |
| 57 | return; |
| 58 | case -3: |
| 59 | p->phase3mult = PHASE3TOTAL / progress; |
| 60 | return; |
| 61 | case 1: |
| 62 | while (p->phase1n < progress) { |
| 63 | p->phase1n++; |
| 64 | p->phase1current *= p->phase1param; |
| 65 | p->phase1current /= 0x10000; |
| 66 | } |
| 67 | position = PHASE1START + 0x10000 - p->phase1current; |
| 68 | break; |
| 69 | case 2: |
| 70 | while (p->phase2n < progress) { |
| 71 | p->phase2n++; |
| 72 | p->phase2current *= p->phase2param; |
| 73 | p->phase2current /= 0x10000; |
| 74 | } |
| 75 | position = PHASE2START + 0x10000 - p->phase2current; |
| 76 | break; |
| 77 | case 3: |
| 78 | position = PHASE3START + progress * p->phase3mult; |
| 79 | break; |
| 80 | default: |
| 81 | /* |
| 82 | * Shouldn't happen, but having a default clause placates |
| 83 | * gcc -Wall, which would otherwise complain that |
| 84 | * `position' might be used uninitialised. |
| 85 | */ |
| 86 | return; |
| 87 | } |
| 88 | |
| 89 | SendMessage(p->progbar, PBM_SETPOS, position / DIVISOR, 0); |
| 90 | } |
| 91 | |
| 92 | extern char ver[]; |
| 93 | |
| 94 | #define PASSPHRASE_MAXLEN 512 |
| 95 | |
| 96 | struct PassphraseProcStruct { |
| 97 | char *passphrase; |
| 98 | char *comment; |
| 99 | }; |
| 100 | |
| 101 | /* |
| 102 | * Dialog-box function for the passphrase box. |
| 103 | */ |
| 104 | static int CALLBACK PassphraseProc(HWND hwnd, UINT msg, |
| 105 | WPARAM wParam, LPARAM lParam) |
| 106 | { |
| 107 | static char *passphrase = NULL; |
| 108 | struct PassphraseProcStruct *p; |
| 109 | |
| 110 | switch (msg) { |
| 111 | case WM_INITDIALOG: |
| 112 | SetForegroundWindow(hwnd); |
| 113 | SetWindowPos(hwnd, HWND_TOP, 0, 0, 0, 0, |
| 114 | SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW); |
| 115 | |
| 116 | /* |
| 117 | * Centre the window. |
| 118 | */ |
| 119 | { /* centre the window */ |
| 120 | RECT rs, rd; |
| 121 | HWND hw; |
| 122 | |
| 123 | hw = GetDesktopWindow(); |
| 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); |
| 129 | } |
| 130 | |
| 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; |
| 138 | case WM_COMMAND: |
| 139 | switch (LOWORD(wParam)) { |
| 140 | case IDOK: |
| 141 | if (*passphrase) |
| 142 | EndDialog(hwnd, 1); |
| 143 | else |
| 144 | MessageBeep(0); |
| 145 | return 0; |
| 146 | case IDCANCEL: |
| 147 | EndDialog(hwnd, 0); |
| 148 | return 0; |
| 149 | case 102: /* edit box */ |
| 150 | if ((HIWORD(wParam) == EN_CHANGE) && passphrase) { |
| 151 | GetDlgItemText(hwnd, 102, passphrase, |
| 152 | PASSPHRASE_MAXLEN - 1); |
| 153 | passphrase[PASSPHRASE_MAXLEN - 1] = '\0'; |
| 154 | } |
| 155 | return 0; |
| 156 | } |
| 157 | return 0; |
| 158 | case WM_CLOSE: |
| 159 | EndDialog(hwnd, 0); |
| 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 | */ |
| 169 | static int prompt_keyfile(HWND hwnd, char *dlgtitle, |
| 170 | char *filename, int save) |
| 171 | { |
| 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; |
| 183 | of.lpstrFile = filename; |
| 184 | *filename = '\0'; |
| 185 | of.nMaxFile = FILENAME_MAX; |
| 186 | of.lpstrFileTitle = NULL; |
| 187 | of.lpstrInitialDir = NULL; |
| 188 | of.lpstrTitle = dlgtitle; |
| 189 | of.Flags = 0; |
| 190 | if (save) |
| 191 | return GetSaveFileName(&of); |
| 192 | else |
| 193 | return GetOpenFileName(&of); |
| 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 | */ |
| 200 | void logevent(char *msg) |
| 201 | { |
| 202 | } |
| 203 | |
| 204 | /* |
| 205 | * Dialog-box function for the Licence box. |
| 206 | */ |
| 207 | static int CALLBACK LicenceProc(HWND hwnd, UINT msg, |
| 208 | WPARAM wParam, LPARAM lParam) |
| 209 | { |
| 210 | switch (msg) { |
| 211 | case WM_INITDIALOG: |
| 212 | /* |
| 213 | * Centre the window. |
| 214 | */ |
| 215 | { /* centre the window */ |
| 216 | RECT rs, rd; |
| 217 | HWND hw; |
| 218 | |
| 219 | hw = GetDesktopWindow(); |
| 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); |
| 225 | } |
| 226 | |
| 227 | return 1; |
| 228 | case WM_COMMAND: |
| 229 | switch (LOWORD(wParam)) { |
| 230 | case IDOK: |
| 231 | EndDialog(hwnd, 1); |
| 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 | */ |
| 245 | static int CALLBACK AboutProc(HWND hwnd, UINT msg, |
| 246 | WPARAM wParam, LPARAM lParam) |
| 247 | { |
| 248 | switch (msg) { |
| 249 | case WM_INITDIALOG: |
| 250 | /* |
| 251 | * Centre the window. |
| 252 | */ |
| 253 | { /* centre the window */ |
| 254 | RECT rs, rd; |
| 255 | HWND hw; |
| 256 | |
| 257 | hw = GetDesktopWindow(); |
| 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); |
| 263 | } |
| 264 | |
| 265 | SetDlgItemText(hwnd, 100, ver); |
| 266 | return 1; |
| 267 | case WM_COMMAND: |
| 268 | switch (LOWORD(wParam)) { |
| 269 | case IDOK: |
| 270 | EndDialog(hwnd, 1); |
| 271 | return 0; |
| 272 | case 101: |
| 273 | EnableWindow(hwnd, 0); |
| 274 | DialogBox(hinst, MAKEINTRESOURCE(214), NULL, LicenceProc); |
| 275 | EnableWindow(hwnd, 1); |
| 276 | SetActiveWindow(hwnd); |
| 277 | return 0; |
| 278 | } |
| 279 | return 0; |
| 280 | case WM_CLOSE: |
| 281 | EndDialog(hwnd, 1); |
| 282 | return 0; |
| 283 | } |
| 284 | return 0; |
| 285 | } |
| 286 | |
| 287 | /* |
| 288 | * Thread to generate a key. |
| 289 | */ |
| 290 | struct rsa_key_thread_params { |
| 291 | HWND progressbar; /* notify this with progress */ |
| 292 | HWND dialog; /* notify this on completion */ |
| 293 | int keysize; /* bits in key */ |
| 294 | struct RSAKey *key; |
| 295 | }; |
| 296 | static DWORD WINAPI generate_rsa_key_thread(void *param) |
| 297 | { |
| 298 | struct rsa_key_thread_params *params = |
| 299 | (struct rsa_key_thread_params *) param; |
| 300 | struct progress prog; |
| 301 | prog.progbar = params->progressbar; |
| 302 | |
| 303 | rsa_generate(params->key, params->keysize, progress_update, &prog); |
| 304 | |
| 305 | PostMessage(params->dialog, WM_DONEKEY, 0, 0); |
| 306 | |
| 307 | sfree(params); |
| 308 | return 0; |
| 309 | } |
| 310 | |
| 311 | struct MainDlgState { |
| 312 | int collecting_entropy; |
| 313 | int generation_thread_exists; |
| 314 | int key_exists; |
| 315 | int entropy_got, entropy_required, entropy_size; |
| 316 | int keysize; |
| 317 | int ssh2; |
| 318 | char **commentptr; /* points to key.comment or ssh2key.comment */ |
| 319 | struct ssh2_userkey ssh2key; |
| 320 | unsigned *entropy; |
| 321 | struct RSAKey key; |
| 322 | }; |
| 323 | |
| 324 | static void hidemany(HWND hwnd, const int *ids, int hideit) |
| 325 | { |
| 326 | while (*ids) { |
| 327 | ShowWindow(GetDlgItem(hwnd, *ids++), (hideit ? SW_HIDE : SW_SHOW)); |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | static void setupbigedit1(HWND hwnd, int id, struct RSAKey *key) |
| 332 | { |
| 333 | char *buffer; |
| 334 | char *dec1, *dec2; |
| 335 | |
| 336 | dec1 = bignum_decimal(key->exponent); |
| 337 | dec2 = bignum_decimal(key->modulus); |
| 338 | buffer = smalloc(strlen(dec1) + strlen(dec2) + |
| 339 | strlen(key->comment) + 30); |
| 340 | sprintf(buffer, "%d %s %s %s", |
| 341 | bignum_bitcount(key->modulus), dec1, dec2, key->comment); |
| 342 | SetDlgItemText(hwnd, id, buffer); |
| 343 | sfree(dec1); |
| 344 | sfree(dec2); |
| 345 | sfree(buffer); |
| 346 | } |
| 347 | |
| 348 | static void setupbigedit2(HWND hwnd, int id, struct ssh2_userkey *key) |
| 349 | { |
| 350 | unsigned char *pub_blob; |
| 351 | char *buffer, *p; |
| 352 | int pub_len; |
| 353 | int i; |
| 354 | |
| 355 | pub_blob = key->alg->public_blob(key->data, &pub_len); |
| 356 | buffer = smalloc(strlen(key->alg->name) + 4 * ((pub_len + 2) / 3) + |
| 357 | strlen(key->comment) + 3); |
| 358 | strcpy(buffer, key->alg->name); |
| 359 | p = buffer + strlen(buffer); |
| 360 | *p++ = ' '; |
| 361 | i = 0; |
| 362 | while (i < pub_len) { |
| 363 | int n = (pub_len - i < 3 ? pub_len - i : 3); |
| 364 | base64_encode_atom(pub_blob + i, n, p); |
| 365 | i += n; |
| 366 | p += 4; |
| 367 | } |
| 368 | *p++ = ' '; |
| 369 | strcpy(p, key->comment); |
| 370 | SetDlgItemText(hwnd, id, buffer); |
| 371 | sfree(pub_blob); |
| 372 | sfree(buffer); |
| 373 | } |
| 374 | |
| 375 | /* |
| 376 | * Dialog-box function for the main PuTTYgen dialog box. |
| 377 | */ |
| 378 | static int CALLBACK MainDlgProc(HWND hwnd, UINT msg, |
| 379 | WPARAM wParam, LPARAM lParam) |
| 380 | { |
| 381 | enum { |
| 382 | controlidstart = 100, |
| 383 | IDC_TITLE, |
| 384 | IDC_BOX_KEY, |
| 385 | IDC_NOKEY, |
| 386 | IDC_GENERATING, |
| 387 | IDC_PROGRESS, |
| 388 | IDC_PKSTATIC, IDC_KEYDISPLAY, |
| 389 | IDC_FPSTATIC, IDC_FINGERPRINT, |
| 390 | IDC_COMMENTSTATIC, IDC_COMMENTEDIT, |
| 391 | IDC_PASSPHRASE1STATIC, IDC_PASSPHRASE1EDIT, |
| 392 | IDC_PASSPHRASE2STATIC, IDC_PASSPHRASE2EDIT, |
| 393 | IDC_BOX_ACTIONS, |
| 394 | IDC_GENSTATIC, IDC_GENERATE, |
| 395 | IDC_LOADSTATIC, IDC_LOAD, |
| 396 | IDC_SAVESTATIC, IDC_SAVE, |
| 397 | IDC_BOX_PARAMS, |
| 398 | IDC_TYPESTATIC, IDC_KEYSSH1, IDC_KEYSSH2RSA, |
| 399 | IDC_BITSSTATIC, IDC_BITS, |
| 400 | IDC_ABOUT, |
| 401 | }; |
| 402 | static const int nokey_ids[] = { IDC_NOKEY, 0 }; |
| 403 | static const int generating_ids[] = |
| 404 | { IDC_GENERATING, IDC_PROGRESS, 0 }; |
| 405 | static const int gotkey_ids[] = { |
| 406 | IDC_PKSTATIC, IDC_KEYDISPLAY, |
| 407 | IDC_FPSTATIC, IDC_FINGERPRINT, |
| 408 | IDC_COMMENTSTATIC, IDC_COMMENTEDIT, |
| 409 | IDC_PASSPHRASE1STATIC, IDC_PASSPHRASE1EDIT, |
| 410 | IDC_PASSPHRASE2STATIC, IDC_PASSPHRASE2EDIT, 0 |
| 411 | }; |
| 412 | static const char generating_msg[] = |
| 413 | "Please wait while a key is generated..."; |
| 414 | static const char entropy_msg[] = |
| 415 | "Please generate some randomness by moving the mouse over the blank area."; |
| 416 | struct MainDlgState *state; |
| 417 | |
| 418 | switch (msg) { |
| 419 | case WM_INITDIALOG: |
| 420 | /* |
| 421 | * Centre the window. |
| 422 | */ |
| 423 | { /* centre the window */ |
| 424 | RECT rs, rd; |
| 425 | HWND hw; |
| 426 | |
| 427 | hw = GetDesktopWindow(); |
| 428 | if (GetWindowRect(hw, &rs) && GetWindowRect(hwnd, &rd)) |
| 429 | MoveWindow(hwnd, |
| 430 | (rs.right + rs.left + rd.left - rd.right) / 2, |
| 431 | (rs.bottom + rs.top + rd.top - rd.bottom) / 2, |
| 432 | rd.right - rd.left, rd.bottom - rd.top, TRUE); |
| 433 | } |
| 434 | |
| 435 | state = smalloc(sizeof(*state)); |
| 436 | state->generation_thread_exists = FALSE; |
| 437 | state->collecting_entropy = FALSE; |
| 438 | state->entropy = NULL; |
| 439 | state->key_exists = FALSE; |
| 440 | SetWindowLong(hwnd, GWL_USERDATA, (LONG) state); |
| 441 | { |
| 442 | struct ctlpos cp, cp2; |
| 443 | |
| 444 | /* Accelerators used: acglops */ |
| 445 | |
| 446 | ctlposinit(&cp, hwnd, 10, 10, 10); |
| 447 | bartitle(&cp, "Public and private key generation for PuTTY", |
| 448 | IDC_TITLE); |
| 449 | beginbox(&cp, "Key", IDC_BOX_KEY); |
| 450 | cp2 = cp; |
| 451 | statictext(&cp2, "No key.", IDC_NOKEY); |
| 452 | cp2 = cp; |
| 453 | statictext(&cp2, "", IDC_GENERATING); |
| 454 | progressbar(&cp2, IDC_PROGRESS); |
| 455 | bigeditctrl(&cp, |
| 456 | "&Public key for pasting into authorized_keys file:", |
| 457 | IDC_PKSTATIC, IDC_KEYDISPLAY, 7); |
| 458 | SendDlgItemMessage(hwnd, IDC_KEYDISPLAY, EM_SETREADONLY, 1, 0); |
| 459 | staticedit(&cp, "Key fingerprint:", IDC_FPSTATIC, |
| 460 | IDC_FINGERPRINT, 75); |
| 461 | SendDlgItemMessage(hwnd, IDC_FINGERPRINT, EM_SETREADONLY, 1, |
| 462 | 0); |
| 463 | staticedit(&cp, "Key &comment:", IDC_COMMENTSTATIC, |
| 464 | IDC_COMMENTEDIT, 75); |
| 465 | staticpassedit(&cp, "Key p&assphrase:", IDC_PASSPHRASE1STATIC, |
| 466 | IDC_PASSPHRASE1EDIT, 75); |
| 467 | staticpassedit(&cp, "C&onfirm passphrase:", |
| 468 | IDC_PASSPHRASE2STATIC, IDC_PASSPHRASE2EDIT, 75); |
| 469 | endbox(&cp); |
| 470 | beginbox(&cp, "Actions", IDC_BOX_ACTIONS); |
| 471 | staticbtn(&cp, "Generate a public/private key pair", |
| 472 | IDC_GENSTATIC, "&Generate", IDC_GENERATE); |
| 473 | staticbtn(&cp, "Load an existing private key file", |
| 474 | IDC_LOADSTATIC, "&Load", IDC_LOAD); |
| 475 | staticbtn(&cp, "Save the generated key to a new file", |
| 476 | IDC_SAVESTATIC, "&Save", IDC_SAVE); |
| 477 | endbox(&cp); |
| 478 | beginbox(&cp, "Parameters", IDC_BOX_PARAMS); |
| 479 | radioline(&cp, "Type of key to generate:", IDC_TYPESTATIC, 2, |
| 480 | "SSH&1 (RSA)", IDC_KEYSSH1, |
| 481 | "SSH2 &RSA", IDC_KEYSSH2RSA, NULL); |
| 482 | staticedit(&cp, "Number of &bits in a generated key:", |
| 483 | IDC_BITSSTATIC, IDC_BITS, 20); |
| 484 | endbox(&cp); |
| 485 | } |
| 486 | CheckRadioButton(hwnd, IDC_KEYSSH1, IDC_KEYSSH2RSA, IDC_KEYSSH1); |
| 487 | SetDlgItemInt(hwnd, IDC_BITS, DEFAULT_KEYSIZE, FALSE); |
| 488 | |
| 489 | /* |
| 490 | * Initially, hide the progress bar and the key display, |
| 491 | * and show the no-key display. Also disable the Save |
| 492 | * button, because with no key we obviously can't save |
| 493 | * anything. |
| 494 | */ |
| 495 | hidemany(hwnd, nokey_ids, FALSE); |
| 496 | hidemany(hwnd, generating_ids, TRUE); |
| 497 | hidemany(hwnd, gotkey_ids, TRUE); |
| 498 | EnableWindow(GetDlgItem(hwnd, IDC_SAVE), 0); |
| 499 | |
| 500 | return 1; |
| 501 | case WM_MOUSEMOVE: |
| 502 | state = (struct MainDlgState *) GetWindowLong(hwnd, GWL_USERDATA); |
| 503 | if (state->collecting_entropy && |
| 504 | state->entropy && state->entropy_got < state->entropy_required) { |
| 505 | state->entropy[state->entropy_got++] = lParam; |
| 506 | state->entropy[state->entropy_got++] = GetMessageTime(); |
| 507 | SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETPOS, |
| 508 | state->entropy_got, 0); |
| 509 | if (state->entropy_got >= state->entropy_required) { |
| 510 | struct rsa_key_thread_params *params; |
| 511 | DWORD threadid; |
| 512 | |
| 513 | /* |
| 514 | * Seed the entropy pool |
| 515 | */ |
| 516 | random_add_heavynoise(state->entropy, state->entropy_size); |
| 517 | memset(state->entropy, 0, state->entropy_size); |
| 518 | sfree(state->entropy); |
| 519 | state->collecting_entropy = FALSE; |
| 520 | |
| 521 | SetDlgItemText(hwnd, IDC_GENERATING, generating_msg); |
| 522 | SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETRANGE, 0, |
| 523 | MAKELPARAM(0, PROGRESSRANGE)); |
| 524 | SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETPOS, 0, 0); |
| 525 | |
| 526 | params = smalloc(sizeof(*params)); |
| 527 | params->progressbar = GetDlgItem(hwnd, IDC_PROGRESS); |
| 528 | params->dialog = hwnd; |
| 529 | params->keysize = state->keysize; |
| 530 | params->key = &state->key; |
| 531 | |
| 532 | if (!CreateThread(NULL, 0, generate_rsa_key_thread, |
| 533 | params, 0, &threadid)) { |
| 534 | MessageBox(hwnd, "Out of thread resources", |
| 535 | "Key generation error", |
| 536 | MB_OK | MB_ICONERROR); |
| 537 | sfree(params); |
| 538 | } else { |
| 539 | state->generation_thread_exists = TRUE; |
| 540 | } |
| 541 | } |
| 542 | } |
| 543 | break; |
| 544 | case WM_COMMAND: |
| 545 | switch (LOWORD(wParam)) { |
| 546 | case IDC_COMMENTEDIT: |
| 547 | if (HIWORD(wParam) == EN_CHANGE) { |
| 548 | state = (struct MainDlgState *) |
| 549 | GetWindowLong(hwnd, GWL_USERDATA); |
| 550 | if (state->key_exists) { |
| 551 | HWND editctl = GetDlgItem(hwnd, IDC_COMMENTEDIT); |
| 552 | int len = GetWindowTextLength(editctl); |
| 553 | if (*state->commentptr) |
| 554 | sfree(*state->commentptr); |
| 555 | *state->commentptr = smalloc(len + 1); |
| 556 | GetWindowText(editctl, *state->commentptr, len + 1); |
| 557 | if (state->ssh2) { |
| 558 | setupbigedit2(hwnd, IDC_KEYDISPLAY, |
| 559 | &state->ssh2key); |
| 560 | } else { |
| 561 | setupbigedit1(hwnd, IDC_KEYDISPLAY, &state->key); |
| 562 | } |
| 563 | } |
| 564 | } |
| 565 | break; |
| 566 | case IDC_ABOUT: |
| 567 | EnableWindow(hwnd, 0); |
| 568 | DialogBox(hinst, MAKEINTRESOURCE(213), NULL, AboutProc); |
| 569 | EnableWindow(hwnd, 1); |
| 570 | SetActiveWindow(hwnd); |
| 571 | return 0; |
| 572 | case IDC_GENERATE: |
| 573 | state = |
| 574 | (struct MainDlgState *) GetWindowLong(hwnd, GWL_USERDATA); |
| 575 | if (!state->generation_thread_exists) { |
| 576 | BOOL ok; |
| 577 | state->keysize = GetDlgItemInt(hwnd, IDC_BITS, &ok, FALSE); |
| 578 | if (!ok) |
| 579 | state->keysize = DEFAULT_KEYSIZE; |
| 580 | /* If we ever introduce a new key type, check it here! */ |
| 581 | state->ssh2 = !IsDlgButtonChecked(hwnd, IDC_KEYSSH1); |
| 582 | if (state->keysize < 256) { |
| 583 | int ret = MessageBox(hwnd, |
| 584 | "PuTTYgen will not generate a key" |
| 585 | " smaller than 256 bits.\n" |
| 586 | "Key length reset to 256. Continue?", |
| 587 | "PuTTYgen Warning", |
| 588 | MB_ICONWARNING | MB_OKCANCEL); |
| 589 | if (ret != IDOK) |
| 590 | break; |
| 591 | state->keysize = 256; |
| 592 | SetDlgItemInt(hwnd, IDC_BITS, 256, FALSE); |
| 593 | } |
| 594 | hidemany(hwnd, nokey_ids, TRUE); |
| 595 | hidemany(hwnd, generating_ids, FALSE); |
| 596 | hidemany(hwnd, gotkey_ids, TRUE); |
| 597 | EnableWindow(GetDlgItem(hwnd, IDC_GENERATE), 0); |
| 598 | EnableWindow(GetDlgItem(hwnd, IDC_LOAD), 0); |
| 599 | EnableWindow(GetDlgItem(hwnd, IDC_SAVE), 0); |
| 600 | state->key_exists = FALSE; |
| 601 | SetDlgItemText(hwnd, IDC_GENERATING, entropy_msg); |
| 602 | state->collecting_entropy = TRUE; |
| 603 | |
| 604 | /* |
| 605 | * My brief statistical tests on mouse movements |
| 606 | * suggest that there are about 2.5 bits of |
| 607 | * randomness in the x position, 2.5 in the y |
| 608 | * position, and 1.7 in the message time, making |
| 609 | * 5.7 bits of unpredictability per mouse movement. |
| 610 | * However, other people have told me it's far less |
| 611 | * than that, so I'm going to be stupidly cautious |
| 612 | * and knock that down to a nice round 2. With this |
| 613 | * method, we require two words per mouse movement, |
| 614 | * so with 2 bits per mouse movement we expect 2 |
| 615 | * bits every 2 words. |
| 616 | */ |
| 617 | state->entropy_required = (state->keysize / 2) * 2; |
| 618 | state->entropy_got = 0; |
| 619 | state->entropy_size = (state->entropy_required * |
| 620 | sizeof(*state->entropy)); |
| 621 | state->entropy = smalloc(state->entropy_size); |
| 622 | |
| 623 | SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETRANGE, 0, |
| 624 | MAKELPARAM(0, state->entropy_required)); |
| 625 | SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETPOS, 0, 0); |
| 626 | } |
| 627 | break; |
| 628 | case IDC_SAVE: |
| 629 | state = |
| 630 | (struct MainDlgState *) GetWindowLong(hwnd, GWL_USERDATA); |
| 631 | if (state->key_exists) { |
| 632 | char filename[FILENAME_MAX]; |
| 633 | char passphrase[PASSPHRASE_MAXLEN]; |
| 634 | char passphrase2[PASSPHRASE_MAXLEN]; |
| 635 | GetDlgItemText(hwnd, IDC_PASSPHRASE1EDIT, |
| 636 | passphrase, sizeof(passphrase)); |
| 637 | GetDlgItemText(hwnd, IDC_PASSPHRASE2EDIT, |
| 638 | passphrase2, sizeof(passphrase2)); |
| 639 | if (strcmp(passphrase, passphrase2)) { |
| 640 | MessageBox(hwnd, |
| 641 | "The two passphrases given do not match.", |
| 642 | "PuTTYgen Error", MB_OK | MB_ICONERROR); |
| 643 | break; |
| 644 | } |
| 645 | if (!*passphrase) { |
| 646 | int ret; |
| 647 | ret = MessageBox(hwnd, |
| 648 | "Are you sure you want to save this key\n" |
| 649 | "without a passphrase to protect it?", |
| 650 | "PuTTYgen Warning", |
| 651 | MB_YESNO | MB_ICONWARNING); |
| 652 | if (ret != IDYES) |
| 653 | break; |
| 654 | } |
| 655 | if (prompt_keyfile(hwnd, "Save private key as:", |
| 656 | filename, 1)) { |
| 657 | int ret; |
| 658 | FILE *fp = fopen(filename, "r"); |
| 659 | if (fp) { |
| 660 | char buffer[FILENAME_MAX + 80]; |
| 661 | fclose(fp); |
| 662 | sprintf(buffer, "Overwrite existing file\n%.*s?", |
| 663 | FILENAME_MAX, filename); |
| 664 | ret = MessageBox(hwnd, buffer, "PuTTYgen Warning", |
| 665 | MB_YESNO | MB_ICONWARNING); |
| 666 | if (ret != IDYES) |
| 667 | break; |
| 668 | } |
| 669 | if (state->ssh2) { |
| 670 | ret = ssh2_save_userkey(filename, &state->ssh2key, |
| 671 | *passphrase ? passphrase : |
| 672 | NULL); |
| 673 | } else { |
| 674 | ret = saversakey(filename, &state->key, |
| 675 | *passphrase ? passphrase : NULL); |
| 676 | } |
| 677 | if (ret <= 0) { |
| 678 | MessageBox(hwnd, "Unable to save key file", |
| 679 | "PuTTYgen Error", MB_OK | MB_ICONERROR); |
| 680 | } |
| 681 | } |
| 682 | } |
| 683 | break; |
| 684 | case IDC_LOAD: |
| 685 | state = |
| 686 | (struct MainDlgState *) GetWindowLong(hwnd, GWL_USERDATA); |
| 687 | if (!state->generation_thread_exists) { |
| 688 | char filename[FILENAME_MAX]; |
| 689 | if (prompt_keyfile(hwnd, "Load private key:", filename, 0)) { |
| 690 | char passphrase[PASSPHRASE_MAXLEN]; |
| 691 | int needs_pass; |
| 692 | int ver; |
| 693 | int ret; |
| 694 | char *comment; |
| 695 | struct PassphraseProcStruct pps; |
| 696 | struct RSAKey newkey1; |
| 697 | struct ssh2_userkey *newkey2 = NULL; |
| 698 | |
| 699 | ver = keyfile_version(filename); |
| 700 | if (ver == 0) { |
| 701 | MessageBox(NULL, "Couldn't load private key.", |
| 702 | "PuTTYgen Error", MB_OK | MB_ICONERROR); |
| 703 | break; |
| 704 | } |
| 705 | |
| 706 | comment = NULL; |
| 707 | if (ver == 1) |
| 708 | needs_pass = rsakey_encrypted(filename, &comment); |
| 709 | else |
| 710 | needs_pass = |
| 711 | ssh2_userkey_encrypted(filename, &comment); |
| 712 | pps.passphrase = passphrase; |
| 713 | pps.comment = comment; |
| 714 | do { |
| 715 | if (needs_pass) { |
| 716 | int dlgret; |
| 717 | dlgret = DialogBoxParam(hinst, |
| 718 | MAKEINTRESOURCE(210), |
| 719 | NULL, PassphraseProc, |
| 720 | (LPARAM) & pps); |
| 721 | if (!dlgret) { |
| 722 | ret = -2; |
| 723 | break; |
| 724 | } |
| 725 | } else |
| 726 | *passphrase = '\0'; |
| 727 | if (ver == 1) |
| 728 | ret = |
| 729 | loadrsakey(filename, &newkey1, passphrase); |
| 730 | else { |
| 731 | newkey2 = |
| 732 | ssh2_load_userkey(filename, passphrase); |
| 733 | if (newkey2 == SSH2_WRONG_PASSPHRASE) |
| 734 | ret = -1; |
| 735 | else if (!newkey2) |
| 736 | ret = 0; |
| 737 | else |
| 738 | ret = 1; |
| 739 | } |
| 740 | } while (ret == -1); |
| 741 | if (comment) |
| 742 | sfree(comment); |
| 743 | if (ret == 0) { |
| 744 | MessageBox(NULL, "Couldn't load private key.", |
| 745 | "PuTTYgen Error", MB_OK | MB_ICONERROR); |
| 746 | } else if (ret == 1) { |
| 747 | EnableWindow(GetDlgItem(hwnd, IDC_GENERATE), 1); |
| 748 | EnableWindow(GetDlgItem(hwnd, IDC_LOAD), 1); |
| 749 | EnableWindow(GetDlgItem(hwnd, IDC_SAVE), 1); |
| 750 | /* |
| 751 | * Now update the key controls with all the |
| 752 | * key data. |
| 753 | */ |
| 754 | { |
| 755 | SetDlgItemText(hwnd, IDC_PASSPHRASE1EDIT, |
| 756 | passphrase); |
| 757 | SetDlgItemText(hwnd, IDC_PASSPHRASE2EDIT, |
| 758 | passphrase); |
| 759 | if (ver == 1) { |
| 760 | char buf[128]; |
| 761 | char *savecomment; |
| 762 | |
| 763 | state->ssh2 = FALSE; |
| 764 | state->commentptr = &state->key.comment; |
| 765 | state->key = newkey1; |
| 766 | |
| 767 | /* |
| 768 | * Set the key fingerprint. |
| 769 | */ |
| 770 | savecomment = state->key.comment; |
| 771 | state->key.comment = NULL; |
| 772 | rsa_fingerprint(buf, sizeof(buf), |
| 773 | &state->key); |
| 774 | state->key.comment = savecomment; |
| 775 | |
| 776 | SetDlgItemText(hwnd, IDC_FINGERPRINT, buf); |
| 777 | /* |
| 778 | * Construct a decimal representation |
| 779 | * of the key, for pasting into |
| 780 | * .ssh/authorized_keys on a Unix box. |
| 781 | */ |
| 782 | setupbigedit1(hwnd, IDC_KEYDISPLAY, |
| 783 | &state->key); |
| 784 | } else { |
| 785 | char *fp; |
| 786 | char *savecomment; |
| 787 | |
| 788 | state->ssh2 = TRUE; |
| 789 | state->commentptr = |
| 790 | &state->ssh2key.comment; |
| 791 | state->ssh2key = *newkey2; /* structure copy */ |
| 792 | sfree(newkey2); |
| 793 | |
| 794 | savecomment = state->ssh2key.comment; |
| 795 | state->ssh2key.comment = NULL; |
| 796 | fp = |
| 797 | state->ssh2key.alg-> |
| 798 | fingerprint(state->ssh2key.data); |
| 799 | state->ssh2key.comment = savecomment; |
| 800 | |
| 801 | SetDlgItemText(hwnd, IDC_FINGERPRINT, fp); |
| 802 | sfree(fp); |
| 803 | |
| 804 | setupbigedit2(hwnd, IDC_KEYDISPLAY, |
| 805 | &state->ssh2key); |
| 806 | } |
| 807 | SetDlgItemText(hwnd, IDC_COMMENTEDIT, |
| 808 | *state->commentptr); |
| 809 | } |
| 810 | /* |
| 811 | * Finally, hide the progress bar and show |
| 812 | * the key data. |
| 813 | */ |
| 814 | hidemany(hwnd, nokey_ids, TRUE); |
| 815 | hidemany(hwnd, generating_ids, TRUE); |
| 816 | hidemany(hwnd, gotkey_ids, FALSE); |
| 817 | state->key_exists = TRUE; |
| 818 | } |
| 819 | } |
| 820 | } |
| 821 | break; |
| 822 | } |
| 823 | return 0; |
| 824 | case WM_DONEKEY: |
| 825 | state = (struct MainDlgState *) GetWindowLong(hwnd, GWL_USERDATA); |
| 826 | state->generation_thread_exists = FALSE; |
| 827 | state->key_exists = TRUE; |
| 828 | SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETPOS, PROGRESSRANGE, |
| 829 | 0); |
| 830 | EnableWindow(GetDlgItem(hwnd, IDC_GENERATE), 1); |
| 831 | EnableWindow(GetDlgItem(hwnd, IDC_LOAD), 1); |
| 832 | EnableWindow(GetDlgItem(hwnd, IDC_SAVE), 1); |
| 833 | if (state->ssh2) { |
| 834 | state->ssh2key.data = &state->key; |
| 835 | state->ssh2key.alg = &ssh_rsa; |
| 836 | state->commentptr = &state->ssh2key.comment; |
| 837 | } else { |
| 838 | state->commentptr = &state->key.comment; |
| 839 | } |
| 840 | /* |
| 841 | * Invent a comment for the key. We'll do this by including |
| 842 | * the date in it. This will be so horrifyingly ugly that |
| 843 | * the user will immediately want to change it, which is |
| 844 | * what we want :-) |
| 845 | */ |
| 846 | *state->commentptr = smalloc(30); |
| 847 | { |
| 848 | time_t t; |
| 849 | struct tm *tm; |
| 850 | time(&t); |
| 851 | tm = localtime(&t); |
| 852 | strftime(*state->commentptr, 30, "rsa-key-%Y%m%d", tm); |
| 853 | } |
| 854 | |
| 855 | /* |
| 856 | * Now update the key controls with all the key data. |
| 857 | */ |
| 858 | { |
| 859 | char *savecomment; |
| 860 | /* |
| 861 | * Blank passphrase, initially. This isn't dangerous, |
| 862 | * because we will warn (Are You Sure?) before allowing |
| 863 | * the user to save an unprotected private key. |
| 864 | */ |
| 865 | SetDlgItemText(hwnd, IDC_PASSPHRASE1EDIT, ""); |
| 866 | SetDlgItemText(hwnd, IDC_PASSPHRASE2EDIT, ""); |
| 867 | /* |
| 868 | * Set the comment. |
| 869 | */ |
| 870 | SetDlgItemText(hwnd, IDC_COMMENTEDIT, *state->commentptr); |
| 871 | /* |
| 872 | * Set the key fingerprint. |
| 873 | */ |
| 874 | savecomment = *state->commentptr; |
| 875 | *state->commentptr = NULL; |
| 876 | if (state->ssh2) { |
| 877 | char *fp; |
| 878 | fp = state->ssh2key.alg->fingerprint(state->ssh2key.data); |
| 879 | SetDlgItemText(hwnd, IDC_FINGERPRINT, fp); |
| 880 | sfree(fp); |
| 881 | } else { |
| 882 | char buf[128]; |
| 883 | rsa_fingerprint(buf, sizeof(buf), &state->key); |
| 884 | SetDlgItemText(hwnd, IDC_FINGERPRINT, buf); |
| 885 | } |
| 886 | *state->commentptr = savecomment; |
| 887 | /* |
| 888 | * Construct a decimal representation of the key, for |
| 889 | * pasting into .ssh/authorized_keys on a Unix box. |
| 890 | */ |
| 891 | if (state->ssh2) { |
| 892 | setupbigedit2(hwnd, IDC_KEYDISPLAY, &state->ssh2key); |
| 893 | } else { |
| 894 | setupbigedit1(hwnd, IDC_KEYDISPLAY, &state->key); |
| 895 | } |
| 896 | } |
| 897 | /* |
| 898 | * Finally, hide the progress bar and show the key data. |
| 899 | */ |
| 900 | hidemany(hwnd, nokey_ids, TRUE); |
| 901 | hidemany(hwnd, generating_ids, TRUE); |
| 902 | hidemany(hwnd, gotkey_ids, FALSE); |
| 903 | break; |
| 904 | case WM_CLOSE: |
| 905 | state = (struct MainDlgState *) GetWindowLong(hwnd, GWL_USERDATA); |
| 906 | sfree(state); |
| 907 | EndDialog(hwnd, 1); |
| 908 | return 0; |
| 909 | } |
| 910 | return 0; |
| 911 | } |
| 912 | |
| 913 | int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show) |
| 914 | { |
| 915 | InitCommonControls(); |
| 916 | hinst = inst; |
| 917 | random_init(); |
| 918 | return DialogBox(hinst, MAKEINTRESOURCE(201), NULL, |
| 919 | MainDlgProc) != IDOK; |
| 920 | } |