2 * Pageant: the PuTTY Authentication Agent.
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)
18 #define AGENT_COPYDATA_ID 0x804e50ba /* random goop */
21 * FIXME: maybe some day we can sort this out ...
23 #define AGENT_MAX_MSGLEN 8192
25 #define IDM_CLOSE 0x0010
26 #define IDM_VIEWKEYS 0x0020
27 #define IDM_ABOUT 0x0030
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
51 typedef DWORD (WINAPI
*gsi_fn_t
)
52 (HANDLE
, SE_OBJECT_TYPE
, SECURITY_INFORMATION
,
53 PSID
*, PSID
*, PACL
*, PACL
*,
54 PSECURITY_DESCRIPTOR
*);
55 gsi_fn_t getsecurityinfo
;
58 * We need this to link with the RSA code, because rsaencrypt()
59 * pads its data with random bytes. Since we only use rsadecrypt(),
60 * which is deterministic, this should never be called.
62 * If it _is_ called, there is a _serious_ problem, because it
63 * won't generate true random numbers. So we must scream, panic,
64 * and exit immediately if that should happen.
66 int random_byte(void) {
67 MessageBox(hwnd
, "Internal Error", APPNAME
, MB_OK
| MB_ICONERROR
);
72 * This function is needed to link with the DES code. We need not
73 * have it do anything at all.
75 void logevent(char *msg
) {
78 #define GET_32BIT(cp) \
79 (((unsigned long)(unsigned char)(cp)[0] << 24) | \
80 ((unsigned long)(unsigned char)(cp)[1] << 16) | \
81 ((unsigned long)(unsigned char)(cp)[2] << 8) | \
82 ((unsigned long)(unsigned char)(cp)[3]))
84 #define PUT_32BIT(cp, value) { \
85 (cp)[0] = (unsigned char)((value) >> 24); \
86 (cp)[1] = (unsigned char)((value) >> 16); \
87 (cp)[2] = (unsigned char)((value) >> 8); \
88 (cp)[3] = (unsigned char)(value); }
90 #define PASSPHRASE_MAXLEN 512
92 struct PassphraseProcStruct
{
98 * Dialog-box function for the Licence box.
100 static int CALLBACK
LicenceProc (HWND hwnd
, UINT msg
,
101 WPARAM wParam
, LPARAM lParam
) {
106 switch (LOWORD(wParam
)) {
120 * Dialog-box function for the About box.
122 static int CALLBACK
AboutProc (HWND hwnd
, UINT msg
,
123 WPARAM wParam
, LPARAM lParam
) {
126 SetDlgItemText (hwnd
, 100, ver
);
129 switch (LOWORD(wParam
)) {
132 DestroyWindow (hwnd
);
135 EnableWindow(hwnd
, 0);
136 DialogBox (instance
, MAKEINTRESOURCE(214), NULL
, LicenceProc
);
137 EnableWindow(hwnd
, 1);
138 SetActiveWindow(hwnd
);
144 DestroyWindow (hwnd
);
151 * Dialog-box function for the passphrase box.
153 static int CALLBACK
PassphraseProc(HWND hwnd
, UINT msg
,
154 WPARAM wParam
, LPARAM lParam
) {
155 static char *passphrase
;
156 struct PassphraseProcStruct
*p
;
160 p
= (struct PassphraseProcStruct
*)lParam
;
161 passphrase
= p
->passphrase
;
163 SetDlgItemText(hwnd
, 101, p
->comment
);
167 switch (LOWORD(wParam
)) {
177 case 102: /* edit box */
178 if (HIWORD(wParam
) == EN_CHANGE
) {
179 GetDlgItemText (hwnd
, 102, passphrase
, PASSPHRASE_MAXLEN
-1);
180 passphrase
[PASSPHRASE_MAXLEN
-1] = '\0';
193 * Update the visible key list.
195 void keylist_update(void) {
200 SendDlgItemMessage(keylist
, 100, LB_RESETCONTENT
, 0, 0);
201 for (key
= first234(rsakeys
, &e
); key
; key
= next234(&e
)) {
202 char listentry
[512], *p
;
204 * Replace two spaces in the fingerprint with tabs, for
205 * nice alignment in the box.
207 rsa_fingerprint(listentry
, sizeof(listentry
), key
);
208 p
= strchr(listentry
, ' '); if (p
) *p
= '\t';
209 p
= strchr(listentry
, ' '); if (p
) *p
= '\t';
210 SendDlgItemMessage (keylist
, 100, LB_ADDSTRING
,
211 0, (LPARAM
)listentry
);
213 SendDlgItemMessage (keylist
, 100, LB_SETCURSEL
, (WPARAM
) -1, 0);
218 * This function loads a key from a file and adds it.
220 void add_keyfile(char *filename
) {
221 char passphrase
[PASSPHRASE_MAXLEN
];
227 struct PassphraseProcStruct pps
;
229 needs_pass
= rsakey_encrypted(filename
, &comment
);
231 key
= malloc(sizeof(*key
));
232 pps
.passphrase
= passphrase
;
233 pps
.comment
= comment
;
237 dlgret
= DialogBoxParam(instance
, MAKEINTRESOURCE(210),
238 NULL
, PassphraseProc
,
241 if (comment
) free(comment
);
243 return; /* operation cancelled */
247 ret
= loadrsakey(filename
, key
, passphrase
);
250 if (comment
) free(comment
);
252 MessageBox(NULL
, "Couldn't load private key.", APPNAME
,
253 MB_OK
| MB_ICONERROR
);
257 if (add234(rsakeys
, key
) != key
)
258 free(key
); /* already present, don't waste RAM */
262 * This is the main agent function that answers messages.
264 void answer_msg(void *msg
) {
265 unsigned char *p
= msg
;
266 unsigned char *ret
= msg
;
270 * Get the message type.
276 case SSH_AGENTC_REQUEST_RSA_IDENTITIES
:
278 * Reply with SSH_AGENT_RSA_IDENTITIES_ANSWER.
286 * Count up the number and length of keys we hold.
289 for (key
= first234(rsakeys
, &e
); key
; key
= next234(&e
)) {
291 len
+= 4; /* length field */
292 len
+= ssh1_bignum_length(key
->exponent
);
293 len
+= ssh1_bignum_length(key
->modulus
);
294 len
+= 4 + strlen(key
->comment
);
298 * Packet header is the obvious five bytes, plus four
299 * bytes for the key count.
302 if (len
> AGENT_MAX_MSGLEN
)
303 goto failure
; /* aaargh! too much stuff! */
304 PUT_32BIT(ret
, len
-4);
305 ret
[4] = SSH_AGENT_RSA_IDENTITIES_ANSWER
;
306 PUT_32BIT(ret
+5, nkeys
);
308 for (key
= first234(rsakeys
, &e
); key
; key
= next234(&e
)) {
309 PUT_32BIT(p
, ssh1_bignum_bitcount(key
->modulus
));
311 p
+= ssh1_write_bignum(p
, key
->exponent
);
312 p
+= ssh1_write_bignum(p
, key
->modulus
);
313 PUT_32BIT(p
, strlen(key
->comment
));
314 memcpy(p
+4, key
->comment
, strlen(key
->comment
));
315 p
+= 4 + strlen(key
->comment
);
319 case SSH_AGENTC_RSA_CHALLENGE
:
321 * Reply with either SSH_AGENT_RSA_RESPONSE or
322 * SSH_AGENT_FAILURE, depending on whether we have that key
326 struct RSAKey reqkey
, *key
;
327 Bignum challenge
, response
;
328 unsigned char response_source
[48], response_md5
[16];
329 struct MD5Context md5c
;
333 p
+= ssh1_read_bignum(p
, &reqkey
.exponent
);
334 p
+= ssh1_read_bignum(p
, &reqkey
.modulus
);
335 p
+= ssh1_read_bignum(p
, &challenge
);
336 memcpy(response_source
+32, p
, 16); p
+= 16;
337 if (GET_32BIT(p
) != 1 ||
338 (key
= find234(rsakeys
, &reqkey
, NULL
)) == NULL
) {
339 freebn(reqkey
.exponent
);
340 freebn(reqkey
.modulus
);
344 response
= rsadecrypt(challenge
, key
);
345 for (i
= 0; i
< 32; i
++)
346 response_source
[i
] = bignum_byte(response
, 31-i
);
349 MD5Update(&md5c
, response_source
, 48);
350 MD5Final(response_md5
, &md5c
);
351 memset(response_source
, 0, 48); /* burn the evidence */
352 freebn(response
); /* and that evidence */
353 freebn(challenge
); /* yes, and that evidence */
354 freebn(reqkey
.exponent
); /* and free some memory ... */
355 freebn(reqkey
.modulus
); /* ... while we're at it. */
358 * Packet is the obvious five byte header, plus sixteen
362 PUT_32BIT(ret
, len
-4);
363 ret
[4] = SSH_AGENT_RSA_RESPONSE
;
364 memcpy(ret
+5, response_md5
, 16);
367 case SSH_AGENTC_ADD_RSA_IDENTITY
:
369 * Add to the list and return SSH_AGENT_SUCCESS, or
370 * SSH_AGENT_FAILURE if the key was malformed.
375 key
= malloc(sizeof(struct RSAKey
));
376 memset(key
, 0, sizeof(key
));
377 p
+= makekey(p
, key
, NULL
, 1);
378 p
+= makeprivate(p
, key
);
379 p
+= ssh1_read_bignum(p
, NULL
); /* p^-1 mod q */
380 p
+= ssh1_read_bignum(p
, NULL
); /* p */
381 p
+= ssh1_read_bignum(p
, NULL
); /* q */
382 comment
= malloc(GET_32BIT(p
));
384 memcpy(comment
, p
+4, GET_32BIT(p
));
385 key
->comment
= comment
;
388 ret
[4] = SSH_AGENT_FAILURE
;
389 if (add234(rsakeys
, key
) == key
) {
391 ret
[4] = SSH_AGENT_SUCCESS
;
398 case SSH_AGENTC_REMOVE_RSA_IDENTITY
:
400 * Remove from the list and return SSH_AGENT_SUCCESS, or
401 * perhaps SSH_AGENT_FAILURE if it wasn't in the list to
405 struct RSAKey reqkey
, *key
;
407 p
+= makekey(p
, &reqkey
, NULL
, 0);
408 key
= find234(rsakeys
, &reqkey
, NULL
);
409 freebn(reqkey
.exponent
);
410 freebn(reqkey
.modulus
);
412 ret
[4] = SSH_AGENT_FAILURE
;
414 del234(rsakeys
, key
);
417 ret
[4] = SSH_AGENT_SUCCESS
;
424 * Unrecognised message. Return SSH_AGENT_FAILURE.
427 ret
[4] = SSH_AGENT_FAILURE
;
433 * Key comparison function for the 2-3-4 tree of RSA keys.
435 int cmpkeys(void *av
, void *bv
) {
436 struct RSAKey
*a
= (struct RSAKey
*)av
;
437 struct RSAKey
*b
= (struct RSAKey
*)bv
;
444 * Compare by length of moduli.
446 alen
= ssh1_bignum_bitcount(am
);
447 blen
= ssh1_bignum_bitcount(bm
);
448 if (alen
> blen
) return +1; else if (alen
< blen
) return -1;
450 * Now compare by moduli themselves.
452 alen
= (alen
+ 7) / 8; /* byte count */
455 abyte
= bignum_byte(am
, alen
);
456 bbyte
= bignum_byte(bm
, alen
);
457 if (abyte
> bbyte
) return +1; else if (abyte
< bbyte
) return -1;
465 static void error(char *s
) {
466 MessageBox(hwnd
, s
, APPNAME
, MB_OK
| MB_ICONERROR
);
470 * Dialog-box function for the key list box.
472 static int CALLBACK
KeyListProc(HWND hwnd
, UINT msg
,
473 WPARAM wParam
, LPARAM lParam
) {
477 char filename
[FILENAME_MAX
];
483 static int tabs
[2] = {25, 175};
484 SendDlgItemMessage (hwnd
, 100, LB_SETTABSTOPS
, 2,
490 switch (LOWORD(wParam
)) {
496 case 101: /* add key */
497 if (HIWORD(wParam
) == BN_CLICKED
||
498 HIWORD(wParam
) == BN_DOUBLECLICKED
) {
499 memset(&of
, 0, sizeof(of
));
500 #ifdef OPENFILENAME_SIZE_VERSION_400
501 of
.lStructSize
= OPENFILENAME_SIZE_VERSION_400
;
503 of
.lStructSize
= sizeof(of
);
506 of
.lpstrFilter
= "All Files\0*\0\0\0";
507 of
.lpstrCustomFilter
= NULL
;
509 of
.lpstrFile
= filename
; *filename
= '\0';
510 of
.nMaxFile
= sizeof(filename
);
511 of
.lpstrFileTitle
= NULL
;
512 of
.lpstrInitialDir
= NULL
;
513 of
.lpstrTitle
= "Select Private Key File";
515 if (GetOpenFileName(&of
)) {
516 add_keyfile(filename
);
521 case 102: /* remove key */
522 if (HIWORD(wParam
) == BN_CLICKED
||
523 HIWORD(wParam
) == BN_DOUBLECLICKED
) {
524 int n
= SendDlgItemMessage (hwnd
, 100, LB_GETCURSEL
, 0, 0);
529 for (key
= first234(rsakeys
, &e
); key
; key
= next234(&e
))
532 del234(rsakeys
, key
);
533 freersakey(key
); free(key
);
547 static LRESULT CALLBACK
WndProc (HWND hwnd
, UINT message
,
548 WPARAM wParam
, LPARAM lParam
) {
550 static int menuinprogress
;
554 if (lParam
== WM_RBUTTONUP
) {
556 GetCursorPos(&cursorpos
);
557 PostMessage(hwnd
, WM_SYSTRAY2
, cursorpos
.x
, cursorpos
.y
);
558 } else if (lParam
== WM_LBUTTONDBLCLK
) {
559 /* Equivalent to IDM_VIEWKEYS. */
560 PostMessage(hwnd
, WM_COMMAND
, IDM_VIEWKEYS
, 0);
564 if (!menuinprogress
) {
566 SetForegroundWindow(hwnd
);
567 ret
= TrackPopupMenu(systray_menu
,
568 TPM_RIGHTALIGN
| TPM_BOTTOMALIGN
|
570 wParam
, lParam
, 0, hwnd
, NULL
);
576 switch (wParam
& ~0xF) { /* low 4 bits reserved to Windows */
578 SendMessage(hwnd
, WM_CLOSE
, 0, 0);
582 keylist
= CreateDialog (instance
, MAKEINTRESOURCE(211),
584 ShowWindow (keylist
, SW_SHOWNORMAL
);
586 * Sometimes the window comes up minimised / hidden
587 * for no obvious reason. Prevent this.
589 SetForegroundWindow(keylist
);
590 SetWindowPos (keylist
, HWND_TOP
, 0, 0, 0, 0,
591 SWP_NOMOVE
| SWP_NOSIZE
| SWP_SHOWWINDOW
);
596 aboutbox
= CreateDialog (instance
, MAKEINTRESOURCE(213),
598 ShowWindow (aboutbox
, SW_SHOWNORMAL
);
600 * Sometimes the window comes up minimised / hidden
601 * for no obvious reason. Prevent this.
603 SetForegroundWindow(aboutbox
);
604 SetWindowPos (aboutbox
, HWND_TOP
, 0, 0, 0, 0,
605 SWP_NOMOVE
| SWP_NOSIZE
| SWP_SHOWWINDOW
);
618 HANDLE filemap
, proc
;
619 PSID mapowner
, procowner
;
620 PSECURITY_DESCRIPTOR psd1
= NULL
, psd2
= NULL
;
623 cds
= (COPYDATASTRUCT
*)lParam
;
624 if (cds
->dwData
!= AGENT_COPYDATA_ID
)
625 return 0; /* not our message, mate */
626 mapname
= (char *)cds
->lpData
;
627 if (mapname
[cds
->cbData
- 1] != '\0')
628 return 0; /* failure to be ASCIZ! */
630 debug(("mapname is :%s:\r\n", mapname
));
632 filemap
= OpenFileMapping(FILE_MAP_ALL_ACCESS
, FALSE
, mapname
);
634 debug(("filemap is %p\r\n", filemap
));
636 if (filemap
!= NULL
&& filemap
!= INVALID_HANDLE_VALUE
) {
639 if ((proc
= OpenProcess(MAXIMUM_ALLOWED
, FALSE
,
640 GetCurrentProcessId())) == NULL
) {
642 debug(("couldn't get handle for process\r\n"));
646 if (getsecurityinfo(proc
, SE_KERNEL_OBJECT
,
647 OWNER_SECURITY_INFORMATION
,
648 &procowner
, NULL
, NULL
, NULL
,
649 &psd2
) != ERROR_SUCCESS
) {
651 debug(("couldn't get owner info for process\r\n"));
654 return 0; /* unable to get security info */
657 if ((rc
= getsecurityinfo(filemap
, SE_KERNEL_OBJECT
,
658 OWNER_SECURITY_INFORMATION
,
659 &mapowner
, NULL
, NULL
, NULL
,
660 &psd1
) != ERROR_SUCCESS
)) {
662 debug(("couldn't get owner info for filemap: %d\r\n", rc
));
667 debug(("got security stuff\r\n"));
669 if (!EqualSid(mapowner
, procowner
))
670 return 0; /* security ID mismatch! */
672 debug(("security stuff matched\r\n"));
678 debug(("security APIs not present\r\n"));
681 p
= MapViewOfFile(filemap
, FILE_MAP_WRITE
, 0, 0, 0);
683 debug(("p is %p\r\n", p
));
684 {int i
; for(i
=0;i
<5;i
++)debug(("p[%d]=%02x\r\n", i
, ((unsigned char *)p
)[i
]));}
690 CloseHandle(filemap
);
695 return DefWindowProc (hwnd
, message
, wParam
, lParam
);
698 int WINAPI
WinMain(HINSTANCE inst
, HINSTANCE prev
, LPSTR cmdline
, int show
) {
705 * Determine whether we're an NT system (should have security
706 * APIs) or a non-NT system (don't do security).
708 memset(&osi
, 0, sizeof(OSVERSIONINFO
));
709 osi
.dwOSVersionInfoSize
= sizeof(OSVERSIONINFO
);
710 if (GetVersionEx(&osi
) && osi
.dwPlatformId
==VER_PLATFORM_WIN32_NT
) {
713 has_security
= FALSE
;
717 * Attempt to ge the security API we need.
719 advapi
= LoadLibrary("ADVAPI32.DLL");
720 getsecurityinfo
= (gsi_fn_t
)GetProcAddress(advapi
, "GetSecurityInfo");
721 if (!getsecurityinfo
) {
723 "Unable to access security APIs. Pageant will\n"
724 "not run, in case it causes a security breach.",
725 "Pageant Fatal Error", MB_ICONERROR
| MB_OK
);
732 * First bomb out totally if we are already running.
734 if (FindWindow("Pageant", "Pageant")) {
735 MessageBox(NULL
, "Pageant is already running", "Pageant Error",
736 MB_ICONERROR
| MB_OK
);
737 if (advapi
) FreeLibrary(advapi
);
745 wndclass
.lpfnWndProc
= WndProc
;
746 wndclass
.cbClsExtra
= 0;
747 wndclass
.cbWndExtra
= 0;
748 wndclass
.hInstance
= inst
;
749 wndclass
.hIcon
= LoadIcon (inst
,
750 MAKEINTRESOURCE(IDI_MAINICON
));
751 wndclass
.hCursor
= LoadCursor (NULL
, IDC_IBEAM
);
752 wndclass
.hbrBackground
= GetStockObject (BLACK_BRUSH
);
753 wndclass
.lpszMenuName
= NULL
;
754 wndclass
.lpszClassName
= APPNAME
;
756 RegisterClass (&wndclass
);
759 hwnd
= keylist
= NULL
;
761 hwnd
= CreateWindow (APPNAME
, APPNAME
,
762 WS_OVERLAPPEDWINDOW
| WS_VSCROLL
,
763 CW_USEDEFAULT
, CW_USEDEFAULT
,
764 100, 100, NULL
, NULL
, inst
, NULL
);
766 /* Set up a system tray icon */
772 #ifdef NIM_SETVERSION
774 res
= Shell_NotifyIcon(NIM_SETVERSION
, &tnid
);
777 tnid
.cbSize
= sizeof(NOTIFYICONDATA
);
779 tnid
.uID
= 1; /* unique within this systray use */
780 tnid
.uFlags
= NIF_MESSAGE
| NIF_ICON
| NIF_TIP
;
781 tnid
.uCallbackMessage
= WM_SYSTRAY
;
782 tnid
.hIcon
= hicon
= LoadIcon (instance
, MAKEINTRESOURCE(201));
783 strcpy(tnid
.szTip
, "Pageant (PuTTY authentication agent)");
785 res
= Shell_NotifyIcon(NIM_ADD
, &tnid
);
790 systray_menu
= CreatePopupMenu();
791 /* accelerators used: vxa */
792 AppendMenu (systray_menu
, MF_ENABLED
, IDM_VIEWKEYS
, "&View Keys");
793 AppendMenu (systray_menu
, MF_ENABLED
, IDM_ABOUT
, "&About");
794 AppendMenu (systray_menu
, MF_ENABLED
, IDM_CLOSE
, "E&xit");
797 ShowWindow (hwnd
, SW_HIDE
);
800 * Initialise storage for RSA keys.
802 rsakeys
= newtree234(cmpkeys
);
805 * Process the command line and add RSA keys as listed on it.
812 while (*p
&& isspace(*p
)) p
++;
813 if (*p
&& !isspace(*p
)) {
814 char *q
= p
, *pp
= p
;
815 while (*p
&& (inquotes
|| !isspace(*p
)))
818 inquotes
= !inquotes
;
836 while (GetMessage(&msg
, NULL
, 0, 0) == 1) {
837 TranslateMessage(&msg
);
838 DispatchMessage(&msg
);
841 /* Clean up the system tray icon */
845 tnid
.cbSize
= sizeof(NOTIFYICONDATA
);
849 Shell_NotifyIcon(NIM_DELETE
, &tnid
);
851 DestroyMenu(systray_menu
);
854 if (advapi
) FreeLibrary(advapi
);