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 MAILSLOTNAME "\\\\.\\mailslot\\pageant_listener"
26 #define SSH_AGENTC_REQUEST_RSA_IDENTITIES 1
27 #define SSH_AGENT_RSA_IDENTITIES_ANSWER 2
28 #define SSH_AGENTC_RSA_CHALLENGE 3
29 #define SSH_AGENT_RSA_RESPONSE 4
30 #define SSH_AGENT_FAILURE 5
31 #define SSH_AGENT_SUCCESS 6
32 #define SSH_AGENTC_ADD_RSA_IDENTITY 7
33 #define SSH_AGENTC_REMOVE_RSA_IDENTITY 8
43 * We need this to link with the RSA code, because rsaencrypt()
44 * pads its data with random bytes. Since we only use rsadecrypt(),
45 * which is deterministic, this should never be called.
47 * If it _is_ called, there is a _serious_ problem, because it
48 * won't generate true random numbers. So we must scream, panic,
49 * and exit immediately if that should happen.
51 int random_byte(void) {
52 MessageBox(hwnd
, "Internal Error", APPNAME
, MB_OK
| MB_ICONERROR
);
57 * This function is needed to link with the DES code. We need not
58 * have it do anything at all.
60 void logevent(char *msg
) {
63 #define GET_32BIT(cp) \
64 (((unsigned long)(unsigned char)(cp)[0] << 24) | \
65 ((unsigned long)(unsigned char)(cp)[1] << 16) | \
66 ((unsigned long)(unsigned char)(cp)[2] << 8) | \
67 ((unsigned long)(unsigned char)(cp)[3]))
69 #define PUT_32BIT(cp, value) { \
70 (cp)[0] = (unsigned char)((value) >> 24); \
71 (cp)[1] = (unsigned char)((value) >> 16); \
72 (cp)[2] = (unsigned char)((value) >> 8); \
73 (cp)[3] = (unsigned char)(value); }
75 #define PASSPHRASE_MAXLEN 512
78 * Dialog-box function for the passphrase box.
80 static int CALLBACK
PassphraseProc(HWND hwnd
, UINT msg
,
81 WPARAM wParam
, LPARAM lParam
) {
82 static char *passphrase
;
86 passphrase
= (char *)lParam
;
90 switch (LOWORD(wParam
)) {
100 case 102: /* edit box */
101 if (HIWORD(wParam
) == EN_CHANGE
) {
102 GetDlgItemText (hwnd
, 102, passphrase
, PASSPHRASE_MAXLEN
-1);
103 passphrase
[PASSPHRASE_MAXLEN
-1] = '\0';
116 * This function loads a key from a file and adds it.
118 void add_keyfile(char *filename
) {
119 char passphrase
[PASSPHRASE_MAXLEN
];
125 needs_pass
= rsakey_encrypted(filename
);
127 key
= malloc(sizeof(*key
));
131 dlgret
= DialogBoxParam(instance
, MAKEINTRESOURCE(210),
132 NULL
, PassphraseProc
,
136 return; /* operation cancelled */
140 ret
= loadrsakey(filename
, key
, passphrase
);
144 MessageBox(NULL
, "Couldn't load public key.", APPNAME
,
145 MB_OK
| MB_ICONERROR
);
149 if (add234(rsakeys
, key
) != key
)
150 free(key
); /* already present, don't waste RAM */
154 * This is the main agent function that answers messages.
156 void answer_msg(void *in
, int inlen
, void **out
, int *outlen
) {
158 unsigned char *p
= in
;
161 *out
= NULL
; /* default `no go' response */
164 * Basic sanity checks. len >= 5, and len[0:4] holds len-4.
166 if (inlen
< 5 || GET_32BIT(p
) != (unsigned long)(inlen
-4))
170 * Get the message type.
177 case SSH_AGENTC_REQUEST_RSA_IDENTITIES
:
179 * Reply with SSH_AGENT_RSA_IDENTITIES_ANSWER.
187 * Count up the number and length of keys we hold.
190 for (key
= first234(rsakeys
, &e
); key
; key
= next234(&e
)) {
192 len
+= 4; /* length field */
193 len
+= ssh1_bignum_length(key
->exponent
);
194 len
+= ssh1_bignum_length(key
->modulus
);
195 len
+= 4 + strlen(key
->comment
);
199 * Packet header is the obvious five bytes, plus four
200 * bytes for the key count.
203 if ((ret
= malloc(len
)) != NULL
) {
204 PUT_32BIT(ret
, len
-4);
205 ret
[4] = SSH_AGENT_RSA_IDENTITIES_ANSWER
;
206 PUT_32BIT(ret
+5, nkeys
);
208 for (key
= first234(rsakeys
, &e
); key
; key
= next234(&e
)) {
209 PUT_32BIT(p
, ssh1_bignum_bitcount(key
->modulus
));
211 p
+= ssh1_write_bignum(p
, key
->exponent
);
212 p
+= ssh1_write_bignum(p
, key
->modulus
);
213 PUT_32BIT(p
, strlen(key
->comment
));
214 memcpy(p
+4, key
->comment
, strlen(key
->comment
));
215 p
+= 4 + strlen(key
->comment
);
220 case SSH_AGENTC_RSA_CHALLENGE
:
222 * Reply with either SSH_AGENT_RSA_RESPONSE or
223 * SSH_AGENT_FAILURE, depending on whether we have that key
227 struct RSAKey reqkey
, *key
;
228 Bignum challenge
, response
;
229 unsigned char response_source
[48], response_md5
[16];
230 struct MD5Context md5c
;
234 p
+= ssh1_read_bignum(p
, &reqkey
.exponent
);
235 p
+= ssh1_read_bignum(p
, &reqkey
.modulus
);
236 p
+= ssh1_read_bignum(p
, &challenge
);
237 memcpy(response_source
+32, p
, 16); p
+= 16;
238 if (GET_32BIT(p
) != 1 ||
239 (key
= find234(rsakeys
, &reqkey
, NULL
)) == NULL
) {
240 freebn(reqkey
.exponent
);
241 freebn(reqkey
.modulus
);
245 response
= rsadecrypt(challenge
, key
);
246 for (i
= 0; i
< 32; i
++)
247 response_source
[i
] = bignum_byte(response
, 31-i
);
250 MD5Update(&md5c
, response_source
, 48);
251 MD5Final(response_md5
, &md5c
);
252 memset(response_source
, 0, 48); /* burn the evidence */
253 freebn(response
); /* and that evidence */
254 freebn(challenge
); /* yes, and that evidence */
255 freebn(reqkey
.exponent
); /* and free some memory ... */
256 freebn(reqkey
.modulus
); /* ... while we're at it. */
259 * Packet is the obvious five byte header, plus sixteen
263 if ((ret
= malloc(len
)) != NULL
) {
264 PUT_32BIT(ret
, len
-4);
265 ret
[4] = SSH_AGENT_RSA_RESPONSE
;
266 memcpy(ret
+5, response_md5
, 16);
270 #if 0 /* FIXME: implement these */
271 case SSH_AGENTC_ADD_RSA_IDENTITY
:
273 * Add to the list and return SSH_AGENT_SUCCESS, or
274 * SSH_AGENT_FAILURE if the key was malformed.
277 case SSH_AGENTC_REMOVE_RSA_IDENTITY
:
279 * Remove from the list and return SSH_AGENT_SUCCESS, or
280 * perhaps SSH_AGENT_FAILURE if it wasn't in the list to
288 * Unrecognised message. Return SSH_AGENT_FAILURE.
290 if ((ret
= malloc(5)) != NULL
) {
292 ret
[4] = SSH_AGENT_FAILURE
;
299 *outlen
= 4 + GET_32BIT(ret
);
304 * Key comparison function for the 2-3-4 tree of RSA keys.
306 int cmpkeys(void *av
, void *bv
) {
307 struct RSAKey
*a
= (struct RSAKey
*)av
;
308 struct RSAKey
*b
= (struct RSAKey
*)bv
;
315 * Compare by length of moduli.
317 alen
= ssh1_bignum_bitcount(am
);
318 blen
= ssh1_bignum_bitcount(bm
);
319 if (alen
> blen
) return +1; else if (alen
< blen
) return -1;
321 * Now compare by moduli themselves.
323 alen
= (alen
+ 7) / 8; /* byte count */
326 abyte
= bignum_byte(am
, alen
);
327 bbyte
= bignum_byte(bm
, alen
);
328 if (abyte
> bbyte
) return +1; else if (abyte
< bbyte
) return -1;
336 static void error(char *s
) {
337 MessageBox(hwnd
, s
, APPNAME
, MB_OK
| MB_ICONERROR
);
341 * Dialog-box function for the key list box.
343 static int CALLBACK
KeyListProc(HWND hwnd
, UINT msg
,
344 WPARAM wParam
, LPARAM lParam
) {
348 char filename
[FILENAME_MAX
];
352 for (key
= first234(rsakeys
, &e
); key
; key
= next234(&e
)) {
353 SendDlgItemMessage (hwnd
, 100, LB_ADDSTRING
,
354 0, (LPARAM
) key
->comment
);
358 switch (LOWORD(wParam
)) {
364 case 101: /* add key */
365 if (HIWORD(wParam
) == BN_CLICKED
||
366 HIWORD(wParam
) == BN_DOUBLECLICKED
) {
367 memset(&of
, 0, sizeof(of
));
368 #ifdef OPENFILENAME_SIZE_VERSION_400
369 of
.lStructSize
= OPENFILENAME_SIZE_VERSION_400
;
371 of
.lStructSize
= sizeof(of
);
374 of
.lpstrFilter
= "All Files\0*\0\0\0";
375 of
.lpstrCustomFilter
= NULL
;
377 of
.lpstrFile
= filename
; *filename
= '\0';
378 of
.nMaxFile
= sizeof(filename
);
379 of
.lpstrFileTitle
= NULL
;
380 of
.lpstrInitialDir
= NULL
;
381 of
.lpstrTitle
= "Select Public Key File";
383 if (GetOpenFileName(&of
)) {
384 add_keyfile(filename
);
386 SendDlgItemMessage(hwnd
, 100, LB_RESETCONTENT
, 0, 0);
387 for (key
= first234(rsakeys
, &e
); key
; key
= next234(&e
)) {
388 SendDlgItemMessage (hwnd
, 100, LB_ADDSTRING
,
389 0, (LPARAM
) key
->comment
);
391 SendDlgItemMessage (hwnd
, 100, LB_SETCURSEL
, (WPARAM
) -1, 0);
394 case 102: /* remove key */
395 if (HIWORD(wParam
) == BN_CLICKED
||
396 HIWORD(wParam
) == BN_DOUBLECLICKED
) {
397 int n
= SendDlgItemMessage (hwnd
, 100, LB_GETCURSEL
, 0, 0);
398 if (n
== LB_ERR
|| n
== 0) {
402 for (key
= first234(rsakeys
, &e
); key
; key
= next234(&e
))
405 del234(rsakeys
, key
);
406 freersakey(key
); free(key
);
407 SendDlgItemMessage(hwnd
, 100, LB_RESETCONTENT
, 0, 0);
408 for (key
= first234(rsakeys
, &e
); key
; key
= next234(&e
)) {
409 SendDlgItemMessage (hwnd
, 100, LB_ADDSTRING
,
410 0, (LPARAM
) key
->comment
);
412 SendDlgItemMessage (hwnd
, 100, LB_SETCURSEL
, (WPARAM
) -1, 0);
425 static LRESULT CALLBACK
WndProc (HWND hwnd
, UINT message
,
426 WPARAM wParam
, LPARAM lParam
) {
428 static int menuinprogress
;
432 if (lParam
== WM_RBUTTONUP
) {
434 GetCursorPos(&cursorpos
);
435 PostMessage(hwnd
, WM_SYSTRAY2
, cursorpos
.x
, cursorpos
.y
);
439 if (!menuinprogress
) {
441 SetForegroundWindow(hwnd
);
442 ret
= TrackPopupMenu(systray_menu
,
443 TPM_RIGHTALIGN
| TPM_BOTTOMALIGN
|
445 wParam
, lParam
, 0, hwnd
, NULL
);
451 switch (wParam
& ~0xF) { /* low 4 bits reserved to Windows */
453 SendMessage(hwnd
, WM_CLOSE
, 0, 0);
457 keylist
= CreateDialog (instance
, MAKEINTRESOURCE(211),
459 ShowWindow (keylist
, SW_SHOWNORMAL
);
470 void *in
, *out
, *ret
;
476 cds
= (COPYDATASTRUCT
*)lParam
;
478 * FIXME: use dwData somehow.
482 answer_msg(in
, inlen
, &out
, &outlen
);
486 sprintf(mapname
, "PageantReply%08x", ++id
);
487 filemap
= CreateFileMapping(INVALID_HANDLE_VALUE
,
488 NULL
, PAGE_READWRITE
,
489 0, outlen
+sizeof(int),
491 } while (filemap
== INVALID_HANDLE_VALUE
);
492 ret
= MapViewOfFile(filemap
, FILE_MAP_WRITE
, 0, 0,
495 *((int *)ret
) = outlen
;
496 memcpy(((int *)ret
)+1, out
, outlen
);
497 UnmapViewOfFile(ret
);
501 return 0; /* invalid request */
511 return DefWindowProc (hwnd
, message
, wParam
, lParam
);
514 int WINAPI
WinMain(HINSTANCE inst
, HINSTANCE prev
, LPSTR cmdline
, int show
) {
523 wndclass
.lpfnWndProc
= WndProc
;
524 wndclass
.cbClsExtra
= 0;
525 wndclass
.cbWndExtra
= 0;
526 wndclass
.hInstance
= inst
;
527 wndclass
.hIcon
= LoadIcon (inst
,
528 MAKEINTRESOURCE(IDI_MAINICON
));
529 wndclass
.hCursor
= LoadCursor (NULL
, IDC_IBEAM
);
530 wndclass
.hbrBackground
= GetStockObject (BLACK_BRUSH
);
531 wndclass
.lpszMenuName
= NULL
;
532 wndclass
.lpszClassName
= APPNAME
;
534 RegisterClass (&wndclass
);
537 hwnd
= keylist
= NULL
;
539 hwnd
= CreateWindow (APPNAME
, APPNAME
,
540 WS_OVERLAPPEDWINDOW
| WS_VSCROLL
,
541 CW_USEDEFAULT
, CW_USEDEFAULT
,
542 100, 100, NULL
, NULL
, inst
, NULL
);
544 /* Set up a system tray icon */
550 #ifdef NIM_SETVERSION
552 res
= Shell_NotifyIcon(NIM_SETVERSION
, &tnid
);
555 tnid
.cbSize
= sizeof(NOTIFYICONDATA
);
557 tnid
.uID
= 1; /* unique within this systray use */
558 tnid
.uFlags
= NIF_MESSAGE
| NIF_ICON
| NIF_TIP
;
559 tnid
.uCallbackMessage
= WM_SYSTRAY
;
560 tnid
.hIcon
= hicon
= LoadIcon (instance
, MAKEINTRESOURCE(201));
561 strcpy(tnid
.szTip
, "Pageant (PuTTY authentication agent)");
563 res
= Shell_NotifyIcon(NIM_ADD
, &tnid
);
568 systray_menu
= CreatePopupMenu();
569 AppendMenu (systray_menu
, MF_ENABLED
, IDM_VIEWKEYS
, "View Keys");
570 AppendMenu (systray_menu
, MF_ENABLED
, IDM_CLOSE
, "Terminate");
573 ShowWindow (hwnd
, SW_HIDE
);
576 * Create the mailslot.
579 SECURITY_ATTRIBUTES sa
;
580 sa
.nLength
= sizeof(sa
);
581 sa
.lpSecurityDescriptor
= NULL
;
582 sa
.bInheritHandle
= TRUE
;
583 mailslot
= CreateMailslot(MAILSLOTNAME
, 0, 0, &sa
);
587 * Initialise storage for RSA keys.
589 rsakeys
= newtree234(cmpkeys
);
591 while (GetMessage(&msg
, NULL
, 0, 0) == 1) {
592 TranslateMessage(&msg
);
593 DispatchMessage(&msg
);
596 /* Clean up the system tray icon */
600 tnid
.cbSize
= sizeof(NOTIFYICONDATA
);
604 Shell_NotifyIcon(NIM_DELETE
, &tnid
);
606 DestroyMenu(systray_menu
);