2 * Pageant: the PuTTY Authentication Agent.
6 #include <stdio.h> /* FIXME */
7 #include "putty.h" /* FIXME */
11 #define IDI_MAINICON 200
12 #define IDI_TRAYICON 201
14 #define WM_XUSER (WM_USER + 0x2000)
15 #define WM_SYSTRAY (WM_XUSER + 6)
16 #define WM_SYSTRAY2 (WM_XUSER + 7)
17 #define WM_CLOSEMEM (WM_XUSER + 10)
19 #define IDM_CLOSE 0x0010
20 #define IDM_VIEWKEYS 0x0020
22 #define APPNAME "Pageant"
24 #define SSH_AGENTC_REQUEST_RSA_IDENTITIES 1
25 #define SSH_AGENT_RSA_IDENTITIES_ANSWER 2
26 #define SSH_AGENTC_RSA_CHALLENGE 3
27 #define SSH_AGENT_RSA_RESPONSE 4
28 #define SSH_AGENT_FAILURE 5
29 #define SSH_AGENT_SUCCESS 6
30 #define SSH_AGENTC_ADD_RSA_IDENTITY 7
31 #define SSH_AGENTC_REMOVE_RSA_IDENTITY 8
41 * We need this to link with the RSA code, because rsaencrypt()
42 * pads its data with random bytes. Since we only use rsadecrypt(),
43 * which is deterministic, this should never be called.
45 * If it _is_ called, there is a _serious_ problem, because it
46 * won't generate true random numbers. So we must scream, panic,
47 * and exit immediately if that should happen.
49 int random_byte(void) {
50 MessageBox(hwnd, "Internal Error", APPNAME, MB_OK | MB_ICONERROR);
55 * This function is needed to link with the DES code. We need not
56 * have it do anything at all.
58 void logevent(char *msg) {
61 #define GET_32BIT(cp) \
62 (((unsigned long)(unsigned char)(cp)[0] << 24) | \
63 ((unsigned long)(unsigned char)(cp)[1] << 16) | \
64 ((unsigned long)(unsigned char)(cp)[2] << 8) | \
65 ((unsigned long)(unsigned char)(cp)[3]))
67 #define PUT_32BIT(cp, value) { \
68 (cp)[0] = (unsigned char)((value) >> 24); \
69 (cp)[1] = (unsigned char)((value) >> 16); \
70 (cp)[2] = (unsigned char)((value) >> 8); \
71 (cp)[3] = (unsigned char)(value); }
73 #define PASSPHRASE_MAXLEN 512
76 * Dialog-box function for the passphrase box.
78 static int CALLBACK PassphraseProc(HWND hwnd, UINT msg,
79 WPARAM wParam, LPARAM lParam) {
80 static char *passphrase;
84 passphrase = (char *)lParam;
88 switch (LOWORD(wParam)) {
98 case 102: /* edit box */
99 if (HIWORD(wParam) == EN_CHANGE) {
100 GetDlgItemText (hwnd, 102, passphrase, PASSPHRASE_MAXLEN-1);
101 passphrase[PASSPHRASE_MAXLEN-1] = '\0';
114 * This function loads a key from a file and adds it.
116 void add_keyfile(char *filename) {
117 char passphrase[PASSPHRASE_MAXLEN];
123 needs_pass = rsakey_encrypted(filename);
125 key = malloc(sizeof(*key));
129 dlgret = DialogBoxParam(instance, MAKEINTRESOURCE(210),
130 NULL, PassphraseProc,
134 return; /* operation cancelled */
138 ret = loadrsakey(filename, key, passphrase);
142 MessageBox(NULL, "Couldn't load public key.", APPNAME,
143 MB_OK | MB_ICONERROR);
147 if (add234(rsakeys, key) != key)
148 free(key); /* already present, don't waste RAM */
152 * This is the main agent function that answers messages.
154 void answer_msg(void *in, int inlen, void **out, int *outlen) {
156 unsigned char *p = in;
159 *out = NULL; /* default `no go' response */
162 * Basic sanity checks. len >= 5, and len[0:4] holds len-4.
164 if (inlen < 5 || GET_32BIT(p) != (unsigned long)(inlen-4))
168 * Get the message type.
175 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
177 * Reply with SSH_AGENT_RSA_IDENTITIES_ANSWER.
185 * Count up the number and length of keys we hold.
188 for (key = first234(rsakeys, &e); key; key = next234(&e)) {
190 len += 4; /* length field */
191 len += ssh1_bignum_length(key->exponent);
192 len += ssh1_bignum_length(key->modulus);
193 len += 4 + strlen(key->comment);
197 * Packet header is the obvious five bytes, plus four
198 * bytes for the key count.
201 if ((ret = malloc(len)) != NULL) {
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);
218 case SSH_AGENTC_RSA_CHALLENGE:
220 * Reply with either SSH_AGENT_RSA_RESPONSE or
221 * SSH_AGENT_FAILURE, depending on whether we have that key
225 struct RSAKey reqkey, *key;
226 Bignum challenge, response;
227 unsigned char response_source[48], response_md5[16];
228 struct MD5Context md5c;
232 p += ssh1_read_bignum(p, &reqkey.exponent);
233 p += ssh1_read_bignum(p, &reqkey.modulus);
234 p += ssh1_read_bignum(p, &challenge);
235 memcpy(response_source+32, p, 16); p += 16;
236 if (GET_32BIT(p) != 1 ||
237 (key = find234(rsakeys, &reqkey, NULL)) == NULL) {
238 freebn(reqkey.exponent);
239 freebn(reqkey.modulus);
243 response = rsadecrypt(challenge, key);
244 for (i = 0; i < 32; i++)
245 response_source[i] = bignum_byte(response, 31-i);
248 MD5Update(&md5c, response_source, 48);
249 MD5Final(response_md5, &md5c);
250 memset(response_source, 0, 48); /* burn the evidence */
251 freebn(response); /* and that evidence */
252 freebn(challenge); /* yes, and that evidence */
253 freebn(reqkey.exponent); /* and free some memory ... */
254 freebn(reqkey.modulus); /* ... while we're at it. */
257 * Packet is the obvious five byte header, plus sixteen
261 if ((ret = malloc(len)) != NULL) {
262 PUT_32BIT(ret, len-4);
263 ret[4] = SSH_AGENT_RSA_RESPONSE;
264 memcpy(ret+5, response_md5, 16);
268 #if 0 /* FIXME: implement these */
269 case SSH_AGENTC_ADD_RSA_IDENTITY:
271 * Add to the list and return SSH_AGENT_SUCCESS, or
272 * SSH_AGENT_FAILURE if the key was malformed.
275 case SSH_AGENTC_REMOVE_RSA_IDENTITY:
277 * Remove from the list and return SSH_AGENT_SUCCESS, or
278 * perhaps SSH_AGENT_FAILURE if it wasn't in the list to
286 * Unrecognised message. Return SSH_AGENT_FAILURE.
288 if ((ret = malloc(5)) != NULL) {
290 ret[4] = SSH_AGENT_FAILURE;
297 *outlen = 4 + GET_32BIT(ret);
302 * Key comparison function for the 2-3-4 tree of RSA keys.
304 int cmpkeys(void *av, void *bv) {
305 struct RSAKey *a = (struct RSAKey *)av;
306 struct RSAKey *b = (struct RSAKey *)bv;
313 * Compare by length of moduli.
315 alen = ssh1_bignum_bitcount(am);
316 blen = ssh1_bignum_bitcount(bm);
317 if (alen > blen) return +1; else if (alen < blen) return -1;
319 * Now compare by moduli themselves.
321 alen = (alen + 7) / 8; /* byte count */
324 abyte = bignum_byte(am, alen);
325 bbyte = bignum_byte(bm, alen);
326 if (abyte > bbyte) return +1; else if (abyte < bbyte) return -1;
334 static void error(char *s) {
335 MessageBox(hwnd, s, APPNAME, MB_OK | MB_ICONERROR);
339 * Dialog-box function for the key list box.
341 static int CALLBACK KeyListProc(HWND hwnd, UINT msg,
342 WPARAM wParam, LPARAM lParam) {
346 char filename[FILENAME_MAX];
350 for (key = first234(rsakeys, &e); key; key = next234(&e)) {
351 SendDlgItemMessage (hwnd, 100, LB_ADDSTRING,
352 0, (LPARAM) key->comment);
356 switch (LOWORD(wParam)) {
362 case 101: /* add key */
363 if (HIWORD(wParam) == BN_CLICKED ||
364 HIWORD(wParam) == BN_DOUBLECLICKED) {
365 memset(&of, 0, sizeof(of));
366 #ifdef OPENFILENAME_SIZE_VERSION_400
367 of.lStructSize = OPENFILENAME_SIZE_VERSION_400;
369 of.lStructSize = sizeof(of);
372 of.lpstrFilter = "All Files\0*\0\0\0";
373 of.lpstrCustomFilter = NULL;
375 of.lpstrFile = filename; *filename = '\0';
376 of.nMaxFile = sizeof(filename);
377 of.lpstrFileTitle = NULL;
378 of.lpstrInitialDir = NULL;
379 of.lpstrTitle = "Select Public Key File";
381 if (GetOpenFileName(&of)) {
382 add_keyfile(filename);
384 SendDlgItemMessage(hwnd, 100, LB_RESETCONTENT, 0, 0);
385 for (key = first234(rsakeys, &e); key; key = next234(&e)) {
386 SendDlgItemMessage (hwnd, 100, LB_ADDSTRING,
387 0, (LPARAM) key->comment);
389 SendDlgItemMessage (hwnd, 100, LB_SETCURSEL, (WPARAM) -1, 0);
392 case 102: /* remove key */
393 if (HIWORD(wParam) == BN_CLICKED ||
394 HIWORD(wParam) == BN_DOUBLECLICKED) {
395 int n = SendDlgItemMessage (hwnd, 100, LB_GETCURSEL, 0, 0);
396 if (n == LB_ERR || n == 0) {
400 for (key = first234(rsakeys, &e); key; key = next234(&e))
403 del234(rsakeys, key);
404 freersakey(key); free(key);
405 SendDlgItemMessage(hwnd, 100, LB_RESETCONTENT, 0, 0);
406 for (key = first234(rsakeys, &e); key; key = next234(&e)) {
407 SendDlgItemMessage (hwnd, 100, LB_ADDSTRING,
408 0, (LPARAM) key->comment);
410 SendDlgItemMessage (hwnd, 100, LB_SETCURSEL, (WPARAM) -1, 0);
423 static LRESULT CALLBACK WndProc (HWND hwnd, UINT message,
424 WPARAM wParam, LPARAM lParam) {
426 static int menuinprogress;
430 if (lParam == WM_RBUTTONUP) {
432 GetCursorPos(&cursorpos);
433 PostMessage(hwnd, WM_SYSTRAY2, cursorpos.x, cursorpos.y);
437 if (!menuinprogress) {
439 SetForegroundWindow(hwnd);
440 ret = TrackPopupMenu(systray_menu,
441 TPM_RIGHTALIGN | TPM_BOTTOMALIGN |
443 wParam, lParam, 0, hwnd, NULL);
449 switch (wParam & ~0xF) { /* low 4 bits reserved to Windows */
451 SendMessage(hwnd, WM_CLOSE, 0, 0);
455 keylist = CreateDialog (instance, MAKEINTRESOURCE(211),
457 ShowWindow (keylist, SW_SHOWNORMAL);
468 void *in, *out, *ret;
474 cds = (COPYDATASTRUCT *)lParam;
476 * FIXME: use dwData somehow.
480 answer_msg(in, inlen, &out, &outlen);
484 sprintf(mapname, "PageantReply%08x", ++id);
485 filemap = CreateFileMapping(INVALID_HANDLE_VALUE,
486 NULL, PAGE_READWRITE,
487 0, outlen+sizeof(int),
489 } while (filemap == INVALID_HANDLE_VALUE);
490 ret = MapViewOfFile(filemap, FILE_MAP_WRITE, 0, 0,
493 *((int *)ret) = outlen;
494 memcpy(((int *)ret)+1, out, outlen);
495 UnmapViewOfFile(ret);
499 return 0; /* invalid request */
509 return DefWindowProc (hwnd, message, wParam, lParam);
512 int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show) {
520 wndclass.lpfnWndProc = WndProc;
521 wndclass.cbClsExtra = 0;
522 wndclass.cbWndExtra = 0;
523 wndclass.hInstance = inst;
524 wndclass.hIcon = LoadIcon (inst,
525 MAKEINTRESOURCE(IDI_MAINICON));
526 wndclass.hCursor = LoadCursor (NULL, IDC_IBEAM);
527 wndclass.hbrBackground = GetStockObject (BLACK_BRUSH);
528 wndclass.lpszMenuName = NULL;
529 wndclass.lpszClassName = APPNAME;
531 RegisterClass (&wndclass);
534 hwnd = keylist = NULL;
536 hwnd = CreateWindow (APPNAME, APPNAME,
537 WS_OVERLAPPEDWINDOW | WS_VSCROLL,
538 CW_USEDEFAULT, CW_USEDEFAULT,
539 100, 100, NULL, NULL, inst, NULL);
541 /* Set up a system tray icon */
547 #ifdef NIM_SETVERSION
549 res = Shell_NotifyIcon(NIM_SETVERSION, &tnid);
552 tnid.cbSize = sizeof(NOTIFYICONDATA);
554 tnid.uID = 1; /* unique within this systray use */
555 tnid.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
556 tnid.uCallbackMessage = WM_SYSTRAY;
557 tnid.hIcon = hicon = LoadIcon (instance, MAKEINTRESOURCE(201));
558 strcpy(tnid.szTip, "Pageant (PuTTY authentication agent)");
560 res = Shell_NotifyIcon(NIM_ADD, &tnid);
565 systray_menu = CreatePopupMenu();
566 AppendMenu (systray_menu, MF_ENABLED, IDM_VIEWKEYS, "View Keys");
567 AppendMenu (systray_menu, MF_ENABLED, IDM_CLOSE, "Terminate");
570 ShowWindow (hwnd, SW_HIDE);
573 * Initialise storage for RSA keys.
575 rsakeys = newtree234(cmpkeys);
578 * Process the command line and add RSA keys as listed on it.
579 * FIXME: we don't support spaces in filenames here. We should.
584 while (*p && isspace(*p)) p++;
585 if (*p && !isspace(*p)) {
587 while (*p && !isspace(*p)) p++;
597 while (GetMessage(&msg, NULL, 0, 0) == 1) {
598 TranslateMessage(&msg);
599 DispatchMessage(&msg);
602 /* Clean up the system tray icon */
606 tnid.cbSize = sizeof(NOTIFYICONDATA);
610 Shell_NotifyIcon(NIM_DELETE, &tnid);
612 DestroyMenu(systray_menu);