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
28 #define APPNAME "Pageant"
30 #define SSH_AGENTC_REQUEST_RSA_IDENTITIES 1
31 #define SSH_AGENT_RSA_IDENTITIES_ANSWER 2
32 #define SSH_AGENTC_RSA_CHALLENGE 3
33 #define SSH_AGENT_RSA_RESPONSE 4
34 #define SSH_AGENT_FAILURE 5
35 #define SSH_AGENT_SUCCESS 6
36 #define SSH_AGENTC_ADD_RSA_IDENTITY 7
37 #define SSH_AGENTC_REMOVE_RSA_IDENTITY 8
47 typedef DWORD (WINAPI
*gsi_fn_t
)
48 (HANDLE
, SE_OBJECT_TYPE
, SECURITY_INFORMATION
,
49 PSID
*, PSID
*, PACL
*, PACL
*,
50 PSECURITY_DESCRIPTOR
*);
51 gsi_fn_t getsecurityinfo
;
54 * We need this to link with the RSA code, because rsaencrypt()
55 * pads its data with random bytes. Since we only use rsadecrypt(),
56 * which is deterministic, this should never be called.
58 * If it _is_ called, there is a _serious_ problem, because it
59 * won't generate true random numbers. So we must scream, panic,
60 * and exit immediately if that should happen.
62 int random_byte(void) {
63 MessageBox(hwnd
, "Internal Error", APPNAME
, MB_OK
| MB_ICONERROR
);
68 * This function is needed to link with the DES code. We need not
69 * have it do anything at all.
71 void logevent(char *msg
) {
74 #define GET_32BIT(cp) \
75 (((unsigned long)(unsigned char)(cp)[0] << 24) | \
76 ((unsigned long)(unsigned char)(cp)[1] << 16) | \
77 ((unsigned long)(unsigned char)(cp)[2] << 8) | \
78 ((unsigned long)(unsigned char)(cp)[3]))
80 #define PUT_32BIT(cp, value) { \
81 (cp)[0] = (unsigned char)((value) >> 24); \
82 (cp)[1] = (unsigned char)((value) >> 16); \
83 (cp)[2] = (unsigned char)((value) >> 8); \
84 (cp)[3] = (unsigned char)(value); }
86 #define PASSPHRASE_MAXLEN 512
88 struct PassphraseProcStruct
{
94 * Dialog-box function for the passphrase box.
96 static int CALLBACK
PassphraseProc(HWND hwnd
, UINT msg
,
97 WPARAM wParam
, LPARAM lParam
) {
98 static char *passphrase
;
99 struct PassphraseProcStruct
*p
;
103 p
= (struct PassphraseProcStruct
*)lParam
;
104 passphrase
= p
->passphrase
;
106 SetDlgItemText(hwnd
, 101, p
->comment
);
110 switch (LOWORD(wParam
)) {
120 case 102: /* edit box */
121 if (HIWORD(wParam
) == EN_CHANGE
) {
122 GetDlgItemText (hwnd
, 102, passphrase
, PASSPHRASE_MAXLEN
-1);
123 passphrase
[PASSPHRASE_MAXLEN
-1] = '\0';
136 * Update the visible key list.
138 void keylist_update(void) {
143 SendDlgItemMessage(keylist
, 100, LB_RESETCONTENT
, 0, 0);
144 for (key
= first234(rsakeys
, &e
); key
; key
= next234(&e
)) {
145 char listentry
[512], *p
;
147 * Replace two spaces in the fingerprint with tabs, for
148 * nice alignment in the box.
150 rsa_fingerprint(listentry
, sizeof(listentry
), key
);
151 p
= strchr(listentry
, ' '); if (p
) *p
= '\t';
152 p
= strchr(listentry
, ' '); if (p
) *p
= '\t';
153 SendDlgItemMessage (keylist
, 100, LB_ADDSTRING
,
154 0, (LPARAM
)listentry
);
156 SendDlgItemMessage (keylist
, 100, LB_SETCURSEL
, (WPARAM
) -1, 0);
161 * This function loads a key from a file and adds it.
163 void add_keyfile(char *filename
) {
164 char passphrase
[PASSPHRASE_MAXLEN
];
170 struct PassphraseProcStruct pps
;
172 needs_pass
= rsakey_encrypted(filename
, &comment
);
174 key
= malloc(sizeof(*key
));
175 pps
.passphrase
= passphrase
;
176 pps
.comment
= comment
;
180 dlgret
= DialogBoxParam(instance
, MAKEINTRESOURCE(210),
181 NULL
, PassphraseProc
,
184 if (comment
) free(comment
);
186 return; /* operation cancelled */
190 ret
= loadrsakey(filename
, key
, passphrase
);
193 if (comment
) free(comment
);
195 MessageBox(NULL
, "Couldn't load public key.", APPNAME
,
196 MB_OK
| MB_ICONERROR
);
200 if (add234(rsakeys
, key
) != key
)
201 free(key
); /* already present, don't waste RAM */
205 * This is the main agent function that answers messages.
207 void answer_msg(void *msg
) {
208 unsigned char *p
= msg
;
209 unsigned char *ret
= msg
;
213 * Get the message type.
219 case SSH_AGENTC_REQUEST_RSA_IDENTITIES
:
221 * Reply with SSH_AGENT_RSA_IDENTITIES_ANSWER.
229 * Count up the number and length of keys we hold.
232 for (key
= first234(rsakeys
, &e
); key
; key
= next234(&e
)) {
234 len
+= 4; /* length field */
235 len
+= ssh1_bignum_length(key
->exponent
);
236 len
+= ssh1_bignum_length(key
->modulus
);
237 len
+= 4 + strlen(key
->comment
);
241 * Packet header is the obvious five bytes, plus four
242 * bytes for the key count.
245 if (len
> AGENT_MAX_MSGLEN
)
246 goto failure
; /* aaargh! too much stuff! */
247 PUT_32BIT(ret
, len
-4);
248 ret
[4] = SSH_AGENT_RSA_IDENTITIES_ANSWER
;
249 PUT_32BIT(ret
+5, nkeys
);
251 for (key
= first234(rsakeys
, &e
); key
; key
= next234(&e
)) {
252 PUT_32BIT(p
, ssh1_bignum_bitcount(key
->modulus
));
254 p
+= ssh1_write_bignum(p
, key
->exponent
);
255 p
+= ssh1_write_bignum(p
, key
->modulus
);
256 PUT_32BIT(p
, strlen(key
->comment
));
257 memcpy(p
+4, key
->comment
, strlen(key
->comment
));
258 p
+= 4 + strlen(key
->comment
);
262 case SSH_AGENTC_RSA_CHALLENGE
:
264 * Reply with either SSH_AGENT_RSA_RESPONSE or
265 * SSH_AGENT_FAILURE, depending on whether we have that key
269 struct RSAKey reqkey
, *key
;
270 Bignum challenge
, response
;
271 unsigned char response_source
[48], response_md5
[16];
272 struct MD5Context md5c
;
276 p
+= ssh1_read_bignum(p
, &reqkey
.exponent
);
277 p
+= ssh1_read_bignum(p
, &reqkey
.modulus
);
278 p
+= ssh1_read_bignum(p
, &challenge
);
279 memcpy(response_source
+32, p
, 16); p
+= 16;
280 if (GET_32BIT(p
) != 1 ||
281 (key
= find234(rsakeys
, &reqkey
, NULL
)) == NULL
) {
282 freebn(reqkey
.exponent
);
283 freebn(reqkey
.modulus
);
287 response
= rsadecrypt(challenge
, key
);
288 for (i
= 0; i
< 32; i
++)
289 response_source
[i
] = bignum_byte(response
, 31-i
);
292 MD5Update(&md5c
, response_source
, 48);
293 MD5Final(response_md5
, &md5c
);
294 memset(response_source
, 0, 48); /* burn the evidence */
295 freebn(response
); /* and that evidence */
296 freebn(challenge
); /* yes, and that evidence */
297 freebn(reqkey
.exponent
); /* and free some memory ... */
298 freebn(reqkey
.modulus
); /* ... while we're at it. */
301 * Packet is the obvious five byte header, plus sixteen
305 PUT_32BIT(ret
, len
-4);
306 ret
[4] = SSH_AGENT_RSA_RESPONSE
;
307 memcpy(ret
+5, response_md5
, 16);
310 case SSH_AGENTC_ADD_RSA_IDENTITY
:
312 * Add to the list and return SSH_AGENT_SUCCESS, or
313 * SSH_AGENT_FAILURE if the key was malformed.
318 key
= malloc(sizeof(struct RSAKey
));
319 memset(key
, 0, sizeof(key
));
320 p
+= makekey(p
, key
, NULL
, 1);
321 p
+= makeprivate(p
, key
);
322 p
+= ssh1_read_bignum(p
, NULL
); /* p^-1 mod q */
323 p
+= ssh1_read_bignum(p
, NULL
); /* p */
324 p
+= ssh1_read_bignum(p
, NULL
); /* q */
325 comment
= malloc(GET_32BIT(p
));
327 memcpy(comment
, p
+4, GET_32BIT(p
));
328 key
->comment
= comment
;
331 ret
[4] = SSH_AGENT_FAILURE
;
332 if (add234(rsakeys
, key
) == key
) {
334 ret
[4] = SSH_AGENT_SUCCESS
;
341 case SSH_AGENTC_REMOVE_RSA_IDENTITY
:
343 * Remove from the list and return SSH_AGENT_SUCCESS, or
344 * perhaps SSH_AGENT_FAILURE if it wasn't in the list to
348 struct RSAKey reqkey
, *key
;
350 p
+= makekey(p
, &reqkey
, NULL
, 0);
351 key
= find234(rsakeys
, &reqkey
, NULL
);
352 freebn(reqkey
.exponent
);
353 freebn(reqkey
.modulus
);
355 ret
[4] = SSH_AGENT_FAILURE
;
357 del234(rsakeys
, key
);
360 ret
[4] = SSH_AGENT_SUCCESS
;
367 * Unrecognised message. Return SSH_AGENT_FAILURE.
370 ret
[4] = SSH_AGENT_FAILURE
;
376 * Key comparison function for the 2-3-4 tree of RSA keys.
378 int cmpkeys(void *av
, void *bv
) {
379 struct RSAKey
*a
= (struct RSAKey
*)av
;
380 struct RSAKey
*b
= (struct RSAKey
*)bv
;
387 * Compare by length of moduli.
389 alen
= ssh1_bignum_bitcount(am
);
390 blen
= ssh1_bignum_bitcount(bm
);
391 if (alen
> blen
) return +1; else if (alen
< blen
) return -1;
393 * Now compare by moduli themselves.
395 alen
= (alen
+ 7) / 8; /* byte count */
398 abyte
= bignum_byte(am
, alen
);
399 bbyte
= bignum_byte(bm
, alen
);
400 if (abyte
> bbyte
) return +1; else if (abyte
< bbyte
) return -1;
408 static void error(char *s
) {
409 MessageBox(hwnd
, s
, APPNAME
, MB_OK
| MB_ICONERROR
);
413 * Dialog-box function for the key list box.
415 static int CALLBACK
KeyListProc(HWND hwnd
, UINT msg
,
416 WPARAM wParam
, LPARAM lParam
) {
420 char filename
[FILENAME_MAX
];
426 static int tabs
[2] = {25, 175};
427 SendDlgItemMessage (hwnd
, 100, LB_SETTABSTOPS
, 2,
433 switch (LOWORD(wParam
)) {
439 case 101: /* add key */
440 if (HIWORD(wParam
) == BN_CLICKED
||
441 HIWORD(wParam
) == BN_DOUBLECLICKED
) {
442 memset(&of
, 0, sizeof(of
));
443 #ifdef OPENFILENAME_SIZE_VERSION_400
444 of
.lStructSize
= OPENFILENAME_SIZE_VERSION_400
;
446 of
.lStructSize
= sizeof(of
);
449 of
.lpstrFilter
= "All Files\0*\0\0\0";
450 of
.lpstrCustomFilter
= NULL
;
452 of
.lpstrFile
= filename
; *filename
= '\0';
453 of
.nMaxFile
= sizeof(filename
);
454 of
.lpstrFileTitle
= NULL
;
455 of
.lpstrInitialDir
= NULL
;
456 of
.lpstrTitle
= "Select Public Key File";
458 if (GetOpenFileName(&of
)) {
459 add_keyfile(filename
);
464 case 102: /* remove key */
465 if (HIWORD(wParam
) == BN_CLICKED
||
466 HIWORD(wParam
) == BN_DOUBLECLICKED
) {
467 int n
= SendDlgItemMessage (hwnd
, 100, LB_GETCURSEL
, 0, 0);
468 if (n
== LB_ERR
|| n
== 0) {
472 for (key
= first234(rsakeys
, &e
); key
; key
= next234(&e
))
475 del234(rsakeys
, key
);
476 freersakey(key
); free(key
);
490 static LRESULT CALLBACK
WndProc (HWND hwnd
, UINT message
,
491 WPARAM wParam
, LPARAM lParam
) {
493 static int menuinprogress
;
497 if (lParam
== WM_RBUTTONUP
) {
499 GetCursorPos(&cursorpos
);
500 PostMessage(hwnd
, WM_SYSTRAY2
, cursorpos
.x
, cursorpos
.y
);
501 } else if (lParam
== WM_LBUTTONDBLCLK
) {
502 /* Equivalent to IDM_VIEWKEYS. */
503 PostMessage(hwnd
, WM_COMMAND
, IDM_VIEWKEYS
, 0);
507 if (!menuinprogress
) {
509 SetForegroundWindow(hwnd
);
510 ret
= TrackPopupMenu(systray_menu
,
511 TPM_RIGHTALIGN
| TPM_BOTTOMALIGN
|
513 wParam
, lParam
, 0, hwnd
, NULL
);
519 switch (wParam
& ~0xF) { /* low 4 bits reserved to Windows */
521 SendMessage(hwnd
, WM_CLOSE
, 0, 0);
525 keylist
= CreateDialog (instance
, MAKEINTRESOURCE(211),
527 ShowWindow (keylist
, SW_SHOWNORMAL
);
529 * Sometimes the window comes up minimised / hidden
530 * for no obvious reason. Prevent this.
532 SetForegroundWindow(keylist
);
533 SetWindowPos (keylist
, HWND_TOP
, 0, 0, 0, 0,
534 SWP_NOMOVE
| SWP_NOSIZE
| SWP_SHOWWINDOW
);
547 HANDLE filemap
, proc
;
548 PSID mapowner
, procowner
;
549 PSECURITY_DESCRIPTOR psd1
= NULL
, psd2
= NULL
;
552 cds
= (COPYDATASTRUCT
*)lParam
;
553 if (cds
->dwData
!= AGENT_COPYDATA_ID
)
554 return 0; /* not our message, mate */
555 mapname
= (char *)cds
->lpData
;
556 if (mapname
[cds
->cbData
- 1] != '\0')
557 return 0; /* failure to be ASCIZ! */
559 debug(("mapname is :%s:\r\n", mapname
));
561 filemap
= OpenFileMapping(FILE_MAP_ALL_ACCESS
, FALSE
, mapname
);
563 debug(("filemap is %p\r\n", filemap
));
565 if (filemap
!= NULL
&& filemap
!= INVALID_HANDLE_VALUE
) {
568 if ((proc
= OpenProcess(MAXIMUM_ALLOWED
, FALSE
,
569 GetCurrentProcessId())) == NULL
) {
571 debug(("couldn't get handle for process\r\n"));
575 if (getsecurityinfo(proc
, SE_KERNEL_OBJECT
,
576 OWNER_SECURITY_INFORMATION
,
577 &procowner
, NULL
, NULL
, NULL
,
578 &psd2
) != ERROR_SUCCESS
) {
580 debug(("couldn't get owner info for process\r\n"));
583 return 0; /* unable to get security info */
586 if ((rc
= getsecurityinfo(filemap
, SE_KERNEL_OBJECT
,
587 OWNER_SECURITY_INFORMATION
,
588 &mapowner
, NULL
, NULL
, NULL
,
589 &psd1
) != ERROR_SUCCESS
)) {
591 debug(("couldn't get owner info for filemap: %d\r\n", rc
));
596 debug(("got security stuff\r\n"));
598 if (!EqualSid(mapowner
, procowner
))
599 return 0; /* security ID mismatch! */
601 debug(("security stuff matched\r\n"));
607 debug(("security APIs not present\r\n"));
610 p
= MapViewOfFile(filemap
, FILE_MAP_WRITE
, 0, 0, 0);
612 debug(("p is %p\r\n", p
));
613 {int i
; for(i
=0;i
<5;i
++)debug(("p[%d]=%02x\r\n", i
, ((unsigned char *)p
)[i
]));}
619 CloseHandle(filemap
);
624 return DefWindowProc (hwnd
, message
, wParam
, lParam
);
627 int WINAPI
WinMain(HINSTANCE inst
, HINSTANCE prev
, LPSTR cmdline
, int show
) {
634 * Determine whether we're an NT system (should have security
635 * APIs) or a non-NT system (don't do security).
637 memset(&osi
, 0, sizeof(OSVERSIONINFO
));
638 osi
.dwOSVersionInfoSize
= sizeof(OSVERSIONINFO
);
639 if (GetVersionEx(&osi
) && osi
.dwPlatformId
==VER_PLATFORM_WIN32_NT
) {
642 has_security
= FALSE
;
646 * Attempt to ge the security API we need.
648 advapi
= LoadLibrary("ADVAPI32.DLL");
649 getsecurityinfo
= (gsi_fn_t
)GetProcAddress(advapi
, "GetSecurityInfo");
650 if (!getsecurityinfo
) {
652 "Unable to access security APIs. Pageant will\n"
653 "not run, in case it causes a security breach.",
654 "Pageant Fatal Error", MB_ICONERROR
| MB_OK
);
661 * First bomb out totally if we are already running.
663 if (FindWindow("Pageant", "Pageant")) {
664 MessageBox(NULL
, "Pageant is already running", "Pageant Error",
665 MB_ICONERROR
| MB_OK
);
666 if (advapi
) FreeLibrary(advapi
);
674 wndclass
.lpfnWndProc
= WndProc
;
675 wndclass
.cbClsExtra
= 0;
676 wndclass
.cbWndExtra
= 0;
677 wndclass
.hInstance
= inst
;
678 wndclass
.hIcon
= LoadIcon (inst
,
679 MAKEINTRESOURCE(IDI_MAINICON
));
680 wndclass
.hCursor
= LoadCursor (NULL
, IDC_IBEAM
);
681 wndclass
.hbrBackground
= GetStockObject (BLACK_BRUSH
);
682 wndclass
.lpszMenuName
= NULL
;
683 wndclass
.lpszClassName
= APPNAME
;
685 RegisterClass (&wndclass
);
688 hwnd
= keylist
= NULL
;
690 hwnd
= CreateWindow (APPNAME
, APPNAME
,
691 WS_OVERLAPPEDWINDOW
| WS_VSCROLL
,
692 CW_USEDEFAULT
, CW_USEDEFAULT
,
693 100, 100, NULL
, NULL
, inst
, NULL
);
695 /* Set up a system tray icon */
701 #ifdef NIM_SETVERSION
703 res
= Shell_NotifyIcon(NIM_SETVERSION
, &tnid
);
706 tnid
.cbSize
= sizeof(NOTIFYICONDATA
);
708 tnid
.uID
= 1; /* unique within this systray use */
709 tnid
.uFlags
= NIF_MESSAGE
| NIF_ICON
| NIF_TIP
;
710 tnid
.uCallbackMessage
= WM_SYSTRAY
;
711 tnid
.hIcon
= hicon
= LoadIcon (instance
, MAKEINTRESOURCE(201));
712 strcpy(tnid
.szTip
, "Pageant (PuTTY authentication agent)");
714 res
= Shell_NotifyIcon(NIM_ADD
, &tnid
);
719 systray_menu
= CreatePopupMenu();
720 AppendMenu (systray_menu
, MF_ENABLED
, IDM_VIEWKEYS
, "&View Keys");
721 AppendMenu (systray_menu
, MF_ENABLED
, IDM_CLOSE
, "E&xit");
724 ShowWindow (hwnd
, SW_HIDE
);
727 * Initialise storage for RSA keys.
729 rsakeys
= newtree234(cmpkeys
);
732 * Process the command line and add RSA keys as listed on it.
739 while (*p
&& isspace(*p
)) p
++;
740 if (*p
&& !isspace(*p
)) {
741 char *q
= p
, *pp
= p
;
742 while (*p
&& (inquotes
|| !isspace(*p
)))
745 inquotes
= !inquotes
;
763 while (GetMessage(&msg
, NULL
, 0, 0) == 1) {
764 TranslateMessage(&msg
);
765 DispatchMessage(&msg
);
768 /* Clean up the system tray icon */
772 tnid
.cbSize
= sizeof(NOTIFYICONDATA
);
776 Shell_NotifyIcon(NIM_DELETE
, &tnid
);
778 DestroyMenu(systray_menu
);
781 if (advapi
) FreeLibrary(advapi
);