2 * Pageant: the PuTTY Authentication Agent.
13 #define IDI_MAINICON 200
14 #define IDI_TRAYICON 201
16 #define WM_XUSER (WM_USER + 0x2000)
17 #define WM_SYSTRAY (WM_XUSER + 6)
18 #define WM_SYSTRAY2 (WM_XUSER + 7)
20 #define AGENT_COPYDATA_ID 0x804e50ba /* random goop */
23 * FIXME: maybe some day we can sort this out ...
25 #define AGENT_MAX_MSGLEN 8192
27 #define IDM_CLOSE 0x0010
28 #define IDM_VIEWKEYS 0x0020
29 #define IDM_ABOUT 0x0030
31 #define APPNAME "Pageant"
33 #define SSH_AGENTC_REQUEST_RSA_IDENTITIES 1
34 #define SSH_AGENT_RSA_IDENTITIES_ANSWER 2
35 #define SSH_AGENTC_RSA_CHALLENGE 3
36 #define SSH_AGENT_RSA_RESPONSE 4
37 #define SSH_AGENT_FAILURE 5
38 #define SSH_AGENT_SUCCESS 6
39 #define SSH_AGENTC_ADD_RSA_IDENTITY 7
40 #define SSH_AGENTC_REMOVE_RSA_IDENTITY 8
44 static HINSTANCE instance
;
48 static HMENU systray_menu
;
50 static tree234
*rsakeys
;
52 static int has_security
;
54 typedef DWORD (WINAPI
*gsi_fn_t
)
55 (HANDLE
, SE_OBJECT_TYPE
, SECURITY_INFORMATION
,
56 PSID
*, PSID
*, PACL
*, PACL
*,
57 PSECURITY_DESCRIPTOR
*);
58 static gsi_fn_t getsecurityinfo
;
62 * We need this to link with the RSA code, because rsaencrypt()
63 * pads its data with random bytes. Since we only use rsadecrypt(),
64 * which is deterministic, this should never be called.
66 * If it _is_ called, there is a _serious_ problem, because it
67 * won't generate true random numbers. So we must scream, panic,
68 * and exit immediately if that should happen.
70 int random_byte(void) {
71 MessageBox(hwnd
, "Internal Error", APPNAME
, MB_OK
| MB_ICONERROR
);
76 * This function is needed to link with the DES code. We need not
77 * have it do anything at all.
79 void logevent(char *msg
) {
82 #define GET_32BIT(cp) \
83 (((unsigned long)(unsigned char)(cp)[0] << 24) | \
84 ((unsigned long)(unsigned char)(cp)[1] << 16) | \
85 ((unsigned long)(unsigned char)(cp)[2] << 8) | \
86 ((unsigned long)(unsigned char)(cp)[3]))
88 #define PUT_32BIT(cp, value) { \
89 (cp)[0] = (unsigned char)((value) >> 24); \
90 (cp)[1] = (unsigned char)((value) >> 16); \
91 (cp)[2] = (unsigned char)((value) >> 8); \
92 (cp)[3] = (unsigned char)(value); }
94 #define PASSPHRASE_MAXLEN 512
96 struct PassphraseProcStruct
{
102 * Dialog-box function for the Licence box.
104 static int CALLBACK
LicenceProc (HWND hwnd
, UINT msg
,
105 WPARAM wParam
, LPARAM lParam
) {
110 switch (LOWORD(wParam
)) {
124 * Dialog-box function for the About box.
126 static int CALLBACK
AboutProc (HWND hwnd
, UINT msg
,
127 WPARAM wParam
, LPARAM lParam
) {
130 SetDlgItemText (hwnd
, 100, ver
);
133 switch (LOWORD(wParam
)) {
136 DestroyWindow (hwnd
);
139 EnableWindow(hwnd
, 0);
140 DialogBox (instance
, MAKEINTRESOURCE(214), NULL
, LicenceProc
);
141 EnableWindow(hwnd
, 1);
142 SetActiveWindow(hwnd
);
148 DestroyWindow (hwnd
);
155 * Dialog-box function for the passphrase box.
157 static int CALLBACK
PassphraseProc(HWND hwnd
, UINT msg
,
158 WPARAM wParam
, LPARAM lParam
) {
159 static char *passphrase
;
160 struct PassphraseProcStruct
*p
;
164 p
= (struct PassphraseProcStruct
*)lParam
;
165 passphrase
= p
->passphrase
;
167 SetDlgItemText(hwnd
, 101, p
->comment
);
171 switch (LOWORD(wParam
)) {
181 case 102: /* edit box */
182 if (HIWORD(wParam
) == EN_CHANGE
) {
183 GetDlgItemText (hwnd
, 102, passphrase
, PASSPHRASE_MAXLEN
-1);
184 passphrase
[PASSPHRASE_MAXLEN
-1] = '\0';
197 * Update the visible key list.
199 static void keylist_update(void) {
204 SendDlgItemMessage(keylist
, 100, LB_RESETCONTENT
, 0, 0);
205 for (key
= first234(rsakeys
, &e
); key
; key
= next234(&e
)) {
206 char listentry
[512], *p
;
208 * Replace two spaces in the fingerprint with tabs, for
209 * nice alignment in the box.
211 rsa_fingerprint(listentry
, sizeof(listentry
), key
);
212 p
= strchr(listentry
, ' '); if (p
) *p
= '\t';
213 p
= strchr(listentry
, ' '); if (p
) *p
= '\t';
214 SendDlgItemMessage (keylist
, 100, LB_ADDSTRING
,
215 0, (LPARAM
)listentry
);
217 SendDlgItemMessage (keylist
, 100, LB_SETCURSEL
, (WPARAM
) -1, 0);
222 * This function loads a key from a file and adds it.
224 static void add_keyfile(char *filename
) {
225 char passphrase
[PASSPHRASE_MAXLEN
];
231 struct PassphraseProcStruct pps
;
233 needs_pass
= rsakey_encrypted(filename
, &comment
);
235 key
= malloc(sizeof(*key
));
236 pps
.passphrase
= passphrase
;
237 pps
.comment
= comment
;
241 dlgret
= DialogBoxParam(instance
, MAKEINTRESOURCE(210),
242 NULL
, PassphraseProc
,
245 if (comment
) free(comment
);
247 return; /* operation cancelled */
251 ret
= loadrsakey(filename
, key
, passphrase
);
254 if (comment
) free(comment
);
256 MessageBox(NULL
, "Couldn't load private key.", APPNAME
,
257 MB_OK
| MB_ICONERROR
);
261 if (add234(rsakeys
, key
) != key
)
262 free(key
); /* already present, don't waste RAM */
266 * This is the main agent function that answers messages.
268 static void answer_msg(void *msg
) {
269 unsigned char *p
= msg
;
270 unsigned char *ret
= msg
;
274 * Get the message type.
280 case SSH_AGENTC_REQUEST_RSA_IDENTITIES
:
282 * Reply with SSH_AGENT_RSA_IDENTITIES_ANSWER.
290 * Count up the number and length of keys we hold.
293 for (key
= first234(rsakeys
, &e
); key
; key
= next234(&e
)) {
295 len
+= 4; /* length field */
296 len
+= ssh1_bignum_length(key
->exponent
);
297 len
+= ssh1_bignum_length(key
->modulus
);
298 len
+= 4 + strlen(key
->comment
);
302 * Packet header is the obvious five bytes, plus four
303 * bytes for the key count.
306 if (len
> AGENT_MAX_MSGLEN
)
307 goto failure
; /* aaargh! too much stuff! */
308 PUT_32BIT(ret
, len
-4);
309 ret
[4] = SSH_AGENT_RSA_IDENTITIES_ANSWER
;
310 PUT_32BIT(ret
+5, nkeys
);
312 for (key
= first234(rsakeys
, &e
); key
; key
= next234(&e
)) {
313 PUT_32BIT(p
, ssh1_bignum_bitcount(key
->modulus
));
315 p
+= ssh1_write_bignum(p
, key
->exponent
);
316 p
+= ssh1_write_bignum(p
, key
->modulus
);
317 PUT_32BIT(p
, strlen(key
->comment
));
318 memcpy(p
+4, key
->comment
, strlen(key
->comment
));
319 p
+= 4 + strlen(key
->comment
);
323 case SSH_AGENTC_RSA_CHALLENGE
:
325 * Reply with either SSH_AGENT_RSA_RESPONSE or
326 * SSH_AGENT_FAILURE, depending on whether we have that key
330 struct RSAKey reqkey
, *key
;
331 Bignum challenge
, response
;
332 unsigned char response_source
[48], response_md5
[16];
333 struct MD5Context md5c
;
337 p
+= ssh1_read_bignum(p
, &reqkey
.exponent
);
338 p
+= ssh1_read_bignum(p
, &reqkey
.modulus
);
339 p
+= ssh1_read_bignum(p
, &challenge
);
340 memcpy(response_source
+32, p
, 16); p
+= 16;
341 if (GET_32BIT(p
) != 1 ||
342 (key
= find234(rsakeys
, &reqkey
, NULL
)) == NULL
) {
343 freebn(reqkey
.exponent
);
344 freebn(reqkey
.modulus
);
348 response
= rsadecrypt(challenge
, key
);
349 for (i
= 0; i
< 32; i
++)
350 response_source
[i
] = bignum_byte(response
, 31-i
);
353 MD5Update(&md5c
, response_source
, 48);
354 MD5Final(response_md5
, &md5c
);
355 memset(response_source
, 0, 48); /* burn the evidence */
356 freebn(response
); /* and that evidence */
357 freebn(challenge
); /* yes, and that evidence */
358 freebn(reqkey
.exponent
); /* and free some memory ... */
359 freebn(reqkey
.modulus
); /* ... while we're at it. */
362 * Packet is the obvious five byte header, plus sixteen
366 PUT_32BIT(ret
, len
-4);
367 ret
[4] = SSH_AGENT_RSA_RESPONSE
;
368 memcpy(ret
+5, response_md5
, 16);
371 case SSH_AGENTC_ADD_RSA_IDENTITY
:
373 * Add to the list and return SSH_AGENT_SUCCESS, or
374 * SSH_AGENT_FAILURE if the key was malformed.
379 key
= malloc(sizeof(struct RSAKey
));
380 memset(key
, 0, sizeof(key
));
381 p
+= makekey(p
, key
, NULL
, 1);
382 p
+= makeprivate(p
, key
);
383 p
+= ssh1_read_bignum(p
, NULL
); /* p^-1 mod q */
384 p
+= ssh1_read_bignum(p
, NULL
); /* p */
385 p
+= ssh1_read_bignum(p
, NULL
); /* q */
386 comment
= malloc(GET_32BIT(p
));
388 memcpy(comment
, p
+4, GET_32BIT(p
));
389 key
->comment
= comment
;
392 ret
[4] = SSH_AGENT_FAILURE
;
393 if (add234(rsakeys
, key
) == key
) {
395 ret
[4] = SSH_AGENT_SUCCESS
;
402 case SSH_AGENTC_REMOVE_RSA_IDENTITY
:
404 * Remove from the list and return SSH_AGENT_SUCCESS, or
405 * perhaps SSH_AGENT_FAILURE if it wasn't in the list to
409 struct RSAKey reqkey
, *key
;
411 p
+= makekey(p
, &reqkey
, NULL
, 0);
412 key
= find234(rsakeys
, &reqkey
, NULL
);
413 freebn(reqkey
.exponent
);
414 freebn(reqkey
.modulus
);
416 ret
[4] = SSH_AGENT_FAILURE
;
418 del234(rsakeys
, key
);
421 ret
[4] = SSH_AGENT_SUCCESS
;
428 * Unrecognised message. Return SSH_AGENT_FAILURE.
431 ret
[4] = SSH_AGENT_FAILURE
;
437 * Key comparison function for the 2-3-4 tree of RSA keys.
439 static int cmpkeys(void *av
, void *bv
) {
440 struct RSAKey
*a
= (struct RSAKey
*)av
;
441 struct RSAKey
*b
= (struct RSAKey
*)bv
;
448 * Compare by length of moduli.
450 alen
= ssh1_bignum_bitcount(am
);
451 blen
= ssh1_bignum_bitcount(bm
);
452 if (alen
> blen
) return +1; else if (alen
< blen
) return -1;
454 * Now compare by moduli themselves.
456 alen
= (alen
+ 7) / 8; /* byte count */
459 abyte
= bignum_byte(am
, alen
);
460 bbyte
= bignum_byte(bm
, alen
);
461 if (abyte
> bbyte
) return +1; else if (abyte
< bbyte
) return -1;
469 static void error(char *s
) {
470 MessageBox(hwnd
, s
, APPNAME
, MB_OK
| MB_ICONERROR
);
474 * Dialog-box function for the key list box.
476 static int CALLBACK
KeyListProc(HWND hwnd
, UINT msg
,
477 WPARAM wParam
, LPARAM lParam
) {
481 char filename
[FILENAME_MAX
];
487 static int tabs
[2] = {25, 175};
488 SendDlgItemMessage (hwnd
, 100, LB_SETTABSTOPS
, 2,
494 switch (LOWORD(wParam
)) {
500 case 101: /* add key */
501 if (HIWORD(wParam
) == BN_CLICKED
||
502 HIWORD(wParam
) == BN_DOUBLECLICKED
) {
503 memset(&of
, 0, sizeof(of
));
504 #ifdef OPENFILENAME_SIZE_VERSION_400
505 of
.lStructSize
= OPENFILENAME_SIZE_VERSION_400
;
507 of
.lStructSize
= sizeof(of
);
510 of
.lpstrFilter
= "All Files\0*\0\0\0";
511 of
.lpstrCustomFilter
= NULL
;
513 of
.lpstrFile
= filename
; *filename
= '\0';
514 of
.nMaxFile
= sizeof(filename
);
515 of
.lpstrFileTitle
= NULL
;
516 of
.lpstrInitialDir
= NULL
;
517 of
.lpstrTitle
= "Select Private Key File";
519 if (GetOpenFileName(&of
)) {
520 add_keyfile(filename
);
525 case 102: /* remove key */
526 if (HIWORD(wParam
) == BN_CLICKED
||
527 HIWORD(wParam
) == BN_DOUBLECLICKED
) {
528 int n
= SendDlgItemMessage (hwnd
, 100, LB_GETCURSEL
, 0, 0);
533 for (key
= first234(rsakeys
, &e
); key
; key
= next234(&e
))
536 del234(rsakeys
, key
);
537 freersakey(key
); free(key
);
551 static LRESULT CALLBACK
WndProc (HWND hwnd
, UINT message
,
552 WPARAM wParam
, LPARAM lParam
) {
554 static int menuinprogress
;
558 if (lParam
== WM_RBUTTONUP
) {
560 GetCursorPos(&cursorpos
);
561 PostMessage(hwnd
, WM_SYSTRAY2
, cursorpos
.x
, cursorpos
.y
);
562 } else if (lParam
== WM_LBUTTONDBLCLK
) {
563 /* Equivalent to IDM_VIEWKEYS. */
564 PostMessage(hwnd
, WM_COMMAND
, IDM_VIEWKEYS
, 0);
568 if (!menuinprogress
) {
570 SetForegroundWindow(hwnd
);
571 ret
= TrackPopupMenu(systray_menu
,
572 TPM_RIGHTALIGN
| TPM_BOTTOMALIGN
|
574 wParam
, lParam
, 0, hwnd
, NULL
);
580 switch (wParam
& ~0xF) { /* low 4 bits reserved to Windows */
582 SendMessage(hwnd
, WM_CLOSE
, 0, 0);
586 keylist
= CreateDialog (instance
, MAKEINTRESOURCE(211),
588 ShowWindow (keylist
, SW_SHOWNORMAL
);
590 * Sometimes the window comes up minimised / hidden
591 * for no obvious reason. Prevent this.
593 SetForegroundWindow(keylist
);
594 SetWindowPos (keylist
, HWND_TOP
, 0, 0, 0, 0,
595 SWP_NOMOVE
| SWP_NOSIZE
| SWP_SHOWWINDOW
);
600 aboutbox
= CreateDialog (instance
, MAKEINTRESOURCE(213),
602 ShowWindow (aboutbox
, SW_SHOWNORMAL
);
604 * Sometimes the window comes up minimised / hidden
605 * for no obvious reason. Prevent this.
607 SetForegroundWindow(aboutbox
);
608 SetWindowPos (aboutbox
, HWND_TOP
, 0, 0, 0, 0,
609 SWP_NOMOVE
| SWP_NOSIZE
| SWP_SHOWWINDOW
);
622 HANDLE filemap
, proc
;
623 PSID mapowner
, procowner
;
624 PSECURITY_DESCRIPTOR psd1
= NULL
, psd2
= NULL
;
627 cds
= (COPYDATASTRUCT
*)lParam
;
628 if (cds
->dwData
!= AGENT_COPYDATA_ID
)
629 return 0; /* not our message, mate */
630 mapname
= (char *)cds
->lpData
;
631 if (mapname
[cds
->cbData
- 1] != '\0')
632 return 0; /* failure to be ASCIZ! */
634 debug(("mapname is :%s:\r\n", mapname
));
636 filemap
= OpenFileMapping(FILE_MAP_ALL_ACCESS
, FALSE
, mapname
);
638 debug(("filemap is %p\r\n", filemap
));
640 if (filemap
!= NULL
&& filemap
!= INVALID_HANDLE_VALUE
) {
644 if ((proc
= OpenProcess(MAXIMUM_ALLOWED
, FALSE
,
645 GetCurrentProcessId())) == NULL
) {
647 debug(("couldn't get handle for process\r\n"));
651 if (getsecurityinfo(proc
, SE_KERNEL_OBJECT
,
652 OWNER_SECURITY_INFORMATION
,
653 &procowner
, NULL
, NULL
, NULL
,
654 &psd2
) != ERROR_SUCCESS
) {
656 debug(("couldn't get owner info for process\r\n"));
659 return 0; /* unable to get security info */
662 if ((rc
= getsecurityinfo(filemap
, SE_KERNEL_OBJECT
,
663 OWNER_SECURITY_INFORMATION
,
664 &mapowner
, NULL
, NULL
, NULL
,
665 &psd1
) != ERROR_SUCCESS
)) {
667 debug(("couldn't get owner info for filemap: %d\r\n", rc
));
672 debug(("got security stuff\r\n"));
674 if (!EqualSid(mapowner
, procowner
))
675 return 0; /* security ID mismatch! */
677 debug(("security stuff matched\r\n"));
683 debug(("security APIs not present\r\n"));
687 p
= MapViewOfFile(filemap
, FILE_MAP_WRITE
, 0, 0, 0);
689 debug(("p is %p\r\n", p
));
690 {int i
; for(i
=0;i
<5;i
++)debug(("p[%d]=%02x\r\n", i
, ((unsigned char *)p
)[i
]));}
696 CloseHandle(filemap
);
701 return DefWindowProc (hwnd
, message
, wParam
, lParam
);
704 int WINAPI
WinMain(HINSTANCE inst
, HINSTANCE prev
, LPSTR cmdline
, int show
) {
711 * Determine whether we're an NT system (should have security
712 * APIs) or a non-NT system (don't do security).
714 memset(&osi
, 0, sizeof(OSVERSIONINFO
));
715 osi
.dwOSVersionInfoSize
= sizeof(OSVERSIONINFO
);
716 if (GetVersionEx(&osi
) && osi
.dwPlatformId
==VER_PLATFORM_WIN32_NT
) {
719 has_security
= FALSE
;
724 * Attempt to ge the security API we need.
726 advapi
= LoadLibrary("ADVAPI32.DLL");
727 getsecurityinfo
= (gsi_fn_t
)GetProcAddress(advapi
, "GetSecurityInfo");
728 if (!getsecurityinfo
) {
730 "Unable to access security APIs. Pageant will\n"
731 "not run, in case it causes a security breach.",
732 "Pageant Fatal Error", MB_ICONERROR
| MB_OK
);
737 "This program has been compiled for Win9X and will\n"
738 "not run on NT, in case it causes a security breach.",
739 "Pageant Fatal Error", MB_ICONERROR
| MB_OK
);
746 * First bomb out totally if we are already running.
748 if (FindWindow("Pageant", "Pageant")) {
749 MessageBox(NULL
, "Pageant is already running", "Pageant Error",
750 MB_ICONERROR
| MB_OK
);
751 if (advapi
) FreeLibrary(advapi
);
759 wndclass
.lpfnWndProc
= WndProc
;
760 wndclass
.cbClsExtra
= 0;
761 wndclass
.cbWndExtra
= 0;
762 wndclass
.hInstance
= inst
;
763 wndclass
.hIcon
= LoadIcon (inst
,
764 MAKEINTRESOURCE(IDI_MAINICON
));
765 wndclass
.hCursor
= LoadCursor (NULL
, IDC_IBEAM
);
766 wndclass
.hbrBackground
= GetStockObject (BLACK_BRUSH
);
767 wndclass
.lpszMenuName
= NULL
;
768 wndclass
.lpszClassName
= APPNAME
;
770 RegisterClass (&wndclass
);
773 hwnd
= keylist
= NULL
;
775 hwnd
= CreateWindow (APPNAME
, APPNAME
,
776 WS_OVERLAPPEDWINDOW
| WS_VSCROLL
,
777 CW_USEDEFAULT
, CW_USEDEFAULT
,
778 100, 100, NULL
, NULL
, inst
, NULL
);
780 /* Set up a system tray icon */
786 #ifdef NIM_SETVERSION
788 res
= Shell_NotifyIcon(NIM_SETVERSION
, &tnid
);
791 tnid
.cbSize
= sizeof(NOTIFYICONDATA
);
793 tnid
.uID
= 1; /* unique within this systray use */
794 tnid
.uFlags
= NIF_MESSAGE
| NIF_ICON
| NIF_TIP
;
795 tnid
.uCallbackMessage
= WM_SYSTRAY
;
796 tnid
.hIcon
= hicon
= LoadIcon (instance
, MAKEINTRESOURCE(201));
797 strcpy(tnid
.szTip
, "Pageant (PuTTY authentication agent)");
799 res
= Shell_NotifyIcon(NIM_ADD
, &tnid
);
804 systray_menu
= CreatePopupMenu();
805 /* accelerators used: vxa */
806 AppendMenu (systray_menu
, MF_ENABLED
, IDM_VIEWKEYS
, "&View Keys");
807 AppendMenu (systray_menu
, MF_ENABLED
, IDM_ABOUT
, "&About");
808 AppendMenu (systray_menu
, MF_ENABLED
, IDM_CLOSE
, "E&xit");
811 ShowWindow (hwnd
, SW_HIDE
);
814 * Initialise storage for RSA keys.
816 rsakeys
= newtree234(cmpkeys
);
819 * Process the command line and add RSA keys as listed on it.
826 while (*p
&& isspace(*p
)) p
++;
827 if (*p
&& !isspace(*p
)) {
828 char *q
= p
, *pp
= p
;
829 while (*p
&& (inquotes
|| !isspace(*p
)))
832 inquotes
= !inquotes
;
850 while (GetMessage(&msg
, NULL
, 0, 0) == 1) {
851 TranslateMessage(&msg
);
852 DispatchMessage(&msg
);
855 /* Clean up the system tray icon */
859 tnid
.cbSize
= sizeof(NOTIFYICONDATA
);
863 Shell_NotifyIcon(NIM_DELETE
, &tnid
);
865 DestroyMenu(systray_menu
);
868 if (advapi
) FreeLibrary(advapi
);