2 * Pageant: the PuTTY Authentication Agent.
7 #include <stdio.h> /* FIXME */
8 #include "putty.h" /* FIXME */
12 #define IDI_MAINICON 200
13 #define IDI_TRAYICON 201
15 #define WM_XUSER (WM_USER + 0x2000)
16 #define WM_SYSTRAY (WM_XUSER + 6)
17 #define WM_SYSTRAY2 (WM_XUSER + 7)
19 #define AGENT_COPYDATA_ID 0x804e50ba /* random goop */
22 * FIXME: maybe some day we can sort this out ...
24 #define AGENT_MAX_MSGLEN 8192
26 #define IDM_CLOSE 0x0010
27 #define IDM_VIEWKEYS 0x0020
29 #define APPNAME "Pageant"
31 #define SSH_AGENTC_REQUEST_RSA_IDENTITIES 1
32 #define SSH_AGENT_RSA_IDENTITIES_ANSWER 2
33 #define SSH_AGENTC_RSA_CHALLENGE 3
34 #define SSH_AGENT_RSA_RESPONSE 4
35 #define SSH_AGENT_FAILURE 5
36 #define SSH_AGENT_SUCCESS 6
37 #define SSH_AGENTC_ADD_RSA_IDENTITY 7
38 #define SSH_AGENTC_REMOVE_RSA_IDENTITY 8
48 * We need this to link with the RSA code, because rsaencrypt()
49 * pads its data with random bytes. Since we only use rsadecrypt(),
50 * which is deterministic, this should never be called.
52 * If it _is_ called, there is a _serious_ problem, because it
53 * won't generate true random numbers. So we must scream, panic,
54 * and exit immediately if that should happen.
56 int random_byte(void) {
57 MessageBox(hwnd, "Internal Error", APPNAME, MB_OK | MB_ICONERROR);
62 * This function is needed to link with the DES code. We need not
63 * have it do anything at all.
65 void logevent(char *msg) {
68 #define GET_32BIT(cp) \
69 (((unsigned long)(unsigned char)(cp)[0] << 24) | \
70 ((unsigned long)(unsigned char)(cp)[1] << 16) | \
71 ((unsigned long)(unsigned char)(cp)[2] << 8) | \
72 ((unsigned long)(unsigned char)(cp)[3]))
74 #define PUT_32BIT(cp, value) { \
75 (cp)[0] = (unsigned char)((value) >> 24); \
76 (cp)[1] = (unsigned char)((value) >> 16); \
77 (cp)[2] = (unsigned char)((value) >> 8); \
78 (cp)[3] = (unsigned char)(value); }
80 #define PASSPHRASE_MAXLEN 512
83 * Dialog-box function for the passphrase box.
85 static int CALLBACK PassphraseProc(HWND hwnd, UINT msg,
86 WPARAM wParam, LPARAM lParam) {
87 static char *passphrase;
91 passphrase = (char *)lParam;
95 switch (LOWORD(wParam)) {
105 case 102: /* edit box */
106 if (HIWORD(wParam) == EN_CHANGE) {
107 GetDlgItemText (hwnd, 102, passphrase, PASSPHRASE_MAXLEN-1);
108 passphrase[PASSPHRASE_MAXLEN-1] = '\0';
121 * This function loads a key from a file and adds it.
123 void add_keyfile(char *filename) {
124 char passphrase[PASSPHRASE_MAXLEN];
130 needs_pass = rsakey_encrypted(filename);
132 key = malloc(sizeof(*key));
136 dlgret = DialogBoxParam(instance, MAKEINTRESOURCE(210),
137 NULL, PassphraseProc,
141 return; /* operation cancelled */
145 ret = loadrsakey(filename, key, passphrase);
149 MessageBox(NULL, "Couldn't load public key.", APPNAME,
150 MB_OK | MB_ICONERROR);
154 if (add234(rsakeys, key) != key)
155 free(key); /* already present, don't waste RAM */
159 * This is the main agent function that answers messages.
161 void answer_msg(void *msg) {
162 unsigned char *p = msg;
163 unsigned char *ret = msg;
167 * Get the message type.
173 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
175 * Reply with SSH_AGENT_RSA_IDENTITIES_ANSWER.
183 * Count up the number and length of keys we hold.
186 for (key = first234(rsakeys, &e); key; key = next234(&e)) {
188 len += 4; /* length field */
189 len += ssh1_bignum_length(key->exponent);
190 len += ssh1_bignum_length(key->modulus);
191 len += 4 + strlen(key->comment);
195 * Packet header is the obvious five bytes, plus four
196 * bytes for the key count.
199 if (len > AGENT_MAX_MSGLEN)
200 goto failure; /* aaargh! too much stuff! */
201 PUT_32BIT(ret, len-4);
202 ret[4] = SSH_AGENT_RSA_IDENTITIES_ANSWER;
203 PUT_32BIT(ret+5, nkeys);
205 for (key = first234(rsakeys, &e); key; key = next234(&e)) {
206 PUT_32BIT(p, ssh1_bignum_bitcount(key->modulus));
208 p += ssh1_write_bignum(p, key->exponent);
209 p += ssh1_write_bignum(p, key->modulus);
210 PUT_32BIT(p, strlen(key->comment));
211 memcpy(p+4, key->comment, strlen(key->comment));
212 p += 4 + strlen(key->comment);
216 case SSH_AGENTC_RSA_CHALLENGE:
218 * Reply with either SSH_AGENT_RSA_RESPONSE or
219 * SSH_AGENT_FAILURE, depending on whether we have that key
223 struct RSAKey reqkey, *key;
224 Bignum challenge, response;
225 unsigned char response_source[48], response_md5[16];
226 struct MD5Context md5c;
230 p += ssh1_read_bignum(p, &reqkey.exponent);
231 p += ssh1_read_bignum(p, &reqkey.modulus);
232 p += ssh1_read_bignum(p, &challenge);
233 memcpy(response_source+32, p, 16); p += 16;
234 if (GET_32BIT(p) != 1 ||
235 (key = find234(rsakeys, &reqkey, NULL)) == NULL) {
236 freebn(reqkey.exponent);
237 freebn(reqkey.modulus);
241 response = rsadecrypt(challenge, key);
242 for (i = 0; i < 32; i++)
243 response_source[i] = bignum_byte(response, 31-i);
246 MD5Update(&md5c, response_source, 48);
247 MD5Final(response_md5, &md5c);
248 memset(response_source, 0, 48); /* burn the evidence */
249 freebn(response); /* and that evidence */
250 freebn(challenge); /* yes, and that evidence */
251 freebn(reqkey.exponent); /* and free some memory ... */
252 freebn(reqkey.modulus); /* ... while we're at it. */
255 * Packet is the obvious five byte header, plus sixteen
259 PUT_32BIT(ret, len-4);
260 ret[4] = SSH_AGENT_RSA_RESPONSE;
261 memcpy(ret+5, response_md5, 16);
264 #if 0 /* FIXME: implement these */
265 case SSH_AGENTC_ADD_RSA_IDENTITY:
267 * Add to the list and return SSH_AGENT_SUCCESS, or
268 * SSH_AGENT_FAILURE if the key was malformed.
271 case SSH_AGENTC_REMOVE_RSA_IDENTITY:
273 * Remove from the list and return SSH_AGENT_SUCCESS, or
274 * perhaps SSH_AGENT_FAILURE if it wasn't in the list to
282 * Unrecognised message. Return SSH_AGENT_FAILURE.
285 ret[4] = SSH_AGENT_FAILURE;
291 * Key comparison function for the 2-3-4 tree of RSA keys.
293 int cmpkeys(void *av, void *bv) {
294 struct RSAKey *a = (struct RSAKey *)av;
295 struct RSAKey *b = (struct RSAKey *)bv;
302 * Compare by length of moduli.
304 alen = ssh1_bignum_bitcount(am);
305 blen = ssh1_bignum_bitcount(bm);
306 if (alen > blen) return +1; else if (alen < blen) return -1;
308 * Now compare by moduli themselves.
310 alen = (alen + 7) / 8; /* byte count */
313 abyte = bignum_byte(am, alen);
314 bbyte = bignum_byte(bm, alen);
315 if (abyte > bbyte) return +1; else if (abyte < bbyte) return -1;
323 static void error(char *s) {
324 MessageBox(hwnd, s, APPNAME, MB_OK | MB_ICONERROR);
328 * Dialog-box function for the key list box.
330 static int CALLBACK KeyListProc(HWND hwnd, UINT msg,
331 WPARAM wParam, LPARAM lParam) {
335 char filename[FILENAME_MAX];
339 for (key = first234(rsakeys, &e); key; key = next234(&e)) {
340 SendDlgItemMessage (hwnd, 100, LB_ADDSTRING,
341 0, (LPARAM) key->comment);
345 switch (LOWORD(wParam)) {
351 case 101: /* add key */
352 if (HIWORD(wParam) == BN_CLICKED ||
353 HIWORD(wParam) == BN_DOUBLECLICKED) {
354 memset(&of, 0, sizeof(of));
355 #ifdef OPENFILENAME_SIZE_VERSION_400
356 of.lStructSize = OPENFILENAME_SIZE_VERSION_400;
358 of.lStructSize = sizeof(of);
361 of.lpstrFilter = "All Files\0*\0\0\0";
362 of.lpstrCustomFilter = NULL;
364 of.lpstrFile = filename; *filename = '\0';
365 of.nMaxFile = sizeof(filename);
366 of.lpstrFileTitle = NULL;
367 of.lpstrInitialDir = NULL;
368 of.lpstrTitle = "Select Public Key File";
370 if (GetOpenFileName(&of)) {
371 add_keyfile(filename);
373 SendDlgItemMessage(hwnd, 100, LB_RESETCONTENT, 0, 0);
374 for (key = first234(rsakeys, &e); key; key = next234(&e)) {
375 SendDlgItemMessage (hwnd, 100, LB_ADDSTRING,
376 0, (LPARAM) key->comment);
378 SendDlgItemMessage (hwnd, 100, LB_SETCURSEL, (WPARAM) -1, 0);
381 case 102: /* remove key */
382 if (HIWORD(wParam) == BN_CLICKED ||
383 HIWORD(wParam) == BN_DOUBLECLICKED) {
384 int n = SendDlgItemMessage (hwnd, 100, LB_GETCURSEL, 0, 0);
385 if (n == LB_ERR || n == 0) {
389 for (key = first234(rsakeys, &e); key; key = next234(&e))
392 del234(rsakeys, key);
393 freersakey(key); free(key);
394 SendDlgItemMessage(hwnd, 100, LB_RESETCONTENT, 0, 0);
395 for (key = first234(rsakeys, &e); key; key = next234(&e)) {
396 SendDlgItemMessage (hwnd, 100, LB_ADDSTRING,
397 0, (LPARAM) key->comment);
399 SendDlgItemMessage (hwnd, 100, LB_SETCURSEL, (WPARAM) -1, 0);
412 static LRESULT CALLBACK WndProc (HWND hwnd, UINT message,
413 WPARAM wParam, LPARAM lParam) {
415 static int menuinprogress;
419 if (lParam == WM_RBUTTONUP) {
421 GetCursorPos(&cursorpos);
422 PostMessage(hwnd, WM_SYSTRAY2, cursorpos.x, cursorpos.y);
426 if (!menuinprogress) {
428 SetForegroundWindow(hwnd);
429 ret = TrackPopupMenu(systray_menu,
430 TPM_RIGHTALIGN | TPM_BOTTOMALIGN |
432 wParam, lParam, 0, hwnd, NULL);
438 switch (wParam & ~0xF) { /* low 4 bits reserved to Windows */
440 SendMessage(hwnd, WM_CLOSE, 0, 0);
444 keylist = CreateDialog (instance, MAKEINTRESOURCE(211),
446 ShowWindow (keylist, SW_SHOWNORMAL);
459 HANDLE filemap, proc;
460 PSID mapowner, procowner;
461 PSECURITY_DESCRIPTOR psd1 = NULL, psd2 = NULL;
464 cds = (COPYDATASTRUCT *)lParam;
465 if (cds->dwData != AGENT_COPYDATA_ID)
466 return 0; /* not our message, mate */
467 mapname = (char *)cds->lpData;
468 if (mapname[cds->cbData - 1] != '\0')
469 return 0; /* failure to be ASCIZ! */
471 debug(("mapname is :%s:\r\n", mapname));
473 filemap = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, mapname);
475 debug(("filemap is %p\r\n", filemap));
477 if (filemap != NULL && filemap != INVALID_HANDLE_VALUE) {
479 if ((proc = OpenProcess(MAXIMUM_ALLOWED, FALSE,
480 GetCurrentProcessId())) == NULL) {
482 debug(("couldn't get handle for process\r\n"));
486 if (GetSecurityInfo(proc, SE_KERNEL_OBJECT,
487 OWNER_SECURITY_INFORMATION,
488 &procowner, NULL, NULL, NULL,
489 &psd2) != ERROR_SUCCESS) {
491 debug(("couldn't get owner info for process\r\n"));
494 return 0; /* unable to get security info */
497 if ((rc = GetSecurityInfo(filemap, SE_KERNEL_OBJECT,
498 OWNER_SECURITY_INFORMATION,
499 &mapowner, NULL, NULL, NULL,
500 &psd1) != ERROR_SUCCESS)) {
502 debug(("couldn't get owner info for filemap: %d\r\n", rc));
507 debug(("got security stuff\r\n"));
509 if (!EqualSid(mapowner, procowner))
510 return 0; /* security ID mismatch! */
512 debug(("security stuff matched\r\n"));
516 p = MapViewOfFile(filemap, FILE_MAP_WRITE, 0, 0, 0);
518 debug(("p is %p\r\n", p));
519 {int i; for(i=0;i<5;i++)debug(("p[%d]=%02x\r\n", i, ((unsigned char *)p)[i]));}
525 CloseHandle(filemap);
530 return DefWindowProc (hwnd, message, wParam, lParam);
533 int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show) {
541 wndclass.lpfnWndProc = WndProc;
542 wndclass.cbClsExtra = 0;
543 wndclass.cbWndExtra = 0;
544 wndclass.hInstance = inst;
545 wndclass.hIcon = LoadIcon (inst,
546 MAKEINTRESOURCE(IDI_MAINICON));
547 wndclass.hCursor = LoadCursor (NULL, IDC_IBEAM);
548 wndclass.hbrBackground = GetStockObject (BLACK_BRUSH);
549 wndclass.lpszMenuName = NULL;
550 wndclass.lpszClassName = APPNAME;
552 RegisterClass (&wndclass);
555 hwnd = keylist = NULL;
557 hwnd = CreateWindow (APPNAME, APPNAME,
558 WS_OVERLAPPEDWINDOW | WS_VSCROLL,
559 CW_USEDEFAULT, CW_USEDEFAULT,
560 100, 100, NULL, NULL, inst, NULL);
562 /* Set up a system tray icon */
568 #ifdef NIM_SETVERSION
570 res = Shell_NotifyIcon(NIM_SETVERSION, &tnid);
573 tnid.cbSize = sizeof(NOTIFYICONDATA);
575 tnid.uID = 1; /* unique within this systray use */
576 tnid.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
577 tnid.uCallbackMessage = WM_SYSTRAY;
578 tnid.hIcon = hicon = LoadIcon (instance, MAKEINTRESOURCE(201));
579 strcpy(tnid.szTip, "Pageant (PuTTY authentication agent)");
581 res = Shell_NotifyIcon(NIM_ADD, &tnid);
586 systray_menu = CreatePopupMenu();
587 AppendMenu (systray_menu, MF_ENABLED, IDM_VIEWKEYS, "View Keys");
588 AppendMenu (systray_menu, MF_ENABLED, IDM_CLOSE, "Terminate");
591 ShowWindow (hwnd, SW_HIDE);
594 * Initialise storage for RSA keys.
596 rsakeys = newtree234(cmpkeys);
599 * Process the command line and add RSA keys as listed on it.
600 * FIXME: we don't support spaces in filenames here. We should.
605 while (*p && isspace(*p)) p++;
606 if (*p && !isspace(*p)) {
608 while (*p && !isspace(*p)) p++;
618 while (GetMessage(&msg, NULL, 0, 0) == 1) {
619 TranslateMessage(&msg);
620 DispatchMessage(&msg);
623 /* Clean up the system tray icon */
627 tnid.cbSize = sizeof(NOTIFYICONDATA);
631 Shell_NotifyIcon(NIM_DELETE, &tnid);
633 DestroyMenu(systray_menu);