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 /* FIXME: we can acquire comment here and use it in dialog */
131 needs_pass = rsakey_encrypted(filename, NULL);
133 key = malloc(sizeof(*key));
137 dlgret = DialogBoxParam(instance, MAKEINTRESOURCE(210),
138 NULL, PassphraseProc,
142 return; /* operation cancelled */
146 ret = loadrsakey(filename, key, passphrase);
150 MessageBox(NULL, "Couldn't load public key.", APPNAME,
151 MB_OK | MB_ICONERROR);
155 if (add234(rsakeys, key) != key)
156 free(key); /* already present, don't waste RAM */
160 * This is the main agent function that answers messages.
162 void answer_msg(void *msg) {
163 unsigned char *p = msg;
164 unsigned char *ret = msg;
168 * Get the message type.
174 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
176 * Reply with SSH_AGENT_RSA_IDENTITIES_ANSWER.
184 * Count up the number and length of keys we hold.
187 for (key = first234(rsakeys, &e); key; key = next234(&e)) {
189 len += 4; /* length field */
190 len += ssh1_bignum_length(key->exponent);
191 len += ssh1_bignum_length(key->modulus);
192 len += 4 + strlen(key->comment);
196 * Packet header is the obvious five bytes, plus four
197 * bytes for the key count.
200 if (len > AGENT_MAX_MSGLEN)
201 goto failure; /* aaargh! too much stuff! */
202 PUT_32BIT(ret, len-4);
203 ret[4] = SSH_AGENT_RSA_IDENTITIES_ANSWER;
204 PUT_32BIT(ret+5, nkeys);
206 for (key = first234(rsakeys, &e); key; key = next234(&e)) {
207 PUT_32BIT(p, ssh1_bignum_bitcount(key->modulus));
209 p += ssh1_write_bignum(p, key->exponent);
210 p += ssh1_write_bignum(p, key->modulus);
211 PUT_32BIT(p, strlen(key->comment));
212 memcpy(p+4, key->comment, strlen(key->comment));
213 p += 4 + strlen(key->comment);
217 case SSH_AGENTC_RSA_CHALLENGE:
219 * Reply with either SSH_AGENT_RSA_RESPONSE or
220 * SSH_AGENT_FAILURE, depending on whether we have that key
224 struct RSAKey reqkey, *key;
225 Bignum challenge, response;
226 unsigned char response_source[48], response_md5[16];
227 struct MD5Context md5c;
231 p += ssh1_read_bignum(p, &reqkey.exponent);
232 p += ssh1_read_bignum(p, &reqkey.modulus);
233 p += ssh1_read_bignum(p, &challenge);
234 memcpy(response_source+32, p, 16); p += 16;
235 if (GET_32BIT(p) != 1 ||
236 (key = find234(rsakeys, &reqkey, NULL)) == NULL) {
237 freebn(reqkey.exponent);
238 freebn(reqkey.modulus);
242 response = rsadecrypt(challenge, key);
243 for (i = 0; i < 32; i++)
244 response_source[i] = bignum_byte(response, 31-i);
247 MD5Update(&md5c, response_source, 48);
248 MD5Final(response_md5, &md5c);
249 memset(response_source, 0, 48); /* burn the evidence */
250 freebn(response); /* and that evidence */
251 freebn(challenge); /* yes, and that evidence */
252 freebn(reqkey.exponent); /* and free some memory ... */
253 freebn(reqkey.modulus); /* ... while we're at it. */
256 * Packet is the obvious five byte header, plus sixteen
260 PUT_32BIT(ret, len-4);
261 ret[4] = SSH_AGENT_RSA_RESPONSE;
262 memcpy(ret+5, response_md5, 16);
265 #if 0 /* FIXME: implement these */
266 case SSH_AGENTC_ADD_RSA_IDENTITY:
268 * Add to the list and return SSH_AGENT_SUCCESS, or
269 * SSH_AGENT_FAILURE if the key was malformed.
272 case SSH_AGENTC_REMOVE_RSA_IDENTITY:
274 * Remove from the list and return SSH_AGENT_SUCCESS, or
275 * perhaps SSH_AGENT_FAILURE if it wasn't in the list to
283 * Unrecognised message. Return SSH_AGENT_FAILURE.
286 ret[4] = SSH_AGENT_FAILURE;
292 * Key comparison function for the 2-3-4 tree of RSA keys.
294 int cmpkeys(void *av, void *bv) {
295 struct RSAKey *a = (struct RSAKey *)av;
296 struct RSAKey *b = (struct RSAKey *)bv;
303 * Compare by length of moduli.
305 alen = ssh1_bignum_bitcount(am);
306 blen = ssh1_bignum_bitcount(bm);
307 if (alen > blen) return +1; else if (alen < blen) return -1;
309 * Now compare by moduli themselves.
311 alen = (alen + 7) / 8; /* byte count */
314 abyte = bignum_byte(am, alen);
315 bbyte = bignum_byte(bm, alen);
316 if (abyte > bbyte) return +1; else if (abyte < bbyte) return -1;
324 static void error(char *s) {
325 MessageBox(hwnd, s, APPNAME, MB_OK | MB_ICONERROR);
329 * Dialog-box function for the key list box.
331 static int CALLBACK KeyListProc(HWND hwnd, UINT msg,
332 WPARAM wParam, LPARAM lParam) {
336 char filename[FILENAME_MAX];
340 for (key = first234(rsakeys, &e); key; key = next234(&e)) {
341 SendDlgItemMessage (hwnd, 100, LB_ADDSTRING,
342 0, (LPARAM) key->comment);
346 switch (LOWORD(wParam)) {
352 case 101: /* add key */
353 if (HIWORD(wParam) == BN_CLICKED ||
354 HIWORD(wParam) == BN_DOUBLECLICKED) {
355 memset(&of, 0, sizeof(of));
356 #ifdef OPENFILENAME_SIZE_VERSION_400
357 of.lStructSize = OPENFILENAME_SIZE_VERSION_400;
359 of.lStructSize = sizeof(of);
362 of.lpstrFilter = "All Files\0*\0\0\0";
363 of.lpstrCustomFilter = NULL;
365 of.lpstrFile = filename; *filename = '\0';
366 of.nMaxFile = sizeof(filename);
367 of.lpstrFileTitle = NULL;
368 of.lpstrInitialDir = NULL;
369 of.lpstrTitle = "Select Public Key File";
371 if (GetOpenFileName(&of)) {
372 add_keyfile(filename);
374 SendDlgItemMessage(hwnd, 100, LB_RESETCONTENT, 0, 0);
375 for (key = first234(rsakeys, &e); key; key = next234(&e)) {
376 SendDlgItemMessage (hwnd, 100, LB_ADDSTRING,
377 0, (LPARAM) key->comment);
379 SendDlgItemMessage (hwnd, 100, LB_SETCURSEL, (WPARAM) -1, 0);
382 case 102: /* remove key */
383 if (HIWORD(wParam) == BN_CLICKED ||
384 HIWORD(wParam) == BN_DOUBLECLICKED) {
385 int n = SendDlgItemMessage (hwnd, 100, LB_GETCURSEL, 0, 0);
386 if (n == LB_ERR || n == 0) {
390 for (key = first234(rsakeys, &e); key; key = next234(&e))
393 del234(rsakeys, key);
394 freersakey(key); free(key);
395 SendDlgItemMessage(hwnd, 100, LB_RESETCONTENT, 0, 0);
396 for (key = first234(rsakeys, &e); key; key = next234(&e)) {
397 SendDlgItemMessage (hwnd, 100, LB_ADDSTRING,
398 0, (LPARAM) key->comment);
400 SendDlgItemMessage (hwnd, 100, LB_SETCURSEL, (WPARAM) -1, 0);
413 static LRESULT CALLBACK WndProc (HWND hwnd, UINT message,
414 WPARAM wParam, LPARAM lParam) {
416 static int menuinprogress;
420 if (lParam == WM_RBUTTONUP) {
422 GetCursorPos(&cursorpos);
423 PostMessage(hwnd, WM_SYSTRAY2, cursorpos.x, cursorpos.y);
424 } else if (lParam == WM_LBUTTONDBLCLK) {
425 /* Equivalent to IDM_VIEWKEYS. */
426 PostMessage(hwnd, WM_COMMAND, IDM_VIEWKEYS, 0);
430 if (!menuinprogress) {
432 SetForegroundWindow(hwnd);
433 ret = TrackPopupMenu(systray_menu,
434 TPM_RIGHTALIGN | TPM_BOTTOMALIGN |
436 wParam, lParam, 0, hwnd, NULL);
442 switch (wParam & ~0xF) { /* low 4 bits reserved to Windows */
444 SendMessage(hwnd, WM_CLOSE, 0, 0);
448 keylist = CreateDialog (instance, MAKEINTRESOURCE(211),
450 ShowWindow (keylist, SW_SHOWNORMAL);
452 * Sometimes the window comes up minimised / hidden
453 * for no obvious reason. Prevent this.
455 SetForegroundWindow(keylist);
456 SetWindowPos (keylist, HWND_TOP, 0, 0, 0, 0,
457 SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
470 HANDLE filemap, proc;
471 PSID mapowner, procowner;
472 PSECURITY_DESCRIPTOR psd1 = NULL, psd2 = NULL;
475 cds = (COPYDATASTRUCT *)lParam;
476 if (cds->dwData != AGENT_COPYDATA_ID)
477 return 0; /* not our message, mate */
478 mapname = (char *)cds->lpData;
479 if (mapname[cds->cbData - 1] != '\0')
480 return 0; /* failure to be ASCIZ! */
482 debug(("mapname is :%s:\r\n", mapname));
484 filemap = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, mapname);
486 debug(("filemap is %p\r\n", filemap));
488 if (filemap != NULL && filemap != INVALID_HANDLE_VALUE) {
490 if ((proc = OpenProcess(MAXIMUM_ALLOWED, FALSE,
491 GetCurrentProcessId())) == NULL) {
493 debug(("couldn't get handle for process\r\n"));
497 if (GetSecurityInfo(proc, SE_KERNEL_OBJECT,
498 OWNER_SECURITY_INFORMATION,
499 &procowner, NULL, NULL, NULL,
500 &psd2) != ERROR_SUCCESS) {
502 debug(("couldn't get owner info for process\r\n"));
505 return 0; /* unable to get security info */
508 if ((rc = GetSecurityInfo(filemap, SE_KERNEL_OBJECT,
509 OWNER_SECURITY_INFORMATION,
510 &mapowner, NULL, NULL, NULL,
511 &psd1) != ERROR_SUCCESS)) {
513 debug(("couldn't get owner info for filemap: %d\r\n", rc));
518 debug(("got security stuff\r\n"));
520 if (!EqualSid(mapowner, procowner))
521 return 0; /* security ID mismatch! */
523 debug(("security stuff matched\r\n"));
527 p = MapViewOfFile(filemap, FILE_MAP_WRITE, 0, 0, 0);
529 debug(("p is %p\r\n", p));
530 {int i; for(i=0;i<5;i++)debug(("p[%d]=%02x\r\n", i, ((unsigned char *)p)[i]));}
536 CloseHandle(filemap);
541 return DefWindowProc (hwnd, message, wParam, lParam);
544 int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show) {
552 wndclass.lpfnWndProc = WndProc;
553 wndclass.cbClsExtra = 0;
554 wndclass.cbWndExtra = 0;
555 wndclass.hInstance = inst;
556 wndclass.hIcon = LoadIcon (inst,
557 MAKEINTRESOURCE(IDI_MAINICON));
558 wndclass.hCursor = LoadCursor (NULL, IDC_IBEAM);
559 wndclass.hbrBackground = GetStockObject (BLACK_BRUSH);
560 wndclass.lpszMenuName = NULL;
561 wndclass.lpszClassName = APPNAME;
563 RegisterClass (&wndclass);
566 hwnd = keylist = NULL;
568 hwnd = CreateWindow (APPNAME, APPNAME,
569 WS_OVERLAPPEDWINDOW | WS_VSCROLL,
570 CW_USEDEFAULT, CW_USEDEFAULT,
571 100, 100, NULL, NULL, inst, NULL);
573 /* Set up a system tray icon */
579 #ifdef NIM_SETVERSION
581 res = Shell_NotifyIcon(NIM_SETVERSION, &tnid);
584 tnid.cbSize = sizeof(NOTIFYICONDATA);
586 tnid.uID = 1; /* unique within this systray use */
587 tnid.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
588 tnid.uCallbackMessage = WM_SYSTRAY;
589 tnid.hIcon = hicon = LoadIcon (instance, MAKEINTRESOURCE(201));
590 strcpy(tnid.szTip, "Pageant (PuTTY authentication agent)");
592 res = Shell_NotifyIcon(NIM_ADD, &tnid);
597 systray_menu = CreatePopupMenu();
598 AppendMenu (systray_menu, MF_ENABLED, IDM_VIEWKEYS, "View Keys");
599 AppendMenu (systray_menu, MF_ENABLED, IDM_CLOSE, "Terminate");
602 ShowWindow (hwnd, SW_HIDE);
605 * Initialise storage for RSA keys.
607 rsakeys = newtree234(cmpkeys);
610 * Process the command line and add RSA keys as listed on it.
611 * FIXME: we don't support spaces in filenames here. We should.
616 while (*p && isspace(*p)) p++;
617 if (*p && !isspace(*p)) {
619 while (*p && !isspace(*p)) p++;
629 while (GetMessage(&msg, NULL, 0, 0) == 1) {
630 TranslateMessage(&msg);
631 DispatchMessage(&msg);
634 /* Clean up the system tray icon */
638 tnid.cbSize = sizeof(NOTIFYICONDATA);
642 Shell_NotifyIcon(NIM_DELETE, &tnid);
644 DestroyMenu(systray_menu);