2 * Pageant: the PuTTY Authentication Agent.
12 #include "putty.h" // FIXME
16 #define IDI_MAINICON 200
17 #define IDI_TRAYICON 201
19 #define WM_XUSER (WM_USER + 0x2000)
20 #define WM_SYSTRAY (WM_XUSER + 6)
21 #define WM_SYSTRAY2 (WM_XUSER + 7)
23 #define AGENT_COPYDATA_ID 0x804e50ba /* random goop */
26 * FIXME: maybe some day we can sort this out ...
28 #define AGENT_MAX_MSGLEN 8192
30 #define IDM_CLOSE 0x0010
31 #define IDM_VIEWKEYS 0x0020
32 #define IDM_ADDKEY 0x0030
33 #define IDM_ABOUT 0x0040
35 #define APPNAME "Pageant"
38 * SSH1 agent messages.
40 #define SSH1_AGENTC_REQUEST_RSA_IDENTITIES 1
41 #define SSH1_AGENT_RSA_IDENTITIES_ANSWER 2
42 #define SSH1_AGENTC_RSA_CHALLENGE 3
43 #define SSH1_AGENT_RSA_RESPONSE 4
44 #define SSH1_AGENTC_ADD_RSA_IDENTITY 7
45 #define SSH1_AGENTC_REMOVE_RSA_IDENTITY 8
46 #define SSH1_AGENTC_REMOVE_ALL_RSA_IDENTITIES 9 /* openssh private? */
49 * Messages common to SSH1 and OpenSSH's SSH2.
51 #define SSH_AGENT_FAILURE 5
52 #define SSH_AGENT_SUCCESS 6
55 * OpenSSH's SSH2 agent messages.
57 #define SSH2_AGENTC_REQUEST_IDENTITIES 11
58 #define SSH2_AGENT_IDENTITIES_ANSWER 12
59 #define SSH2_AGENTC_SIGN_REQUEST 13
60 #define SSH2_AGENT_SIGN_RESPONSE 14
61 #define SSH2_AGENTC_ADD_IDENTITY 17
62 #define SSH2_AGENTC_REMOVE_IDENTITY 18
63 #define SSH2_AGENTC_REMOVE_ALL_IDENTITIES 19
67 static HINSTANCE instance
;
71 static HMENU systray_menu
;
73 static tree234
*rsakeys
, *ssh2keys
;
75 static int has_security
;
77 typedef DWORD (WINAPI
*gsi_fn_t
)
78 (HANDLE
, SE_OBJECT_TYPE
, SECURITY_INFORMATION
,
79 PSID
*, PSID
*, PACL
*, PACL
*,
80 PSECURITY_DESCRIPTOR
*);
81 static gsi_fn_t getsecurityinfo
;
85 * We need this to link with the RSA code, because rsaencrypt()
86 * pads its data with random bytes. Since we only use rsadecrypt()
87 * and the signing functions, which are deterministic, this should
90 * If it _is_ called, there is a _serious_ problem, because it
91 * won't generate true random numbers. So we must scream, panic,
92 * and exit immediately if that should happen.
94 int random_byte(void) {
95 MessageBox(hwnd
, "Internal Error", APPNAME
, MB_OK
| MB_ICONERROR
);
100 * Blob structure for passing to the asymmetric SSH2 key compare
101 * function, prototyped here.
107 static int cmpkeys_ssh2_asymm(void *av
, void *bv
);
110 * This function is needed to link with the DES code. We need not
111 * have it do anything at all.
113 void logevent(char *msg
) {
116 #define GET_32BIT(cp) \
117 (((unsigned long)(unsigned char)(cp)[0] << 24) | \
118 ((unsigned long)(unsigned char)(cp)[1] << 16) | \
119 ((unsigned long)(unsigned char)(cp)[2] << 8) | \
120 ((unsigned long)(unsigned char)(cp)[3]))
122 #define PUT_32BIT(cp, value) { \
123 (cp)[0] = (unsigned char)((value) >> 24); \
124 (cp)[1] = (unsigned char)((value) >> 16); \
125 (cp)[2] = (unsigned char)((value) >> 8); \
126 (cp)[3] = (unsigned char)(value); }
128 #define PASSPHRASE_MAXLEN 512
130 struct PassphraseProcStruct
{
136 * Dialog-box function for the Licence box.
138 static int CALLBACK
LicenceProc (HWND hwnd
, UINT msg
,
139 WPARAM wParam
, LPARAM lParam
) {
144 switch (LOWORD(wParam
)) {
158 * Dialog-box function for the About box.
160 static int CALLBACK
AboutProc (HWND hwnd
, UINT msg
,
161 WPARAM wParam
, LPARAM lParam
) {
164 SetDlgItemText (hwnd
, 100, ver
);
167 switch (LOWORD(wParam
)) {
170 DestroyWindow (hwnd
);
173 EnableWindow(hwnd
, 0);
174 DialogBox (instance
, MAKEINTRESOURCE(214), NULL
, LicenceProc
);
175 EnableWindow(hwnd
, 1);
176 SetActiveWindow(hwnd
);
182 DestroyWindow (hwnd
);
189 * Dialog-box function for the passphrase box.
191 static int CALLBACK
PassphraseProc(HWND hwnd
, UINT msg
,
192 WPARAM wParam
, LPARAM lParam
) {
193 static char *passphrase
;
194 struct PassphraseProcStruct
*p
;
201 { /* centre the window */
205 hw
= GetDesktopWindow();
206 if (GetWindowRect (hw
, &rs
) && GetWindowRect (hwnd
, &rd
))
207 MoveWindow (hwnd
, (rs
.right
+ rs
.left
+ rd
.left
- rd
.right
)/2,
208 (rs
.bottom
+ rs
.top
+ rd
.top
- rd
.bottom
)/2,
209 rd
.right
-rd
.left
, rd
.bottom
-rd
.top
, TRUE
);
212 SetForegroundWindow(hwnd
);
213 SetWindowPos (hwnd
, HWND_TOP
, 0, 0, 0, 0,
214 SWP_NOMOVE
| SWP_NOSIZE
| SWP_SHOWWINDOW
);
215 p
= (struct PassphraseProcStruct
*)lParam
;
216 passphrase
= p
->passphrase
;
218 SetDlgItemText(hwnd
, 101, p
->comment
);
222 switch (LOWORD(wParam
)) {
232 case 102: /* edit box */
233 if (HIWORD(wParam
) == EN_CHANGE
) {
234 GetDlgItemText (hwnd
, 102, passphrase
, PASSPHRASE_MAXLEN
-1);
235 passphrase
[PASSPHRASE_MAXLEN
-1] = '\0';
248 * Update the visible key list.
250 static void keylist_update(void) {
252 struct ssh2_userkey
*skey
;
256 SendDlgItemMessage(keylist
, 100, LB_RESETCONTENT
, 0, 0);
257 for (rkey
= first234(rsakeys
, &e
); rkey
; rkey
= next234(&e
)) {
258 char listentry
[512], *p
;
260 * Replace two spaces in the fingerprint with tabs, for
261 * nice alignment in the box.
263 strcpy(listentry
, "ssh1\t");
264 p
= listentry
+strlen(listentry
);
265 rsa_fingerprint(p
, sizeof(listentry
)-(p
-listentry
), rkey
);
266 p
= strchr(listentry
, ' '); if (p
) *p
= '\t';
267 p
= strchr(listentry
, ' '); if (p
) *p
= '\t';
268 SendDlgItemMessage (keylist
, 100, LB_ADDSTRING
,
269 0, (LPARAM
)listentry
);
271 for (skey
= first234(ssh2keys
, &e
); skey
; skey
= next234(&e
)) {
272 char listentry
[512], *p
;
275 * Replace two spaces in the fingerprint with tabs, for
276 * nice alignment in the box.
278 p
= skey
->alg
->fingerprint(skey
->data
);
279 strncpy(listentry
, p
, sizeof(listentry
));
280 p
= strchr(listentry
, ' '); if (p
) *p
= '\t';
281 p
= strchr(listentry
, ' '); if (p
) *p
= '\t';
282 len
= strlen(listentry
);
283 if (len
< sizeof(listentry
)-2) {
284 listentry
[len
] = '\t';
285 strncpy(listentry
+len
+1, skey
->comment
, sizeof(listentry
)-len
-1);
287 SendDlgItemMessage (keylist
, 100, LB_ADDSTRING
,
288 0, (LPARAM
)listentry
);
290 SendDlgItemMessage (keylist
, 100, LB_SETCURSEL
, (WPARAM
) -1, 0);
295 * This function loads a key from a file and adds it.
297 static void add_keyfile(char *filename
) {
298 char passphrase
[PASSPHRASE_MAXLEN
];
300 struct ssh2_userkey
*skey
;
305 struct PassphraseProcStruct pps
;
308 ver
= keyfile_version(filename
);
310 MessageBox(NULL
, "Couldn't load private key.", APPNAME
,
311 MB_OK
| MB_ICONERROR
);
316 needs_pass
= rsakey_encrypted(filename
, &comment
);
318 needs_pass
= ssh2_userkey_encrypted(filename
, &comment
);
321 rkey
= smalloc(sizeof(*rkey
));
322 pps
.passphrase
= passphrase
;
323 pps
.comment
= comment
;
327 dlgret
= DialogBoxParam(instance
, MAKEINTRESOURCE(210),
328 NULL
, PassphraseProc
,
331 if (comment
) sfree(comment
);
334 return; /* operation cancelled */
339 ret
= loadrsakey(filename
, rkey
, passphrase
);
341 skey
= ssh2_load_userkey(filename
, passphrase
);
342 if (skey
== SSH2_WRONG_PASSPHRASE
)
351 if (comment
) sfree(comment
);
353 MessageBox(NULL
, "Couldn't load private key.", APPNAME
,
354 MB_OK
| MB_ICONERROR
);
360 if (add234(rsakeys
, rkey
) != rkey
)
361 sfree(rkey
); /* already present, don't waste RAM */
363 if (add234(ssh2keys
, skey
) != skey
) {
364 skey
->alg
->freekey(skey
->data
);
365 sfree(skey
); /* already present, don't waste RAM */
371 * This is the main agent function that answers messages.
373 static void answer_msg(void *msg
) {
374 unsigned char *p
= msg
;
375 unsigned char *ret
= msg
;
379 * Get the message type.
385 case SSH1_AGENTC_REQUEST_RSA_IDENTITIES
:
387 * Reply with SSH1_AGENT_RSA_IDENTITIES_ANSWER.
395 * Count up the number and length of keys we hold.
398 for (key
= first234(rsakeys
, &e
); key
; key
= next234(&e
)) {
400 len
+= 4; /* length field */
401 len
+= ssh1_bignum_length(key
->exponent
);
402 len
+= ssh1_bignum_length(key
->modulus
);
403 len
+= 4 + strlen(key
->comment
);
407 * Packet header is the obvious five bytes, plus four
408 * bytes for the key count.
411 if (len
> AGENT_MAX_MSGLEN
)
412 goto failure
; /* aaargh! too much stuff! */
413 PUT_32BIT(ret
, len
-4);
414 ret
[4] = SSH1_AGENT_RSA_IDENTITIES_ANSWER
;
415 PUT_32BIT(ret
+5, nkeys
);
417 for (key
= first234(rsakeys
, &e
); key
; key
= next234(&e
)) {
418 PUT_32BIT(p
, ssh1_bignum_bitcount(key
->modulus
));
420 p
+= ssh1_write_bignum(p
, key
->exponent
);
421 p
+= ssh1_write_bignum(p
, key
->modulus
);
422 PUT_32BIT(p
, strlen(key
->comment
));
423 memcpy(p
+4, key
->comment
, strlen(key
->comment
));
424 p
+= 4 + strlen(key
->comment
);
428 case SSH2_AGENTC_REQUEST_IDENTITIES
:
430 * Reply with SSH2_AGENT_IDENTITIES_ANSWER.
434 struct ssh2_userkey
*key
;
440 * Count up the number and length of keys we hold.
443 for (key
= first234(ssh2keys
, &e
); key
; key
= next234(&e
)) {
445 len
+= 4; /* length field */
446 blob
= key
->alg
->public_blob(key
->data
, &bloblen
);
449 len
+= 4 + strlen(key
->comment
);
453 * Packet header is the obvious five bytes, plus four
454 * bytes for the key count.
457 if (len
> AGENT_MAX_MSGLEN
)
458 goto failure
; /* aaargh! too much stuff! */
459 PUT_32BIT(ret
, len
-4);
460 ret
[4] = SSH2_AGENT_IDENTITIES_ANSWER
;
461 PUT_32BIT(ret
+5, nkeys
);
463 for (key
= first234(ssh2keys
, &e
); key
; key
= next234(&e
)) {
464 blob
= key
->alg
->public_blob(key
->data
, &bloblen
);
465 PUT_32BIT(p
, bloblen
);
467 memcpy(p
, blob
, bloblen
);
470 PUT_32BIT(p
, strlen(key
->comment
));
471 memcpy(p
+4, key
->comment
, strlen(key
->comment
));
472 p
+= 4 + strlen(key
->comment
);
476 case SSH1_AGENTC_RSA_CHALLENGE
:
478 * Reply with either SSH1_AGENT_RSA_RESPONSE or
479 * SSH_AGENT_FAILURE, depending on whether we have that key
483 struct RSAKey reqkey
, *key
;
484 Bignum challenge
, response
;
485 unsigned char response_source
[48], response_md5
[16];
486 struct MD5Context md5c
;
490 p
+= ssh1_read_bignum(p
, &reqkey
.exponent
);
491 p
+= ssh1_read_bignum(p
, &reqkey
.modulus
);
492 p
+= ssh1_read_bignum(p
, &challenge
);
493 memcpy(response_source
+32, p
, 16); p
+= 16;
494 if (GET_32BIT(p
) != 1 ||
495 (key
= find234(rsakeys
, &reqkey
, NULL
)) == NULL
) {
496 freebn(reqkey
.exponent
);
497 freebn(reqkey
.modulus
);
501 response
= rsadecrypt(challenge
, key
);
502 for (i
= 0; i
< 32; i
++)
503 response_source
[i
] = bignum_byte(response
, 31-i
);
506 MD5Update(&md5c
, response_source
, 48);
507 MD5Final(response_md5
, &md5c
);
508 memset(response_source
, 0, 48); /* burn the evidence */
509 freebn(response
); /* and that evidence */
510 freebn(challenge
); /* yes, and that evidence */
511 freebn(reqkey
.exponent
); /* and free some memory ... */
512 freebn(reqkey
.modulus
); /* ... while we're at it. */
515 * Packet is the obvious five byte header, plus sixteen
519 PUT_32BIT(ret
, len
-4);
520 ret
[4] = SSH1_AGENT_RSA_RESPONSE
;
521 memcpy(ret
+5, response_md5
, 16);
524 case SSH2_AGENTC_SIGN_REQUEST
:
526 * Reply with either SSH2_AGENT_RSA_RESPONSE or
527 * SSH_AGENT_FAILURE, depending on whether we have that key
531 struct ssh2_userkey
*key
;
533 unsigned char *data
, *signature
;
534 int datalen
, siglen
, len
;
536 b
.len
= GET_32BIT(p
);
540 datalen
= GET_32BIT(p
);
543 key
= find234(ssh2keys
, &b
, cmpkeys_ssh2_asymm
);
546 signature
= key
->alg
->sign(key
->data
, data
, datalen
, &siglen
);
548 PUT_32BIT(ret
, len
-4);
549 ret
[4] = SSH2_AGENT_SIGN_RESPONSE
;
550 PUT_32BIT(ret
+5, siglen
);
551 memcpy(ret
+5+4, signature
, siglen
);
555 case SSH1_AGENTC_ADD_RSA_IDENTITY
:
557 * Add to the list and return SSH_AGENT_SUCCESS, or
558 * SSH_AGENT_FAILURE if the key was malformed.
563 key
= smalloc(sizeof(struct RSAKey
));
564 memset(key
, 0, sizeof(key
));
565 p
+= makekey(p
, key
, NULL
, 1);
566 p
+= makeprivate(p
, key
);
567 p
+= ssh1_read_bignum(p
, key
->iqmp
); /* p^-1 mod q */
568 p
+= ssh1_read_bignum(p
, key
->p
); /* p */
569 p
+= ssh1_read_bignum(p
, key
->q
); /* q */
570 comment
= smalloc(GET_32BIT(p
));
572 memcpy(comment
, p
+4, GET_32BIT(p
));
573 key
->comment
= comment
;
576 ret
[4] = SSH_AGENT_FAILURE
;
577 if (add234(rsakeys
, key
) == key
) {
579 ret
[4] = SSH_AGENT_SUCCESS
;
586 case SSH2_AGENTC_ADD_IDENTITY
:
588 * Add to the list and return SSH_AGENT_SUCCESS, or
589 * SSH_AGENT_FAILURE if the key was malformed.
592 struct ssh2_userkey
*key
;
597 key
= smalloc(sizeof(struct ssh2_userkey
));
599 alglen
= GET_32BIT(p
); p
+= 4;
600 alg
= p
; p
+= alglen
;
601 /* Add further algorithm names here. */
602 if (alglen
== 7 && !memcmp(alg
, "ssh-rsa", 7))
609 bloblen
= GET_32BIT((unsigned char *)msg
) - (p
-(unsigned char *)msg
-4);
610 key
->data
= key
->alg
->openssh_createkey(&p
, &bloblen
);
615 commlen
= GET_32BIT(p
); p
+= 4;
617 comment
= smalloc(commlen
+1);
619 memcpy(comment
, p
, commlen
);
620 comment
[commlen
] = '\0';
622 key
->comment
= comment
;
625 ret
[4] = SSH_AGENT_FAILURE
;
626 if (add234(ssh2keys
, key
) == key
) {
628 ret
[4] = SSH_AGENT_SUCCESS
;
630 key
->alg
->freekey(key
->data
);
636 case SSH1_AGENTC_REMOVE_RSA_IDENTITY
:
638 * Remove from the list and return SSH_AGENT_SUCCESS, or
639 * perhaps SSH_AGENT_FAILURE if it wasn't in the list to
643 struct RSAKey reqkey
, *key
;
645 p
+= makekey(p
, &reqkey
, NULL
, 0);
646 key
= find234(rsakeys
, &reqkey
, NULL
);
647 freebn(reqkey
.exponent
);
648 freebn(reqkey
.modulus
);
650 ret
[4] = SSH_AGENT_FAILURE
;
652 del234(rsakeys
, key
);
656 ret
[4] = SSH_AGENT_SUCCESS
;
660 case SSH2_AGENTC_REMOVE_IDENTITY
:
662 * Remove from the list and return SSH_AGENT_SUCCESS, or
663 * perhaps SSH_AGENT_FAILURE if it wasn't in the list to
667 struct ssh2_userkey
*key
;
670 b
.len
= GET_32BIT(p
);
674 key
= find234(ssh2keys
, &b
, cmpkeys_ssh2_asymm
);
679 ret
[4] = SSH_AGENT_FAILURE
;
681 del234(ssh2keys
, key
);
683 key
->alg
->freekey(key
->data
);
685 ret
[4] = SSH_AGENT_SUCCESS
;
689 case SSH1_AGENTC_REMOVE_ALL_RSA_IDENTITIES
:
691 * Remove all SSH1 keys. Always returns success.
697 while ( (rkey
= first234(rsakeys
, &e
)) != NULL
) {
698 del234(rsakeys
, rkey
);
705 ret
[4] = SSH_AGENT_SUCCESS
;
708 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES
:
710 * Remove all SSH2 keys. Always returns success.
713 struct ssh2_userkey
*skey
;
716 while ( (skey
= first234(ssh2keys
, &e
)) != NULL
) {
717 del234(ssh2keys
, skey
);
718 skey
->alg
->freekey(skey
->data
);
724 ret
[4] = SSH_AGENT_SUCCESS
;
730 * Unrecognised message. Return SSH_AGENT_FAILURE.
733 ret
[4] = SSH_AGENT_FAILURE
;
739 * Key comparison function for the 2-3-4 tree of RSA keys.
741 static int cmpkeys_rsa(void *av
, void *bv
) {
742 struct RSAKey
*a
= (struct RSAKey
*)av
;
743 struct RSAKey
*b
= (struct RSAKey
*)bv
;
750 * Compare by length of moduli.
752 alen
= ssh1_bignum_bitcount(am
);
753 blen
= ssh1_bignum_bitcount(bm
);
754 if (alen
> blen
) return +1; else if (alen
< blen
) return -1;
756 * Now compare by moduli themselves.
758 alen
= (alen
+ 7) / 8; /* byte count */
761 abyte
= bignum_byte(am
, alen
);
762 bbyte
= bignum_byte(bm
, alen
);
763 if (abyte
> bbyte
) return +1; else if (abyte
< bbyte
) return -1;
772 * Key comparison function for the 2-3-4 tree of SSH2 keys.
774 static int cmpkeys_ssh2(void *av
, void *bv
) {
775 struct ssh2_userkey
*a
= (struct ssh2_userkey
*)av
;
776 struct ssh2_userkey
*b
= (struct ssh2_userkey
*)bv
;
779 unsigned char *ablob
, *bblob
;
783 * Compare purely by public blob.
785 ablob
= a
->alg
->public_blob(a
->data
, &alen
);
786 bblob
= b
->alg
->public_blob(b
->data
, &blen
);
789 for (i
= 0; i
< alen
&& i
< blen
; i
++) {
790 if (ablob
[i
] < bblob
[i
]) {
792 } else if (ablob
[i
] > bblob
[i
]) {
796 if (c
== 0 && i
< alen
) c
= +1; /* a is longer */
797 if (c
== 0 && i
< blen
) c
= -1; /* a is longer */
806 * Key comparison function for looking up a blob in the 2-3-4 tree
809 static int cmpkeys_ssh2_asymm(void *av
, void *bv
) {
810 struct blob
*a
= (struct blob
*)av
;
811 struct ssh2_userkey
*b
= (struct ssh2_userkey
*)bv
;
814 unsigned char *ablob
, *bblob
;
818 * Compare purely by public blob.
822 bblob
= b
->alg
->public_blob(b
->data
, &blen
);
825 for (i
= 0; i
< alen
&& i
< blen
; i
++) {
826 if (ablob
[i
] < bblob
[i
]) {
828 } else if (ablob
[i
] > bblob
[i
]) {
832 if (c
== 0 && i
< alen
) c
= +1; /* a is longer */
833 if (c
== 0 && i
< blen
) c
= -1; /* a is longer */
840 static void error(char *s
) {
841 MessageBox(hwnd
, s
, APPNAME
, MB_OK
| MB_ICONERROR
);
845 * Prompt for a key file to add, and add it.
847 static void prompt_add_keyfile(void) {
849 char filename
[FILENAME_MAX
];
850 memset(&of
, 0, sizeof(of
));
851 #ifdef OPENFILENAME_SIZE_VERSION_400
852 of
.lStructSize
= OPENFILENAME_SIZE_VERSION_400
;
854 of
.lStructSize
= sizeof(of
);
857 of
.lpstrFilter
= "All Files\0*\0\0\0";
858 of
.lpstrCustomFilter
= NULL
;
860 of
.lpstrFile
= filename
; *filename
= '\0';
861 of
.nMaxFile
= sizeof(filename
);
862 of
.lpstrFileTitle
= NULL
;
863 of
.lpstrInitialDir
= NULL
;
864 of
.lpstrTitle
= "Select Private Key File";
866 if (GetOpenFileName(&of
)) {
867 add_keyfile(filename
);
873 * Dialog-box function for the key list box.
875 static int CALLBACK
KeyListProc(HWND hwnd
, UINT msg
,
876 WPARAM wParam
, LPARAM lParam
) {
879 struct ssh2_userkey
*skey
;
886 { /* centre the window */
890 hw
= GetDesktopWindow();
891 if (GetWindowRect (hw
, &rs
) && GetWindowRect (hwnd
, &rd
))
892 MoveWindow (hwnd
, (rs
.right
+ rs
.left
+ rd
.left
- rd
.right
)/2,
893 (rs
.bottom
+ rs
.top
+ rd
.top
- rd
.bottom
)/2,
894 rd
.right
-rd
.left
, rd
.bottom
-rd
.top
, TRUE
);
899 static int tabs
[] = {35, 60, 210};
900 SendDlgItemMessage (hwnd
, 100, LB_SETTABSTOPS
,
901 sizeof(tabs
)/sizeof(*tabs
), (LPARAM
) tabs
);
906 switch (LOWORD(wParam
)) {
912 case 101: /* add key */
913 if (HIWORD(wParam
) == BN_CLICKED
||
914 HIWORD(wParam
) == BN_DOUBLECLICKED
) {
915 prompt_add_keyfile();
918 case 102: /* remove key */
919 if (HIWORD(wParam
) == BN_CLICKED
||
920 HIWORD(wParam
) == BN_DOUBLECLICKED
) {
921 int n
= SendDlgItemMessage (hwnd
, 100, LB_GETCURSEL
, 0, 0);
926 for (rkey
= first234(rsakeys
, &e
); rkey
; rkey
= next234(&e
))
930 del234(rsakeys
, rkey
);
934 for (skey
= first234(ssh2keys
, &e
); skey
; skey
= next234(&e
))
938 del234(ssh2keys
, skey
);
939 skey
->alg
->freekey(skey
->data
);
956 static LRESULT CALLBACK
WndProc (HWND hwnd
, UINT message
,
957 WPARAM wParam
, LPARAM lParam
) {
959 static int menuinprogress
;
963 if (lParam
== WM_RBUTTONUP
) {
965 GetCursorPos(&cursorpos
);
966 PostMessage(hwnd
, WM_SYSTRAY2
, cursorpos
.x
, cursorpos
.y
);
967 } else if (lParam
== WM_LBUTTONDBLCLK
) {
968 /* Equivalent to IDM_VIEWKEYS. */
969 PostMessage(hwnd
, WM_COMMAND
, IDM_VIEWKEYS
, 0);
973 if (!menuinprogress
) {
975 SetForegroundWindow(hwnd
);
976 ret
= TrackPopupMenu(systray_menu
,
977 TPM_RIGHTALIGN
| TPM_BOTTOMALIGN
|
979 wParam
, lParam
, 0, hwnd
, NULL
);
985 switch (wParam
& ~0xF) { /* low 4 bits reserved to Windows */
987 SendMessage(hwnd
, WM_CLOSE
, 0, 0);
991 keylist
= CreateDialog (instance
, MAKEINTRESOURCE(211),
993 ShowWindow (keylist
, SW_SHOWNORMAL
);
995 * Sometimes the window comes up minimised / hidden
996 * for no obvious reason. Prevent this.
998 SetForegroundWindow(keylist
);
999 SetWindowPos (keylist
, HWND_TOP
, 0, 0, 0, 0,
1000 SWP_NOMOVE
| SWP_NOSIZE
| SWP_SHOWWINDOW
);
1004 prompt_add_keyfile();
1008 aboutbox
= CreateDialog (instance
, MAKEINTRESOURCE(213),
1010 ShowWindow (aboutbox
, SW_SHOWNORMAL
);
1012 * Sometimes the window comes up minimised / hidden
1013 * for no obvious reason. Prevent this.
1015 SetForegroundWindow(aboutbox
);
1016 SetWindowPos (aboutbox
, HWND_TOP
, 0, 0, 0, 0,
1017 SWP_NOMOVE
| SWP_NOSIZE
| SWP_SHOWWINDOW
);
1023 PostQuitMessage (0);
1027 COPYDATASTRUCT
*cds
;
1030 HANDLE filemap
, proc
;
1031 PSID mapowner
, procowner
;
1032 PSECURITY_DESCRIPTOR psd1
= NULL
, psd2
= NULL
;
1035 cds
= (COPYDATASTRUCT
*)lParam
;
1036 if (cds
->dwData
!= AGENT_COPYDATA_ID
)
1037 return 0; /* not our message, mate */
1038 mapname
= (char *)cds
->lpData
;
1039 if (mapname
[cds
->cbData
- 1] != '\0')
1040 return 0; /* failure to be ASCIZ! */
1042 debug(("mapname is :%s:\r\n", mapname
));
1044 filemap
= OpenFileMapping(FILE_MAP_ALL_ACCESS
, FALSE
, mapname
);
1046 debug(("filemap is %p\r\n", filemap
));
1048 if (filemap
!= NULL
&& filemap
!= INVALID_HANDLE_VALUE
) {
1052 if ((proc
= OpenProcess(MAXIMUM_ALLOWED
, FALSE
,
1053 GetCurrentProcessId())) == NULL
) {
1055 debug(("couldn't get handle for process\r\n"));
1059 if (getsecurityinfo(proc
, SE_KERNEL_OBJECT
,
1060 OWNER_SECURITY_INFORMATION
,
1061 &procowner
, NULL
, NULL
, NULL
,
1062 &psd2
) != ERROR_SUCCESS
) {
1064 debug(("couldn't get owner info for process\r\n"));
1067 return 0; /* unable to get security info */
1070 if ((rc
= getsecurityinfo(filemap
, SE_KERNEL_OBJECT
,
1071 OWNER_SECURITY_INFORMATION
,
1072 &mapowner
, NULL
, NULL
, NULL
,
1073 &psd1
) != ERROR_SUCCESS
)) {
1075 debug(("couldn't get owner info for filemap: %d\r\n", rc
));
1080 debug(("got security stuff\r\n"));
1082 if (!EqualSid(mapowner
, procowner
))
1083 return 0; /* security ID mismatch! */
1085 debug(("security stuff matched\r\n"));
1091 debug(("security APIs not present\r\n"));
1095 p
= MapViewOfFile(filemap
, FILE_MAP_WRITE
, 0, 0, 0);
1097 debug(("p is %p\r\n", p
));
1098 {int i
; for(i
=0;i
<5;i
++)debug(("p[%d]=%02x\r\n", i
, ((unsigned char *)p
)[i
]));}
1104 CloseHandle(filemap
);
1109 return DefWindowProc (hwnd
, message
, wParam
, lParam
);
1112 int WINAPI
WinMain(HINSTANCE inst
, HINSTANCE prev
, LPSTR cmdline
, int show
) {
1119 * Determine whether we're an NT system (should have security
1120 * APIs) or a non-NT system (don't do security).
1122 memset(&osi
, 0, sizeof(OSVERSIONINFO
));
1123 osi
.dwOSVersionInfoSize
= sizeof(OSVERSIONINFO
);
1124 if (GetVersionEx(&osi
) && osi
.dwPlatformId
==VER_PLATFORM_WIN32_NT
) {
1125 has_security
= TRUE
;
1127 has_security
= FALSE
;
1132 * Attempt to ge the security API we need.
1134 advapi
= LoadLibrary("ADVAPI32.DLL");
1135 getsecurityinfo
= (gsi_fn_t
)GetProcAddress(advapi
, "GetSecurityInfo");
1136 if (!getsecurityinfo
) {
1138 "Unable to access security APIs. Pageant will\n"
1139 "not run, in case it causes a security breach.",
1140 "Pageant Fatal Error", MB_ICONERROR
| MB_OK
);
1145 "This program has been compiled for Win9X and will\n"
1146 "not run on NT, in case it causes a security breach.",
1147 "Pageant Fatal Error", MB_ICONERROR
| MB_OK
);
1154 * First bomb out totally if we are already running.
1156 if (FindWindow("Pageant", "Pageant")) {
1157 MessageBox(NULL
, "Pageant is already running", "Pageant Error",
1158 MB_ICONERROR
| MB_OK
);
1159 if (advapi
) FreeLibrary(advapi
);
1167 wndclass
.lpfnWndProc
= WndProc
;
1168 wndclass
.cbClsExtra
= 0;
1169 wndclass
.cbWndExtra
= 0;
1170 wndclass
.hInstance
= inst
;
1171 wndclass
.hIcon
= LoadIcon (inst
,
1172 MAKEINTRESOURCE(IDI_MAINICON
));
1173 wndclass
.hCursor
= LoadCursor (NULL
, IDC_IBEAM
);
1174 wndclass
.hbrBackground
= GetStockObject (BLACK_BRUSH
);
1175 wndclass
.lpszMenuName
= NULL
;
1176 wndclass
.lpszClassName
= APPNAME
;
1178 RegisterClass (&wndclass
);
1181 hwnd
= keylist
= NULL
;
1183 hwnd
= CreateWindow (APPNAME
, APPNAME
,
1184 WS_OVERLAPPEDWINDOW
| WS_VSCROLL
,
1185 CW_USEDEFAULT
, CW_USEDEFAULT
,
1186 100, 100, NULL
, NULL
, inst
, NULL
);
1188 /* Set up a system tray icon */
1191 NOTIFYICONDATA tnid
;
1194 #ifdef NIM_SETVERSION
1196 res
= Shell_NotifyIcon(NIM_SETVERSION
, &tnid
);
1199 tnid
.cbSize
= sizeof(NOTIFYICONDATA
);
1201 tnid
.uID
= 1; /* unique within this systray use */
1202 tnid
.uFlags
= NIF_MESSAGE
| NIF_ICON
| NIF_TIP
;
1203 tnid
.uCallbackMessage
= WM_SYSTRAY
;
1204 tnid
.hIcon
= hicon
= LoadIcon (instance
, MAKEINTRESOURCE(201));
1205 strcpy(tnid
.szTip
, "Pageant (PuTTY authentication agent)");
1207 res
= Shell_NotifyIcon(NIM_ADD
, &tnid
);
1212 systray_menu
= CreatePopupMenu();
1213 /* accelerators used: vkxa */
1214 AppendMenu (systray_menu
, MF_ENABLED
, IDM_VIEWKEYS
, "&View Keys");
1215 AppendMenu (systray_menu
, MF_ENABLED
, IDM_ADDKEY
, "Add &Key");
1216 AppendMenu (systray_menu
, MF_ENABLED
, IDM_ABOUT
, "&About");
1217 AppendMenu (systray_menu
, MF_ENABLED
, IDM_CLOSE
, "E&xit");
1220 ShowWindow (hwnd
, SW_HIDE
);
1223 * Initialise storage for RSA keys.
1225 rsakeys
= newtree234(cmpkeys_rsa
);
1226 ssh2keys
= newtree234(cmpkeys_ssh2
);
1229 * Process the command line and add keys as listed on it.
1236 while (*p
&& isspace(*p
)) p
++;
1237 if (*p
&& !isspace(*p
)) {
1238 char *q
= p
, *pp
= p
;
1239 while (*p
&& (inquotes
|| !isspace(*p
)))
1242 inquotes
= !inquotes
;
1258 * Main message loop.
1260 while (GetMessage(&msg
, NULL
, 0, 0) == 1) {
1261 TranslateMessage(&msg
);
1262 DispatchMessage(&msg
);
1265 /* Clean up the system tray icon */
1267 NOTIFYICONDATA tnid
;
1269 tnid
.cbSize
= sizeof(NOTIFYICONDATA
);
1273 Shell_NotifyIcon(NIM_DELETE
, &tnid
);
1275 DestroyMenu(systray_menu
);
1278 if (advapi
) FreeLibrary(advapi
);