| 1 | /* |
| 2 | * PuTTY key generation front end. |
| 3 | */ |
| 4 | |
| 5 | #include <windows.h> |
| 6 | #include <commctrl.h> |
| 7 | #include <time.h> |
| 8 | #ifndef NO_SECURITY |
| 9 | #include <aclapi.h> |
| 10 | #endif |
| 11 | #include <stdio.h> |
| 12 | |
| 13 | #define PUTTY_DO_GLOBALS |
| 14 | |
| 15 | #include "putty.h" |
| 16 | #include "ssh.h" |
| 17 | #include "winstuff.h" |
| 18 | |
| 19 | #define WM_DONEKEY (WM_XUSER + 1) |
| 20 | |
| 21 | #define KEYSIZE 1024 |
| 22 | |
| 23 | /* |
| 24 | * TODO: |
| 25 | * - test the generated keys for actual working-RSA-key-hood |
| 26 | * - variable key size |
| 27 | */ |
| 28 | |
| 29 | /* ---------------------------------------------------------------------- |
| 30 | * Progress report code. This is really horrible :-) |
| 31 | */ |
| 32 | #define PHASE1TOTAL 0x10000 |
| 33 | #define PHASE2TOTAL 0x10000 |
| 34 | #define PHASE3TOTAL 0x04000 |
| 35 | #define PHASE1START 0 |
| 36 | #define PHASE2START (PHASE1TOTAL) |
| 37 | #define PHASE3START (PHASE1TOTAL + PHASE2TOTAL) |
| 38 | #define TOTALTOTAL (PHASE1TOTAL + PHASE2TOTAL + PHASE3TOTAL) |
| 39 | #define PROGRESSBIGRANGE 65535 |
| 40 | #define DIVISOR ((TOTALTOTAL + PROGRESSBIGRANGE - 1) / PROGRESSBIGRANGE) |
| 41 | #define PROGRESSRANGE (TOTALTOTAL / DIVISOR) |
| 42 | struct progress { |
| 43 | unsigned phase1param, phase1current, phase1n; |
| 44 | unsigned phase2param, phase2current, phase2n; |
| 45 | unsigned phase3mult; |
| 46 | HWND progbar; |
| 47 | }; |
| 48 | |
| 49 | static void progress_update(void *param, int phase, int iprogress) { |
| 50 | struct progress *p = (struct progress *)param; |
| 51 | unsigned progress = iprogress; |
| 52 | int position; |
| 53 | |
| 54 | switch (phase) { |
| 55 | case -1: |
| 56 | p->phase1param = 0x10000 + progress; |
| 57 | p->phase1current = 0x10000; p->phase1n = 0; |
| 58 | return; |
| 59 | case -2: |
| 60 | p->phase2param = 0x10000 + progress; |
| 61 | p->phase2current = 0x10000; p->phase2n = 0; |
| 62 | return; |
| 63 | case -3: |
| 64 | p->phase3mult = PHASE3TOTAL / progress; |
| 65 | return; |
| 66 | case 1: |
| 67 | while (p->phase1n < progress) { |
| 68 | p->phase1n++; |
| 69 | p->phase1current *= p->phase1param; |
| 70 | p->phase1current /= 0x10000; |
| 71 | } |
| 72 | position = PHASE1START + 0x10000 - p->phase1current; |
| 73 | break; |
| 74 | case 2: |
| 75 | while (p->phase2n < progress) { |
| 76 | p->phase2n++; |
| 77 | p->phase2current *= p->phase2param; |
| 78 | p->phase2current /= 0x10000; |
| 79 | } |
| 80 | position = PHASE2START + 0x10000 - p->phase2current; |
| 81 | break; |
| 82 | case 3: |
| 83 | position = PHASE3START + progress * p->phase3mult; |
| 84 | break; |
| 85 | } |
| 86 | |
| 87 | SendMessage(p->progbar, PBM_SETPOS, position / DIVISOR, 0); |
| 88 | } |
| 89 | |
| 90 | extern char ver[]; |
| 91 | |
| 92 | #define PASSPHRASE_MAXLEN 512 |
| 93 | |
| 94 | struct PassphraseProcStruct { |
| 95 | char *passphrase; |
| 96 | char *comment; |
| 97 | }; |
| 98 | |
| 99 | /* |
| 100 | * Dialog-box function for the passphrase box. |
| 101 | */ |
| 102 | static int CALLBACK PassphraseProc(HWND hwnd, UINT msg, |
| 103 | WPARAM wParam, LPARAM lParam) { |
| 104 | static char *passphrase; |
| 105 | struct PassphraseProcStruct *p; |
| 106 | |
| 107 | switch (msg) { |
| 108 | case WM_INITDIALOG: |
| 109 | SetForegroundWindow(hwnd); |
| 110 | SetWindowPos (hwnd, HWND_TOP, 0, 0, 0, 0, |
| 111 | SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW); |
| 112 | p = (struct PassphraseProcStruct *)lParam; |
| 113 | passphrase = p->passphrase; |
| 114 | if (p->comment) |
| 115 | SetDlgItemText(hwnd, 101, p->comment); |
| 116 | *passphrase = 0; |
| 117 | return 0; |
| 118 | case WM_COMMAND: |
| 119 | switch (LOWORD(wParam)) { |
| 120 | case IDOK: |
| 121 | if (*passphrase) |
| 122 | EndDialog (hwnd, 1); |
| 123 | else |
| 124 | MessageBeep (0); |
| 125 | return 0; |
| 126 | case IDCANCEL: |
| 127 | EndDialog (hwnd, 0); |
| 128 | return 0; |
| 129 | case 102: /* edit box */ |
| 130 | if (HIWORD(wParam) == EN_CHANGE) { |
| 131 | GetDlgItemText (hwnd, 102, passphrase, PASSPHRASE_MAXLEN-1); |
| 132 | passphrase[PASSPHRASE_MAXLEN-1] = '\0'; |
| 133 | } |
| 134 | return 0; |
| 135 | } |
| 136 | return 0; |
| 137 | case WM_CLOSE: |
| 138 | EndDialog (hwnd, 0); |
| 139 | return 0; |
| 140 | } |
| 141 | return 0; |
| 142 | } |
| 143 | |
| 144 | /* |
| 145 | * Prompt for a key file. Assumes the filename buffer is of size |
| 146 | * FILENAME_MAX. |
| 147 | */ |
| 148 | static int prompt_keyfile(HWND hwnd, char *dlgtitle, |
| 149 | char *filename, int save) { |
| 150 | OPENFILENAME of; |
| 151 | memset(&of, 0, sizeof(of)); |
| 152 | #ifdef OPENFILENAME_SIZE_VERSION_400 |
| 153 | of.lStructSize = OPENFILENAME_SIZE_VERSION_400; |
| 154 | #else |
| 155 | of.lStructSize = sizeof(of); |
| 156 | #endif |
| 157 | of.hwndOwner = hwnd; |
| 158 | of.lpstrFilter = "All Files\0*\0\0\0"; |
| 159 | of.lpstrCustomFilter = NULL; |
| 160 | of.nFilterIndex = 1; |
| 161 | of.lpstrFile = filename; *filename = '\0'; |
| 162 | of.nMaxFile = FILENAME_MAX; |
| 163 | of.lpstrFileTitle = NULL; |
| 164 | of.lpstrInitialDir = NULL; |
| 165 | of.lpstrTitle = dlgtitle; |
| 166 | of.Flags = 0; |
| 167 | if (save) |
| 168 | return GetSaveFileName(&of); |
| 169 | else |
| 170 | return GetOpenFileName(&of); |
| 171 | } |
| 172 | |
| 173 | /* |
| 174 | * This function is needed to link with the DES code. We need not |
| 175 | * have it do anything at all. |
| 176 | */ |
| 177 | void logevent(char *msg) { |
| 178 | } |
| 179 | |
| 180 | /* |
| 181 | * Dialog-box function for the Licence box. |
| 182 | */ |
| 183 | static int CALLBACK LicenceProc (HWND hwnd, UINT msg, |
| 184 | WPARAM wParam, LPARAM lParam) { |
| 185 | switch (msg) { |
| 186 | case WM_INITDIALOG: |
| 187 | return 1; |
| 188 | case WM_COMMAND: |
| 189 | switch (LOWORD(wParam)) { |
| 190 | case IDOK: |
| 191 | EndDialog(hwnd, 1); |
| 192 | return 0; |
| 193 | } |
| 194 | return 0; |
| 195 | case WM_CLOSE: |
| 196 | EndDialog(hwnd, 1); |
| 197 | return 0; |
| 198 | } |
| 199 | return 0; |
| 200 | } |
| 201 | |
| 202 | /* |
| 203 | * Dialog-box function for the About box. |
| 204 | */ |
| 205 | static int CALLBACK AboutProc (HWND hwnd, UINT msg, |
| 206 | WPARAM wParam, LPARAM lParam) { |
| 207 | switch (msg) { |
| 208 | case WM_INITDIALOG: |
| 209 | SetDlgItemText (hwnd, 100, ver); |
| 210 | return 1; |
| 211 | case WM_COMMAND: |
| 212 | switch (LOWORD(wParam)) { |
| 213 | case IDOK: |
| 214 | EndDialog(hwnd, 1); |
| 215 | return 0; |
| 216 | case 101: |
| 217 | EnableWindow(hwnd, 0); |
| 218 | DialogBox (hinst, MAKEINTRESOURCE(214), NULL, LicenceProc); |
| 219 | EnableWindow(hwnd, 1); |
| 220 | SetActiveWindow(hwnd); |
| 221 | return 0; |
| 222 | } |
| 223 | return 0; |
| 224 | case WM_CLOSE: |
| 225 | EndDialog(hwnd, 1); |
| 226 | return 0; |
| 227 | } |
| 228 | return 0; |
| 229 | } |
| 230 | |
| 231 | /* |
| 232 | * Thread to generate a key. |
| 233 | */ |
| 234 | struct rsa_key_thread_params { |
| 235 | HWND progressbar; /* notify this with progress */ |
| 236 | HWND dialog; /* notify this on completion */ |
| 237 | struct RSAKey *key; |
| 238 | struct RSAAux *aux; |
| 239 | }; |
| 240 | static DWORD WINAPI generate_rsa_key_thread(void *param) { |
| 241 | struct rsa_key_thread_params *params = |
| 242 | (struct rsa_key_thread_params *)param; |
| 243 | struct progress prog; |
| 244 | prog.progbar = params->progressbar; |
| 245 | |
| 246 | rsa_generate(params->key, params->aux, KEYSIZE, progress_update, &prog); |
| 247 | |
| 248 | PostMessage(params->dialog, WM_DONEKEY, 0, 0); |
| 249 | |
| 250 | free(params); |
| 251 | return 0; |
| 252 | } |
| 253 | |
| 254 | struct MainDlgState { |
| 255 | int collecting_entropy; |
| 256 | int generation_thread_exists; |
| 257 | int key_exists; |
| 258 | int entropy_got, entropy_required, entropy_size; |
| 259 | unsigned *entropy; |
| 260 | struct RSAKey key; |
| 261 | struct RSAAux aux; |
| 262 | }; |
| 263 | |
| 264 | static void hidemany(HWND hwnd, const int *ids, int hideit) { |
| 265 | while (*ids) { |
| 266 | ShowWindow(GetDlgItem(hwnd, *ids++), (hideit ? SW_HIDE : SW_SHOW)); |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | static void setupbigedit(HWND hwnd, int id, struct RSAKey *key) { |
| 271 | char *buffer; |
| 272 | char *dec1, *dec2; |
| 273 | |
| 274 | dec1 = bignum_decimal(key->exponent); |
| 275 | dec2 = bignum_decimal(key->modulus); |
| 276 | buffer = malloc(strlen(dec1)+strlen(dec2)+ |
| 277 | strlen(key->comment)+30); |
| 278 | sprintf(buffer, "%d %s %s %s", |
| 279 | ssh1_bignum_bitcount(key->modulus), |
| 280 | dec1, dec2, key->comment); |
| 281 | SetDlgItemText(hwnd, id, buffer); |
| 282 | free(dec1); |
| 283 | free(dec2); |
| 284 | free(buffer); |
| 285 | } |
| 286 | |
| 287 | /* |
| 288 | * Dialog-box function for the main PuTTYgen dialog box. |
| 289 | */ |
| 290 | static int CALLBACK MainDlgProc (HWND hwnd, UINT msg, |
| 291 | WPARAM wParam, LPARAM lParam) { |
| 292 | enum { |
| 293 | controlidstart = 100, |
| 294 | IDC_TITLE, |
| 295 | IDC_BOX_KEY, IDC_BOXT_KEY, |
| 296 | IDC_NOKEY, |
| 297 | IDC_GENERATING, |
| 298 | IDC_PROGRESS, |
| 299 | IDC_PKSTATIC, IDC_KEYDISPLAY, |
| 300 | IDC_FPSTATIC, IDC_FINGERPRINT, |
| 301 | IDC_COMMENTSTATIC, IDC_COMMENTEDIT, |
| 302 | IDC_PASSPHRASE1STATIC, IDC_PASSPHRASE1EDIT, |
| 303 | IDC_PASSPHRASE2STATIC, IDC_PASSPHRASE2EDIT, |
| 304 | IDC_BOX_ACTIONS, IDC_BOXT_ACTIONS, |
| 305 | IDC_GENSTATIC, IDC_GENERATE, |
| 306 | IDC_LOADSTATIC, IDC_LOAD, |
| 307 | IDC_SAVESTATIC, IDC_SAVE, |
| 308 | IDC_ABOUT, |
| 309 | }; |
| 310 | static const int nokey_ids[] = { IDC_NOKEY, 0 }; |
| 311 | static const int generating_ids[] = { IDC_GENERATING, IDC_PROGRESS, 0 }; |
| 312 | static const int gotkey_ids[] = { |
| 313 | IDC_PKSTATIC, IDC_KEYDISPLAY, |
| 314 | IDC_FPSTATIC, IDC_FINGERPRINT, |
| 315 | IDC_COMMENTSTATIC, IDC_COMMENTEDIT, |
| 316 | IDC_PASSPHRASE1STATIC, IDC_PASSPHRASE1EDIT, |
| 317 | IDC_PASSPHRASE2STATIC, IDC_PASSPHRASE2EDIT, 0 }; |
| 318 | static const char generating_msg[] = |
| 319 | "Please wait while a key is generated..."; |
| 320 | static const char entropy_msg[] = |
| 321 | "Please move the mouse in this window to generate randomness"; |
| 322 | struct MainDlgState *state; |
| 323 | |
| 324 | switch (msg) { |
| 325 | case WM_INITDIALOG: |
| 326 | state = malloc(sizeof(*state)); |
| 327 | state->generation_thread_exists = FALSE; |
| 328 | state->key_exists = FALSE; |
| 329 | SetWindowLong(hwnd, GWL_USERDATA, (LONG)state); |
| 330 | { |
| 331 | struct ctlpos cp, cp2; |
| 332 | |
| 333 | /* Accelerators used: acglops */ |
| 334 | |
| 335 | ctlposinit(&cp, hwnd, 10, 10, 10); |
| 336 | bartitle(&cp, "Public and private key generation for PuTTY", |
| 337 | IDC_TITLE); |
| 338 | beginbox(&cp, "Key", |
| 339 | IDC_BOX_KEY, IDC_BOXT_KEY); |
| 340 | cp2 = cp; |
| 341 | statictext(&cp2, "No key.", IDC_NOKEY); |
| 342 | cp2 = cp; |
| 343 | statictext(&cp2, "", |
| 344 | IDC_GENERATING); |
| 345 | progressbar(&cp2, IDC_PROGRESS); |
| 346 | bigeditctrl(&cp, |
| 347 | "&Public key for pasting into authorized_keys file:", |
| 348 | IDC_PKSTATIC, IDC_KEYDISPLAY, 7); |
| 349 | SendDlgItemMessage(hwnd, IDC_KEYDISPLAY, EM_SETREADONLY, 1, 0); |
| 350 | staticedit(&cp, "Key fingerprint:", IDC_FPSTATIC, |
| 351 | IDC_FINGERPRINT, 70); |
| 352 | SendDlgItemMessage(hwnd, IDC_FINGERPRINT, EM_SETREADONLY, 1, 0); |
| 353 | staticedit(&cp, "Key &comment:", IDC_COMMENTSTATIC, |
| 354 | IDC_COMMENTEDIT, 70); |
| 355 | staticpassedit(&cp, "Key p&assphrase:", IDC_PASSPHRASE1STATIC, |
| 356 | IDC_PASSPHRASE1EDIT, 70); |
| 357 | staticpassedit(&cp, "C&onfirm passphrase:", IDC_PASSPHRASE2STATIC, |
| 358 | IDC_PASSPHRASE2EDIT, 70); |
| 359 | endbox(&cp); |
| 360 | beginbox(&cp, "Actions", |
| 361 | IDC_BOX_ACTIONS, IDC_BOXT_ACTIONS); |
| 362 | staticbtn(&cp, "Generate a public/private key pair", |
| 363 | IDC_GENSTATIC, "&Generate", IDC_GENERATE); |
| 364 | staticbtn(&cp, "Load an existing private key file", |
| 365 | IDC_LOADSTATIC, "&Load", IDC_LOAD); |
| 366 | staticbtn(&cp, "Save the generated key to a new file", |
| 367 | IDC_SAVESTATIC, "&Save", IDC_SAVE); |
| 368 | endbox(&cp); |
| 369 | } |
| 370 | /* |
| 371 | * Initially, hide the progress bar and the key display, |
| 372 | * and show the no-key display. Also disable the Save |
| 373 | * button, because with no key we obviously can't save |
| 374 | * anything. |
| 375 | */ |
| 376 | hidemany(hwnd, nokey_ids, FALSE); |
| 377 | hidemany(hwnd, generating_ids, TRUE); |
| 378 | hidemany(hwnd, gotkey_ids, TRUE); |
| 379 | EnableWindow(GetDlgItem(hwnd, IDC_SAVE), 0); |
| 380 | |
| 381 | return 1; |
| 382 | case WM_MOUSEMOVE: |
| 383 | state = (struct MainDlgState *)GetWindowLong(hwnd, GWL_USERDATA); |
| 384 | if (state->collecting_entropy) { |
| 385 | state->entropy[state->entropy_got++] = lParam; |
| 386 | state->entropy[state->entropy_got++] = GetMessageTime(); |
| 387 | SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETPOS, |
| 388 | state->entropy_got, 0); |
| 389 | if (state->entropy_got >= state->entropy_required) { |
| 390 | struct rsa_key_thread_params *params; |
| 391 | DWORD threadid; |
| 392 | |
| 393 | /* |
| 394 | * Seed the entropy pool |
| 395 | */ |
| 396 | random_add_heavynoise(state->entropy, state->entropy_size); |
| 397 | memset(state->entropy, 0, state->entropy_size); |
| 398 | free(state->entropy); |
| 399 | |
| 400 | SetDlgItemText(hwnd, IDC_GENERATING, generating_msg); |
| 401 | SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETRANGE, 0, |
| 402 | MAKELPARAM(0, PROGRESSRANGE)); |
| 403 | SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETPOS, 0, 0); |
| 404 | |
| 405 | params = malloc(sizeof(*params)); |
| 406 | params->progressbar = GetDlgItem(hwnd, IDC_PROGRESS); |
| 407 | params->dialog = hwnd; |
| 408 | params->key = &state->key; |
| 409 | params->aux = &state->aux; |
| 410 | |
| 411 | if (!CreateThread(NULL, 0, generate_rsa_key_thread, |
| 412 | params, 0, &threadid)) { |
| 413 | MessageBox(hwnd, "Out of thread resources", |
| 414 | "Key generation error", |
| 415 | MB_OK | MB_ICONERROR); |
| 416 | free(params); |
| 417 | } else { |
| 418 | state->generation_thread_exists = TRUE; |
| 419 | state->collecting_entropy = FALSE; |
| 420 | } |
| 421 | } |
| 422 | } |
| 423 | break; |
| 424 | case WM_COMMAND: |
| 425 | switch (LOWORD(wParam)) { |
| 426 | case IDC_COMMENTEDIT: |
| 427 | if (HIWORD(wParam) == EN_CHANGE) { |
| 428 | state = (struct MainDlgState *) |
| 429 | GetWindowLong(hwnd, GWL_USERDATA); |
| 430 | if (state->key_exists) { |
| 431 | HWND editctl = GetDlgItem(hwnd, IDC_COMMENTEDIT); |
| 432 | int len = GetWindowTextLength(editctl); |
| 433 | if (state->key.comment) |
| 434 | free(state->key.comment); |
| 435 | state->key.comment = malloc(len+1); |
| 436 | GetWindowText(editctl, state->key.comment, len+1); |
| 437 | } |
| 438 | } |
| 439 | break; |
| 440 | case IDC_ABOUT: |
| 441 | EnableWindow(hwnd, 0); |
| 442 | DialogBox (hinst, MAKEINTRESOURCE(213), NULL, AboutProc); |
| 443 | EnableWindow(hwnd, 1); |
| 444 | SetActiveWindow(hwnd); |
| 445 | return 0; |
| 446 | case IDC_GENERATE: |
| 447 | state = (struct MainDlgState *)GetWindowLong(hwnd, GWL_USERDATA); |
| 448 | if (!state->generation_thread_exists) { |
| 449 | hidemany(hwnd, nokey_ids, TRUE); |
| 450 | hidemany(hwnd, generating_ids, FALSE); |
| 451 | hidemany(hwnd, gotkey_ids, TRUE); |
| 452 | EnableWindow(GetDlgItem(hwnd, IDC_GENERATE), 0); |
| 453 | EnableWindow(GetDlgItem(hwnd, IDC_LOAD), 0); |
| 454 | EnableWindow(GetDlgItem(hwnd, IDC_SAVE), 0); |
| 455 | state->key_exists = FALSE; |
| 456 | SetDlgItemText(hwnd, IDC_GENERATING, entropy_msg); |
| 457 | state->collecting_entropy = TRUE; |
| 458 | |
| 459 | /* |
| 460 | * My brief statistical tests on mouse movements |
| 461 | * suggest that there are about 5 bits of |
| 462 | * randomness in the x position, 5 in the y |
| 463 | * position, and 1.7 in the message time, making |
| 464 | * 11.7 bits of unpredictability per mouse |
| 465 | * movement. However, other people have told me |
| 466 | * it's far less than that, so I'm going to be |
| 467 | * stupidly cautious and knock that down to a nice |
| 468 | * round 4. |
| 469 | */ |
| 470 | state->entropy_required = (KEYSIZE / 4) * 2; |
| 471 | state->entropy_got = 0; |
| 472 | state->entropy_size = (state->entropy_required * |
| 473 | sizeof(*state->entropy)); |
| 474 | state->entropy = malloc(state->entropy_size); |
| 475 | |
| 476 | SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETRANGE, 0, |
| 477 | MAKELPARAM(0, state->entropy_required)); |
| 478 | SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETPOS, 0, 0); |
| 479 | } |
| 480 | break; |
| 481 | case IDC_SAVE: |
| 482 | state = (struct MainDlgState *)GetWindowLong(hwnd, GWL_USERDATA); |
| 483 | if (state->key_exists) { |
| 484 | char filename[FILENAME_MAX]; |
| 485 | char passphrase[PASSPHRASE_MAXLEN]; |
| 486 | char passphrase2[PASSPHRASE_MAXLEN]; |
| 487 | GetDlgItemText(hwnd, IDC_PASSPHRASE1EDIT, |
| 488 | passphrase, sizeof(passphrase)); |
| 489 | GetDlgItemText(hwnd, IDC_PASSPHRASE2EDIT, |
| 490 | passphrase2, sizeof(passphrase2)); |
| 491 | if (strcmp(passphrase, passphrase2)) { |
| 492 | MessageBox(hwnd, |
| 493 | "The two passphrases given do not match.", |
| 494 | "PuTTYgen Error", |
| 495 | MB_OK | MB_ICONERROR); |
| 496 | break; |
| 497 | } |
| 498 | if (!*passphrase) { |
| 499 | int ret; |
| 500 | ret = MessageBox(hwnd, |
| 501 | "Are you sure you want to save this key\n" |
| 502 | "without a passphrase to protect it?", |
| 503 | "PuTTYgen Warning", |
| 504 | MB_YESNO | MB_ICONWARNING); |
| 505 | if (ret != IDYES) |
| 506 | break; |
| 507 | } |
| 508 | if (prompt_keyfile(hwnd, "Save private key as:", |
| 509 | filename, 1)) { |
| 510 | int ret; |
| 511 | FILE *fp = fopen(filename, "r"); |
| 512 | if (fp) { |
| 513 | char buffer[FILENAME_MAX+80]; |
| 514 | fclose(fp); |
| 515 | sprintf(buffer, "Overwrite existing file\n%.*s?", |
| 516 | FILENAME_MAX, filename); |
| 517 | ret = MessageBox(hwnd, buffer, "PuTTYgen Warning", |
| 518 | MB_YESNO | MB_ICONWARNING); |
| 519 | if (ret != IDYES) |
| 520 | break; |
| 521 | } |
| 522 | ret = saversakey(filename, &state->key, &state->aux, |
| 523 | *passphrase ? passphrase : NULL); |
| 524 | if (ret <= 0) { |
| 525 | MessageBox(hwnd, "Unable to save key file", |
| 526 | "PuTTYgen Error", |
| 527 | MB_OK | MB_ICONERROR); |
| 528 | } |
| 529 | } |
| 530 | } |
| 531 | break; |
| 532 | case IDC_LOAD: |
| 533 | state = (struct MainDlgState *)GetWindowLong(hwnd, GWL_USERDATA); |
| 534 | if (!state->generation_thread_exists) { |
| 535 | char filename[FILENAME_MAX]; |
| 536 | if (prompt_keyfile(hwnd, "Load private key:", |
| 537 | filename, 0)) { |
| 538 | char passphrase[PASSPHRASE_MAXLEN]; |
| 539 | int needs_pass; |
| 540 | int ret; |
| 541 | char *comment; |
| 542 | struct PassphraseProcStruct pps; |
| 543 | struct RSAKey newkey; |
| 544 | struct RSAAux newaux; |
| 545 | |
| 546 | needs_pass = rsakey_encrypted(filename, &comment); |
| 547 | pps.passphrase = passphrase; |
| 548 | pps.comment = comment; |
| 549 | do { |
| 550 | if (needs_pass) { |
| 551 | int dlgret; |
| 552 | dlgret = DialogBoxParam(hinst, |
| 553 | MAKEINTRESOURCE(210), |
| 554 | NULL, PassphraseProc, |
| 555 | (LPARAM)&pps); |
| 556 | if (!dlgret) { |
| 557 | ret = -2; |
| 558 | break; |
| 559 | } |
| 560 | } else |
| 561 | *passphrase = '\0'; |
| 562 | ret = loadrsakey(filename, &newkey, &newaux, |
| 563 | passphrase); |
| 564 | } while (ret == -1); |
| 565 | if (comment) free(comment); |
| 566 | if (ret == 0) { |
| 567 | MessageBox(NULL, "Couldn't load private key.", |
| 568 | "PuTTYgen Error", MB_OK | MB_ICONERROR); |
| 569 | } else if (ret == 1) { |
| 570 | state->key = newkey; |
| 571 | state->aux = newaux; |
| 572 | |
| 573 | EnableWindow(GetDlgItem(hwnd, IDC_GENERATE), 1); |
| 574 | EnableWindow(GetDlgItem(hwnd, IDC_LOAD), 1); |
| 575 | EnableWindow(GetDlgItem(hwnd, IDC_SAVE), 1); |
| 576 | /* |
| 577 | * Now update the key controls with all the |
| 578 | * key data. |
| 579 | */ |
| 580 | { |
| 581 | char buf[128]; |
| 582 | SetDlgItemText(hwnd, IDC_PASSPHRASE1EDIT, |
| 583 | passphrase); |
| 584 | SetDlgItemText(hwnd, IDC_PASSPHRASE2EDIT, |
| 585 | passphrase); |
| 586 | SetDlgItemText(hwnd, IDC_COMMENTEDIT, |
| 587 | state->key.comment); |
| 588 | /* |
| 589 | * Set the key fingerprint. |
| 590 | */ |
| 591 | { |
| 592 | char *savecomment = state->key.comment; |
| 593 | state->key.comment = NULL; |
| 594 | rsa_fingerprint(buf, sizeof(buf), &state->key); |
| 595 | state->key.comment = savecomment; |
| 596 | } |
| 597 | SetDlgItemText(hwnd, IDC_FINGERPRINT, buf); |
| 598 | /* |
| 599 | * Construct a decimal representation |
| 600 | * of the key, for pasting into |
| 601 | * .ssh/authorized_keys on a Unix box. |
| 602 | */ |
| 603 | setupbigedit(hwnd, IDC_KEYDISPLAY, &state->key); |
| 604 | } |
| 605 | /* |
| 606 | * Finally, hide the progress bar and show |
| 607 | * the key data. |
| 608 | */ |
| 609 | hidemany(hwnd, nokey_ids, TRUE); |
| 610 | hidemany(hwnd, generating_ids, TRUE); |
| 611 | hidemany(hwnd, gotkey_ids, FALSE); |
| 612 | state->key_exists = TRUE; |
| 613 | } |
| 614 | } |
| 615 | } |
| 616 | break; |
| 617 | } |
| 618 | return 0; |
| 619 | case WM_DONEKEY: |
| 620 | state = (struct MainDlgState *)GetWindowLong(hwnd, GWL_USERDATA); |
| 621 | state->generation_thread_exists = FALSE; |
| 622 | state->key_exists = TRUE; |
| 623 | SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETPOS, PROGRESSRANGE, 0); |
| 624 | EnableWindow(GetDlgItem(hwnd, IDC_GENERATE), 1); |
| 625 | EnableWindow(GetDlgItem(hwnd, IDC_LOAD), 1); |
| 626 | EnableWindow(GetDlgItem(hwnd, IDC_SAVE), 1); |
| 627 | /* |
| 628 | * Invent a comment for the key. We'll do this by including |
| 629 | * the date in it. This will be so horrifyingly ugly that |
| 630 | * the user will immediately want to change it, which is |
| 631 | * what we want :-) |
| 632 | */ |
| 633 | state->key.comment = malloc(30); |
| 634 | { |
| 635 | time_t t; |
| 636 | struct tm *tm; |
| 637 | time(&t); |
| 638 | tm = localtime(&t); |
| 639 | strftime(state->key.comment, 30, "rsa-key-%Y%m%d", tm); |
| 640 | } |
| 641 | |
| 642 | /* |
| 643 | * Now update the key controls with all the key data. |
| 644 | */ |
| 645 | { |
| 646 | char buf[128]; |
| 647 | /* |
| 648 | * Blank passphrase, initially. This isn't dangerous, |
| 649 | * because we will warn (Are You Sure?) before allowing |
| 650 | * the user to save an unprotected private key. |
| 651 | */ |
| 652 | SetDlgItemText(hwnd, IDC_PASSPHRASE1EDIT, ""); |
| 653 | SetDlgItemText(hwnd, IDC_PASSPHRASE2EDIT, ""); |
| 654 | /* |
| 655 | * Set the comment. |
| 656 | */ |
| 657 | SetDlgItemText(hwnd, IDC_COMMENTEDIT, state->key.comment); |
| 658 | /* |
| 659 | * Set the key fingerprint. |
| 660 | */ |
| 661 | { |
| 662 | char *savecomment = state->key.comment; |
| 663 | state->key.comment = NULL; |
| 664 | rsa_fingerprint(buf, sizeof(buf), &state->key); |
| 665 | state->key.comment = savecomment; |
| 666 | } |
| 667 | SetDlgItemText(hwnd, IDC_FINGERPRINT, buf); |
| 668 | /* |
| 669 | * Construct a decimal representation of the key, for |
| 670 | * pasting into .ssh/authorized_keys on a Unix box. |
| 671 | */ |
| 672 | setupbigedit(hwnd, IDC_KEYDISPLAY, &state->key); |
| 673 | } |
| 674 | /* |
| 675 | * Finally, hide the progress bar and show the key data. |
| 676 | */ |
| 677 | hidemany(hwnd, nokey_ids, TRUE); |
| 678 | hidemany(hwnd, generating_ids, TRUE); |
| 679 | hidemany(hwnd, gotkey_ids, FALSE); |
| 680 | break; |
| 681 | case WM_CLOSE: |
| 682 | state = (struct MainDlgState *)GetWindowLong(hwnd, GWL_USERDATA); |
| 683 | free(state); |
| 684 | EndDialog(hwnd, 1); |
| 685 | return 0; |
| 686 | } |
| 687 | return 0; |
| 688 | } |
| 689 | |
| 690 | int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show) { |
| 691 | InitCommonControls(); |
| 692 | hinst = inst; |
| 693 | random_init(); |
| 694 | return DialogBox(hinst, MAKEINTRESOURCE(201), NULL, MainDlgProc) != IDOK; |
| 695 | } |