2 * Pageant: the PuTTY Authentication Agent.
7 #include <stdio.h> /* FIXME */
8 #include "putty.h" /* FIXME */
12 #define IDI_MAINICON 200
13 #define IDI_TRAYICON 201
15 #define WM_XUSER (WM_USER + 0x2000)
16 #define WM_SYSTRAY (WM_XUSER + 6)
17 #define WM_SYSTRAY2 (WM_XUSER + 7)
19 #define AGENT_COPYDATA_ID 0x804e50ba /* random goop */
22 * FIXME: maybe some day we can sort this out ...
24 #define AGENT_MAX_MSGLEN 8192
26 #define IDM_CLOSE 0x0010
27 #define IDM_VIEWKEYS 0x0020
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
48 typedef DWORD (WINAPI
*gsi_fn_t
)
49 (HANDLE
, SE_OBJECT_TYPE
, SECURITY_INFORMATION
,
50 PSID
*, PSID
*, PACL
*, PACL
*,
51 PSECURITY_DESCRIPTOR
*);
52 gsi_fn_t getsecurityinfo
;
55 * We need this to link with the RSA code, because rsaencrypt()
56 * pads its data with random bytes. Since we only use rsadecrypt(),
57 * which is deterministic, this should never be called.
59 * If it _is_ called, there is a _serious_ problem, because it
60 * won't generate true random numbers. So we must scream, panic,
61 * and exit immediately if that should happen.
63 int random_byte(void) {
64 MessageBox(hwnd
, "Internal Error", APPNAME
, MB_OK
| MB_ICONERROR
);
69 * This function is needed to link with the DES code. We need not
70 * have it do anything at all.
72 void logevent(char *msg
) {
75 #define GET_32BIT(cp) \
76 (((unsigned long)(unsigned char)(cp)[0] << 24) | \
77 ((unsigned long)(unsigned char)(cp)[1] << 16) | \
78 ((unsigned long)(unsigned char)(cp)[2] << 8) | \
79 ((unsigned long)(unsigned char)(cp)[3]))
81 #define PUT_32BIT(cp, value) { \
82 (cp)[0] = (unsigned char)((value) >> 24); \
83 (cp)[1] = (unsigned char)((value) >> 16); \
84 (cp)[2] = (unsigned char)((value) >> 8); \
85 (cp)[3] = (unsigned char)(value); }
87 #define PASSPHRASE_MAXLEN 512
89 struct PassphraseProcStruct
{
95 * Dialog-box function for the passphrase box.
97 static int CALLBACK
PassphraseProc(HWND hwnd
, UINT msg
,
98 WPARAM wParam
, LPARAM lParam
) {
99 static char *passphrase
;
100 struct PassphraseProcStruct
*p
;
104 p
= (struct PassphraseProcStruct
*)lParam
;
105 passphrase
= p
->passphrase
;
107 SetDlgItemText(hwnd
, 101, p
->comment
);
111 switch (LOWORD(wParam
)) {
121 case 102: /* edit box */
122 if (HIWORD(wParam
) == EN_CHANGE
) {
123 GetDlgItemText (hwnd
, 102, passphrase
, PASSPHRASE_MAXLEN
-1);
124 passphrase
[PASSPHRASE_MAXLEN
-1] = '\0';
137 * Update the visible key list.
139 void keylist_update(void) {
144 SendDlgItemMessage(keylist
, 100, LB_RESETCONTENT
, 0, 0);
145 for (key
= first234(rsakeys
, &e
); key
; key
= next234(&e
)) {
146 SendDlgItemMessage (keylist
, 100, LB_ADDSTRING
,
147 0, (LPARAM
) key
->comment
);
149 SendDlgItemMessage (keylist
, 100, LB_SETCURSEL
, (WPARAM
) -1, 0);
154 * This function loads a key from a file and adds it.
156 void add_keyfile(char *filename
) {
157 char passphrase
[PASSPHRASE_MAXLEN
];
163 struct PassphraseProcStruct pps
;
165 needs_pass
= rsakey_encrypted(filename
, &comment
);
167 key
= malloc(sizeof(*key
));
168 pps
.passphrase
= passphrase
;
169 pps
.comment
= comment
;
173 dlgret
= DialogBoxParam(instance
, MAKEINTRESOURCE(210),
174 NULL
, PassphraseProc
,
177 if (comment
) free(comment
);
179 return; /* operation cancelled */
183 ret
= loadrsakey(filename
, key
, passphrase
);
186 if (comment
) free(comment
);
188 MessageBox(NULL
, "Couldn't load public key.", APPNAME
,
189 MB_OK
| MB_ICONERROR
);
193 if (add234(rsakeys
, key
) != key
)
194 free(key
); /* already present, don't waste RAM */
198 * This is the main agent function that answers messages.
200 void answer_msg(void *msg
) {
201 unsigned char *p
= msg
;
202 unsigned char *ret
= msg
;
206 * Get the message type.
212 case SSH_AGENTC_REQUEST_RSA_IDENTITIES
:
214 * Reply with SSH_AGENT_RSA_IDENTITIES_ANSWER.
222 * Count up the number and length of keys we hold.
225 for (key
= first234(rsakeys
, &e
); key
; key
= next234(&e
)) {
227 len
+= 4; /* length field */
228 len
+= ssh1_bignum_length(key
->exponent
);
229 len
+= ssh1_bignum_length(key
->modulus
);
230 len
+= 4 + strlen(key
->comment
);
234 * Packet header is the obvious five bytes, plus four
235 * bytes for the key count.
238 if (len
> AGENT_MAX_MSGLEN
)
239 goto failure
; /* aaargh! too much stuff! */
240 PUT_32BIT(ret
, len
-4);
241 ret
[4] = SSH_AGENT_RSA_IDENTITIES_ANSWER
;
242 PUT_32BIT(ret
+5, nkeys
);
244 for (key
= first234(rsakeys
, &e
); key
; key
= next234(&e
)) {
245 PUT_32BIT(p
, ssh1_bignum_bitcount(key
->modulus
));
247 p
+= ssh1_write_bignum(p
, key
->exponent
);
248 p
+= ssh1_write_bignum(p
, key
->modulus
);
249 PUT_32BIT(p
, strlen(key
->comment
));
250 memcpy(p
+4, key
->comment
, strlen(key
->comment
));
251 p
+= 4 + strlen(key
->comment
);
255 case SSH_AGENTC_RSA_CHALLENGE
:
257 * Reply with either SSH_AGENT_RSA_RESPONSE or
258 * SSH_AGENT_FAILURE, depending on whether we have that key
262 struct RSAKey reqkey
, *key
;
263 Bignum challenge
, response
;
264 unsigned char response_source
[48], response_md5
[16];
265 struct MD5Context md5c
;
269 p
+= ssh1_read_bignum(p
, &reqkey
.exponent
);
270 p
+= ssh1_read_bignum(p
, &reqkey
.modulus
);
271 p
+= ssh1_read_bignum(p
, &challenge
);
272 memcpy(response_source
+32, p
, 16); p
+= 16;
273 if (GET_32BIT(p
) != 1 ||
274 (key
= find234(rsakeys
, &reqkey
, NULL
)) == NULL
) {
275 freebn(reqkey
.exponent
);
276 freebn(reqkey
.modulus
);
280 response
= rsadecrypt(challenge
, key
);
281 for (i
= 0; i
< 32; i
++)
282 response_source
[i
] = bignum_byte(response
, 31-i
);
285 MD5Update(&md5c
, response_source
, 48);
286 MD5Final(response_md5
, &md5c
);
287 memset(response_source
, 0, 48); /* burn the evidence */
288 freebn(response
); /* and that evidence */
289 freebn(challenge
); /* yes, and that evidence */
290 freebn(reqkey
.exponent
); /* and free some memory ... */
291 freebn(reqkey
.modulus
); /* ... while we're at it. */
294 * Packet is the obvious five byte header, plus sixteen
298 PUT_32BIT(ret
, len
-4);
299 ret
[4] = SSH_AGENT_RSA_RESPONSE
;
300 memcpy(ret
+5, response_md5
, 16);
303 case SSH_AGENTC_ADD_RSA_IDENTITY
:
305 * Add to the list and return SSH_AGENT_SUCCESS, or
306 * SSH_AGENT_FAILURE if the key was malformed.
311 key
= malloc(sizeof(struct RSAKey
));
312 memset(key
, 0, sizeof(key
));
313 p
+= makekey(p
, key
, NULL
, 1);
314 p
+= makeprivate(p
, key
);
315 p
+= ssh1_read_bignum(p
, NULL
); /* p^-1 mod q */
316 p
+= ssh1_read_bignum(p
, NULL
); /* p */
317 p
+= ssh1_read_bignum(p
, NULL
); /* q */
318 comment
= malloc(GET_32BIT(p
));
320 memcpy(comment
, p
+4, GET_32BIT(p
));
321 key
->comment
= comment
;
324 ret
[4] = SSH_AGENT_FAILURE
;
325 if (add234(rsakeys
, key
) == key
) {
327 ret
[4] = SSH_AGENT_SUCCESS
;
333 case SSH_AGENTC_REMOVE_RSA_IDENTITY
:
335 * Remove from the list and return SSH_AGENT_SUCCESS, or
336 * perhaps SSH_AGENT_FAILURE if it wasn't in the list to
340 struct RSAKey reqkey
, *key
;
342 p
+= makekey(p
, &reqkey
, NULL
, 0);
343 key
= find234(rsakeys
, &reqkey
, NULL
);
344 freebn(reqkey
.exponent
);
345 freebn(reqkey
.modulus
);
347 ret
[4] = SSH_AGENT_FAILURE
;
349 del234(rsakeys
, key
);
352 ret
[4] = SSH_AGENT_SUCCESS
;
359 * Unrecognised message. Return SSH_AGENT_FAILURE.
362 ret
[4] = SSH_AGENT_FAILURE
;
368 * Key comparison function for the 2-3-4 tree of RSA keys.
370 int cmpkeys(void *av
, void *bv
) {
371 struct RSAKey
*a
= (struct RSAKey
*)av
;
372 struct RSAKey
*b
= (struct RSAKey
*)bv
;
379 * Compare by length of moduli.
381 alen
= ssh1_bignum_bitcount(am
);
382 blen
= ssh1_bignum_bitcount(bm
);
383 if (alen
> blen
) return +1; else if (alen
< blen
) return -1;
385 * Now compare by moduli themselves.
387 alen
= (alen
+ 7) / 8; /* byte count */
390 abyte
= bignum_byte(am
, alen
);
391 bbyte
= bignum_byte(bm
, alen
);
392 if (abyte
> bbyte
) return +1; else if (abyte
< bbyte
) return -1;
400 static void error(char *s
) {
401 MessageBox(hwnd
, s
, APPNAME
, MB_OK
| MB_ICONERROR
);
405 * Dialog-box function for the key list box.
407 static int CALLBACK
KeyListProc(HWND hwnd
, UINT msg
,
408 WPARAM wParam
, LPARAM lParam
) {
412 char filename
[FILENAME_MAX
];
416 for (key
= first234(rsakeys
, &e
); key
; key
= next234(&e
)) {
417 SendDlgItemMessage (hwnd
, 100, LB_ADDSTRING
,
418 0, (LPARAM
) key
->comment
);
422 switch (LOWORD(wParam
)) {
428 case 101: /* add key */
429 if (HIWORD(wParam
) == BN_CLICKED
||
430 HIWORD(wParam
) == BN_DOUBLECLICKED
) {
431 memset(&of
, 0, sizeof(of
));
432 #ifdef OPENFILENAME_SIZE_VERSION_400
433 of
.lStructSize
= OPENFILENAME_SIZE_VERSION_400
;
435 of
.lStructSize
= sizeof(of
);
438 of
.lpstrFilter
= "All Files\0*\0\0\0";
439 of
.lpstrCustomFilter
= NULL
;
441 of
.lpstrFile
= filename
; *filename
= '\0';
442 of
.nMaxFile
= sizeof(filename
);
443 of
.lpstrFileTitle
= NULL
;
444 of
.lpstrInitialDir
= NULL
;
445 of
.lpstrTitle
= "Select Public Key File";
447 if (GetOpenFileName(&of
)) {
448 add_keyfile(filename
);
453 case 102: /* remove key */
454 if (HIWORD(wParam
) == BN_CLICKED
||
455 HIWORD(wParam
) == BN_DOUBLECLICKED
) {
456 int n
= SendDlgItemMessage (hwnd
, 100, LB_GETCURSEL
, 0, 0);
457 if (n
== LB_ERR
|| n
== 0) {
461 for (key
= first234(rsakeys
, &e
); key
; key
= next234(&e
))
464 del234(rsakeys
, key
);
465 freersakey(key
); free(key
);
466 SendDlgItemMessage(hwnd
, 100, LB_RESETCONTENT
, 0, 0);
467 for (key
= first234(rsakeys
, &e
); key
; key
= next234(&e
)) {
468 SendDlgItemMessage (hwnd
, 100, LB_ADDSTRING
,
469 0, (LPARAM
) key
->comment
);
471 SendDlgItemMessage (hwnd
, 100, LB_SETCURSEL
, (WPARAM
) -1, 0);
484 static LRESULT CALLBACK
WndProc (HWND hwnd
, UINT message
,
485 WPARAM wParam
, LPARAM lParam
) {
487 static int menuinprogress
;
491 if (lParam
== WM_RBUTTONUP
) {
493 GetCursorPos(&cursorpos
);
494 PostMessage(hwnd
, WM_SYSTRAY2
, cursorpos
.x
, cursorpos
.y
);
495 } else if (lParam
== WM_LBUTTONDBLCLK
) {
496 /* Equivalent to IDM_VIEWKEYS. */
497 PostMessage(hwnd
, WM_COMMAND
, IDM_VIEWKEYS
, 0);
501 if (!menuinprogress
) {
503 SetForegroundWindow(hwnd
);
504 ret
= TrackPopupMenu(systray_menu
,
505 TPM_RIGHTALIGN
| TPM_BOTTOMALIGN
|
507 wParam
, lParam
, 0, hwnd
, NULL
);
513 switch (wParam
& ~0xF) { /* low 4 bits reserved to Windows */
515 SendMessage(hwnd
, WM_CLOSE
, 0, 0);
519 keylist
= CreateDialog (instance
, MAKEINTRESOURCE(211),
521 ShowWindow (keylist
, SW_SHOWNORMAL
);
523 * Sometimes the window comes up minimised / hidden
524 * for no obvious reason. Prevent this.
526 SetForegroundWindow(keylist
);
527 SetWindowPos (keylist
, HWND_TOP
, 0, 0, 0, 0,
528 SWP_NOMOVE
| SWP_NOSIZE
| SWP_SHOWWINDOW
);
541 HANDLE filemap
, proc
;
542 PSID mapowner
, procowner
;
543 PSECURITY_DESCRIPTOR psd1
= NULL
, psd2
= NULL
;
546 cds
= (COPYDATASTRUCT
*)lParam
;
547 if (cds
->dwData
!= AGENT_COPYDATA_ID
)
548 return 0; /* not our message, mate */
549 mapname
= (char *)cds
->lpData
;
550 if (mapname
[cds
->cbData
- 1] != '\0')
551 return 0; /* failure to be ASCIZ! */
553 debug(("mapname is :%s:\r\n", mapname
));
555 filemap
= OpenFileMapping(FILE_MAP_ALL_ACCESS
, FALSE
, mapname
);
557 debug(("filemap is %p\r\n", filemap
));
559 if (filemap
!= NULL
&& filemap
!= INVALID_HANDLE_VALUE
) {
562 if ((proc
= OpenProcess(MAXIMUM_ALLOWED
, FALSE
,
563 GetCurrentProcessId())) == NULL
) {
565 debug(("couldn't get handle for process\r\n"));
569 if (getsecurityinfo(proc
, SE_KERNEL_OBJECT
,
570 OWNER_SECURITY_INFORMATION
,
571 &procowner
, NULL
, NULL
, NULL
,
572 &psd2
) != ERROR_SUCCESS
) {
574 debug(("couldn't get owner info for process\r\n"));
577 return 0; /* unable to get security info */
580 if ((rc
= getsecurityinfo(filemap
, SE_KERNEL_OBJECT
,
581 OWNER_SECURITY_INFORMATION
,
582 &mapowner
, NULL
, NULL
, NULL
,
583 &psd1
) != ERROR_SUCCESS
)) {
585 debug(("couldn't get owner info for filemap: %d\r\n", rc
));
590 debug(("got security stuff\r\n"));
592 if (!EqualSid(mapowner
, procowner
))
593 return 0; /* security ID mismatch! */
595 debug(("security stuff matched\r\n"));
601 debug(("security APIs not present\r\n"));
604 p
= MapViewOfFile(filemap
, FILE_MAP_WRITE
, 0, 0, 0);
606 debug(("p is %p\r\n", p
));
607 {int i
; for(i
=0;i
<5;i
++)debug(("p[%d]=%02x\r\n", i
, ((unsigned char *)p
)[i
]));}
613 CloseHandle(filemap
);
618 return DefWindowProc (hwnd
, message
, wParam
, lParam
);
621 int WINAPI
WinMain(HINSTANCE inst
, HINSTANCE prev
, LPSTR cmdline
, int show
) {
628 * Determine whether we're an NT system (should have security
629 * APIs) or a non-NT system (don't do security).
631 memset(&osi
, 0, sizeof(OSVERSIONINFO
));
632 osi
.dwOSVersionInfoSize
= sizeof(OSVERSIONINFO
);
633 if (GetVersionEx(&osi
) && osi
.dwPlatformId
==VER_PLATFORM_WIN32_NT
) {
636 has_security
= FALSE
;
640 * Attempt to ge the security API we need.
642 advapi
= LoadLibrary("ADVAPI32.DLL");
643 getsecurityinfo
= (gsi_fn_t
)GetProcAddress(advapi
, "GetSecurityInfo");
644 if (!getsecurityinfo
) {
646 "Unable to access security APIs. Pageant will\n"
647 "not run, in case it causes a security breach.",
648 "Pageant Fatal Error", MB_ICONERROR
| MB_OK
);
655 * First bomb out totally if we are already running.
657 if (FindWindow("Pageant", "Pageant")) {
658 MessageBox(NULL
, "Pageant is already running", "Pageant Error",
659 MB_ICONERROR
| MB_OK
);
660 if (advapi
) FreeLibrary(advapi
);
668 wndclass
.lpfnWndProc
= WndProc
;
669 wndclass
.cbClsExtra
= 0;
670 wndclass
.cbWndExtra
= 0;
671 wndclass
.hInstance
= inst
;
672 wndclass
.hIcon
= LoadIcon (inst
,
673 MAKEINTRESOURCE(IDI_MAINICON
));
674 wndclass
.hCursor
= LoadCursor (NULL
, IDC_IBEAM
);
675 wndclass
.hbrBackground
= GetStockObject (BLACK_BRUSH
);
676 wndclass
.lpszMenuName
= NULL
;
677 wndclass
.lpszClassName
= APPNAME
;
679 RegisterClass (&wndclass
);
682 hwnd
= keylist
= NULL
;
684 hwnd
= CreateWindow (APPNAME
, APPNAME
,
685 WS_OVERLAPPEDWINDOW
| WS_VSCROLL
,
686 CW_USEDEFAULT
, CW_USEDEFAULT
,
687 100, 100, NULL
, NULL
, inst
, NULL
);
689 /* Set up a system tray icon */
695 #ifdef NIM_SETVERSION
697 res
= Shell_NotifyIcon(NIM_SETVERSION
, &tnid
);
700 tnid
.cbSize
= sizeof(NOTIFYICONDATA
);
702 tnid
.uID
= 1; /* unique within this systray use */
703 tnid
.uFlags
= NIF_MESSAGE
| NIF_ICON
| NIF_TIP
;
704 tnid
.uCallbackMessage
= WM_SYSTRAY
;
705 tnid
.hIcon
= hicon
= LoadIcon (instance
, MAKEINTRESOURCE(201));
706 strcpy(tnid
.szTip
, "Pageant (PuTTY authentication agent)");
708 res
= Shell_NotifyIcon(NIM_ADD
, &tnid
);
713 systray_menu
= CreatePopupMenu();
714 AppendMenu (systray_menu
, MF_ENABLED
, IDM_VIEWKEYS
, "&View Keys");
715 AppendMenu (systray_menu
, MF_ENABLED
, IDM_CLOSE
, "E&xit");
718 ShowWindow (hwnd
, SW_HIDE
);
721 * Initialise storage for RSA keys.
723 rsakeys
= newtree234(cmpkeys
);
726 * Process the command line and add RSA keys as listed on it.
733 while (*p
&& isspace(*p
)) p
++;
734 if (*p
&& !isspace(*p
)) {
735 char *q
= p
, *pp
= p
;
736 while (*p
&& (inquotes
|| !isspace(*p
)))
739 inquotes
= !inquotes
;
757 while (GetMessage(&msg
, NULL
, 0, 0) == 1) {
758 TranslateMessage(&msg
);
759 DispatchMessage(&msg
);
762 /* Clean up the system tray icon */
766 tnid
.cbSize
= sizeof(NOTIFYICONDATA
);
770 Shell_NotifyIcon(NIM_DELETE
, &tnid
);
772 DestroyMenu(systray_menu
);
775 if (advapi
) FreeLibrary(advapi
);