2 * Pageant: the PuTTY Authentication Agent.
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"
39 static HINSTANCE instance
;
43 static HMENU systray_menu
;
45 static tree234
*rsakeys
, *ssh2keys
;
47 static int has_security
;
49 typedef DWORD (WINAPI
*gsi_fn_t
)
50 (HANDLE
, SE_OBJECT_TYPE
, SECURITY_INFORMATION
,
51 PSID
*, PSID
*, PACL
*, PACL
*,
52 PSECURITY_DESCRIPTOR
*);
53 static gsi_fn_t getsecurityinfo
;
57 * We need this to link with the RSA code, because rsaencrypt()
58 * pads its data with random bytes. Since we only use rsadecrypt()
59 * and the signing functions, which are deterministic, this should
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 * Blob structure for passing to the asymmetric SSH2 key compare
73 * function, prototyped here.
79 static int cmpkeys_ssh2_asymm(void *av
, void *bv
);
82 * This function is needed to link with the DES code. We need not
83 * have it do anything at all.
85 void logevent(char *msg
) {
88 #define GET_32BIT(cp) \
89 (((unsigned long)(unsigned char)(cp)[0] << 24) | \
90 ((unsigned long)(unsigned char)(cp)[1] << 16) | \
91 ((unsigned long)(unsigned char)(cp)[2] << 8) | \
92 ((unsigned long)(unsigned char)(cp)[3]))
94 #define PUT_32BIT(cp, value) { \
95 (cp)[0] = (unsigned char)((value) >> 24); \
96 (cp)[1] = (unsigned char)((value) >> 16); \
97 (cp)[2] = (unsigned char)((value) >> 8); \
98 (cp)[3] = (unsigned char)(value); }
100 #define PASSPHRASE_MAXLEN 512
102 struct PassphraseProcStruct
{
108 * Dialog-box function for the Licence box.
110 static int CALLBACK
LicenceProc (HWND hwnd
, UINT msg
,
111 WPARAM wParam
, LPARAM lParam
) {
116 switch (LOWORD(wParam
)) {
130 * Dialog-box function for the About box.
132 static int CALLBACK
AboutProc (HWND hwnd
, UINT msg
,
133 WPARAM wParam
, LPARAM lParam
) {
136 SetDlgItemText (hwnd
, 100, ver
);
139 switch (LOWORD(wParam
)) {
142 DestroyWindow (hwnd
);
145 EnableWindow(hwnd
, 0);
146 DialogBox (instance
, MAKEINTRESOURCE(214), NULL
, LicenceProc
);
147 EnableWindow(hwnd
, 1);
148 SetActiveWindow(hwnd
);
154 DestroyWindow (hwnd
);
161 * Dialog-box function for the passphrase box.
163 static int CALLBACK
PassphraseProc(HWND hwnd
, UINT msg
,
164 WPARAM wParam
, LPARAM lParam
) {
165 static char *passphrase
;
166 struct PassphraseProcStruct
*p
;
173 { /* centre the window */
177 hw
= GetDesktopWindow();
178 if (GetWindowRect (hw
, &rs
) && GetWindowRect (hwnd
, &rd
))
179 MoveWindow (hwnd
, (rs
.right
+ rs
.left
+ rd
.left
- rd
.right
)/2,
180 (rs
.bottom
+ rs
.top
+ rd
.top
- rd
.bottom
)/2,
181 rd
.right
-rd
.left
, rd
.bottom
-rd
.top
, TRUE
);
184 SetForegroundWindow(hwnd
);
185 SetWindowPos (hwnd
, HWND_TOP
, 0, 0, 0, 0,
186 SWP_NOMOVE
| SWP_NOSIZE
| SWP_SHOWWINDOW
);
187 p
= (struct PassphraseProcStruct
*)lParam
;
188 passphrase
= p
->passphrase
;
190 SetDlgItemText(hwnd
, 101, p
->comment
);
194 switch (LOWORD(wParam
)) {
204 case 102: /* edit box */
205 if (HIWORD(wParam
) == EN_CHANGE
) {
206 GetDlgItemText (hwnd
, 102, passphrase
, PASSPHRASE_MAXLEN
-1);
207 passphrase
[PASSPHRASE_MAXLEN
-1] = '\0';
220 * Update the visible key list.
222 static void keylist_update(void) {
224 struct ssh2_userkey
*skey
;
228 SendDlgItemMessage(keylist
, 100, LB_RESETCONTENT
, 0, 0);
229 for (rkey
= first234(rsakeys
, &e
); rkey
; rkey
= next234(&e
)) {
230 char listentry
[512], *p
;
232 * Replace two spaces in the fingerprint with tabs, for
233 * nice alignment in the box.
235 strcpy(listentry
, "ssh1\t");
236 p
= listentry
+strlen(listentry
);
237 rsa_fingerprint(p
, sizeof(listentry
)-(p
-listentry
), rkey
);
238 p
= strchr(listentry
, ' '); if (p
) *p
= '\t';
239 p
= strchr(listentry
, ' '); if (p
) *p
= '\t';
240 SendDlgItemMessage (keylist
, 100, LB_ADDSTRING
,
241 0, (LPARAM
)listentry
);
243 for (skey
= first234(ssh2keys
, &e
); skey
; skey
= next234(&e
)) {
244 char listentry
[512], *p
;
247 * Replace two spaces in the fingerprint with tabs, for
248 * nice alignment in the box.
250 p
= skey
->alg
->fingerprint(skey
->data
);
251 strncpy(listentry
, p
, sizeof(listentry
));
252 p
= strchr(listentry
, ' '); if (p
) *p
= '\t';
253 p
= strchr(listentry
, ' '); if (p
) *p
= '\t';
254 len
= strlen(listentry
);
255 if (len
< sizeof(listentry
)-2) {
256 listentry
[len
] = '\t';
257 strncpy(listentry
+len
+1, skey
->comment
, sizeof(listentry
)-len
-1);
259 SendDlgItemMessage (keylist
, 100, LB_ADDSTRING
,
260 0, (LPARAM
)listentry
);
262 SendDlgItemMessage (keylist
, 100, LB_SETCURSEL
, (WPARAM
) -1, 0);
267 * This function loads a key from a file and adds it.
269 static void add_keyfile(char *filename
) {
270 char passphrase
[PASSPHRASE_MAXLEN
];
272 struct ssh2_userkey
*skey
;
277 struct PassphraseProcStruct pps
;
280 ver
= keyfile_version(filename
);
282 MessageBox(NULL
, "Couldn't load private key.", APPNAME
,
283 MB_OK
| MB_ICONERROR
);
288 needs_pass
= rsakey_encrypted(filename
, &comment
);
290 needs_pass
= ssh2_userkey_encrypted(filename
, &comment
);
293 rkey
= smalloc(sizeof(*rkey
));
294 pps
.passphrase
= passphrase
;
295 pps
.comment
= comment
;
299 dlgret
= DialogBoxParam(instance
, MAKEINTRESOURCE(210),
300 NULL
, PassphraseProc
,
303 if (comment
) sfree(comment
);
306 return; /* operation cancelled */
311 ret
= loadrsakey(filename
, rkey
, passphrase
);
313 skey
= ssh2_load_userkey(filename
, passphrase
);
314 if (skey
== SSH2_WRONG_PASSPHRASE
)
323 if (comment
) sfree(comment
);
325 MessageBox(NULL
, "Couldn't load private key.", APPNAME
,
326 MB_OK
| MB_ICONERROR
);
332 if (add234(rsakeys
, rkey
) != rkey
)
333 sfree(rkey
); /* already present, don't waste RAM */
335 if (add234(ssh2keys
, skey
) != skey
) {
336 skey
->alg
->freekey(skey
->data
);
337 sfree(skey
); /* already present, don't waste RAM */
343 * This is the main agent function that answers messages.
345 static void answer_msg(void *msg
) {
346 unsigned char *p
= msg
;
347 unsigned char *ret
= msg
;
351 * Get the message type.
357 case SSH1_AGENTC_REQUEST_RSA_IDENTITIES
:
359 * Reply with SSH1_AGENT_RSA_IDENTITIES_ANSWER.
367 * Count up the number and length of keys we hold.
370 for (key
= first234(rsakeys
, &e
); key
; key
= next234(&e
)) {
372 len
+= 4; /* length field */
373 len
+= ssh1_bignum_length(key
->exponent
);
374 len
+= ssh1_bignum_length(key
->modulus
);
375 len
+= 4 + strlen(key
->comment
);
379 * Packet header is the obvious five bytes, plus four
380 * bytes for the key count.
383 if (len
> AGENT_MAX_MSGLEN
)
384 goto failure
; /* aaargh! too much stuff! */
385 PUT_32BIT(ret
, len
-4);
386 ret
[4] = SSH1_AGENT_RSA_IDENTITIES_ANSWER
;
387 PUT_32BIT(ret
+5, nkeys
);
389 for (key
= first234(rsakeys
, &e
); key
; key
= next234(&e
)) {
390 PUT_32BIT(p
, ssh1_bignum_bitcount(key
->modulus
));
392 p
+= ssh1_write_bignum(p
, key
->exponent
);
393 p
+= ssh1_write_bignum(p
, key
->modulus
);
394 PUT_32BIT(p
, strlen(key
->comment
));
395 memcpy(p
+4, key
->comment
, strlen(key
->comment
));
396 p
+= 4 + strlen(key
->comment
);
400 case SSH2_AGENTC_REQUEST_IDENTITIES
:
402 * Reply with SSH2_AGENT_IDENTITIES_ANSWER.
406 struct ssh2_userkey
*key
;
412 * Count up the number and length of keys we hold.
415 for (key
= first234(ssh2keys
, &e
); key
; key
= next234(&e
)) {
417 len
+= 4; /* length field */
418 blob
= key
->alg
->public_blob(key
->data
, &bloblen
);
421 len
+= 4 + strlen(key
->comment
);
425 * Packet header is the obvious five bytes, plus four
426 * bytes for the key count.
429 if (len
> AGENT_MAX_MSGLEN
)
430 goto failure
; /* aaargh! too much stuff! */
431 PUT_32BIT(ret
, len
-4);
432 ret
[4] = SSH2_AGENT_IDENTITIES_ANSWER
;
433 PUT_32BIT(ret
+5, nkeys
);
435 for (key
= first234(ssh2keys
, &e
); key
; key
= next234(&e
)) {
436 blob
= key
->alg
->public_blob(key
->data
, &bloblen
);
437 PUT_32BIT(p
, bloblen
);
439 memcpy(p
, blob
, bloblen
);
442 PUT_32BIT(p
, strlen(key
->comment
));
443 memcpy(p
+4, key
->comment
, strlen(key
->comment
));
444 p
+= 4 + strlen(key
->comment
);
448 case SSH1_AGENTC_RSA_CHALLENGE
:
450 * Reply with either SSH1_AGENT_RSA_RESPONSE or
451 * SSH_AGENT_FAILURE, depending on whether we have that key
455 struct RSAKey reqkey
, *key
;
456 Bignum challenge
, response
;
457 unsigned char response_source
[48], response_md5
[16];
458 struct MD5Context md5c
;
462 p
+= ssh1_read_bignum(p
, &reqkey
.exponent
);
463 p
+= ssh1_read_bignum(p
, &reqkey
.modulus
);
464 p
+= ssh1_read_bignum(p
, &challenge
);
465 memcpy(response_source
+32, p
, 16); p
+= 16;
466 if (GET_32BIT(p
) != 1 ||
467 (key
= find234(rsakeys
, &reqkey
, NULL
)) == NULL
) {
468 freebn(reqkey
.exponent
);
469 freebn(reqkey
.modulus
);
473 response
= rsadecrypt(challenge
, key
);
474 for (i
= 0; i
< 32; i
++)
475 response_source
[i
] = bignum_byte(response
, 31-i
);
478 MD5Update(&md5c
, response_source
, 48);
479 MD5Final(response_md5
, &md5c
);
480 memset(response_source
, 0, 48); /* burn the evidence */
481 freebn(response
); /* and that evidence */
482 freebn(challenge
); /* yes, and that evidence */
483 freebn(reqkey
.exponent
); /* and free some memory ... */
484 freebn(reqkey
.modulus
); /* ... while we're at it. */
487 * Packet is the obvious five byte header, plus sixteen
491 PUT_32BIT(ret
, len
-4);
492 ret
[4] = SSH1_AGENT_RSA_RESPONSE
;
493 memcpy(ret
+5, response_md5
, 16);
496 case SSH2_AGENTC_SIGN_REQUEST
:
498 * Reply with either SSH2_AGENT_RSA_RESPONSE or
499 * SSH_AGENT_FAILURE, depending on whether we have that key
503 struct ssh2_userkey
*key
;
505 unsigned char *data
, *signature
;
506 int datalen
, siglen
, len
;
508 b
.len
= GET_32BIT(p
);
512 datalen
= GET_32BIT(p
);
515 key
= find234(ssh2keys
, &b
, cmpkeys_ssh2_asymm
);
518 signature
= key
->alg
->sign(key
->data
, data
, datalen
, &siglen
);
520 PUT_32BIT(ret
, len
-4);
521 ret
[4] = SSH2_AGENT_SIGN_RESPONSE
;
522 PUT_32BIT(ret
+5, siglen
);
523 memcpy(ret
+5+4, signature
, siglen
);
527 case SSH1_AGENTC_ADD_RSA_IDENTITY
:
529 * Add to the list and return SSH_AGENT_SUCCESS, or
530 * SSH_AGENT_FAILURE if the key was malformed.
535 key
= smalloc(sizeof(struct RSAKey
));
536 memset(key
, 0, sizeof(key
));
537 p
+= makekey(p
, key
, NULL
, 1);
538 p
+= makeprivate(p
, key
);
539 p
+= ssh1_read_bignum(p
, key
->iqmp
); /* p^-1 mod q */
540 p
+= ssh1_read_bignum(p
, key
->p
); /* p */
541 p
+= ssh1_read_bignum(p
, key
->q
); /* q */
542 comment
= smalloc(GET_32BIT(p
));
544 memcpy(comment
, p
+4, GET_32BIT(p
));
545 key
->comment
= comment
;
548 ret
[4] = SSH_AGENT_FAILURE
;
549 if (add234(rsakeys
, key
) == key
) {
551 ret
[4] = SSH_AGENT_SUCCESS
;
558 case SSH2_AGENTC_ADD_IDENTITY
:
560 * Add to the list and return SSH_AGENT_SUCCESS, or
561 * SSH_AGENT_FAILURE if the key was malformed.
564 struct ssh2_userkey
*key
;
569 key
= smalloc(sizeof(struct ssh2_userkey
));
571 alglen
= GET_32BIT(p
); p
+= 4;
572 alg
= p
; p
+= alglen
;
573 /* Add further algorithm names here. */
574 if (alglen
== 7 && !memcmp(alg
, "ssh-rsa", 7))
581 bloblen
= GET_32BIT((unsigned char *)msg
) - (p
-(unsigned char *)msg
-4);
582 key
->data
= key
->alg
->openssh_createkey(&p
, &bloblen
);
587 commlen
= GET_32BIT(p
); p
+= 4;
589 comment
= smalloc(commlen
+1);
591 memcpy(comment
, p
, commlen
);
592 comment
[commlen
] = '\0';
594 key
->comment
= comment
;
597 ret
[4] = SSH_AGENT_FAILURE
;
598 if (add234(ssh2keys
, key
) == key
) {
600 ret
[4] = SSH_AGENT_SUCCESS
;
602 key
->alg
->freekey(key
->data
);
608 case SSH1_AGENTC_REMOVE_RSA_IDENTITY
:
610 * Remove from the list and return SSH_AGENT_SUCCESS, or
611 * perhaps SSH_AGENT_FAILURE if it wasn't in the list to
615 struct RSAKey reqkey
, *key
;
617 p
+= makekey(p
, &reqkey
, NULL
, 0);
618 key
= find234(rsakeys
, &reqkey
, NULL
);
619 freebn(reqkey
.exponent
);
620 freebn(reqkey
.modulus
);
622 ret
[4] = SSH_AGENT_FAILURE
;
624 del234(rsakeys
, key
);
628 ret
[4] = SSH_AGENT_SUCCESS
;
632 case SSH2_AGENTC_REMOVE_IDENTITY
:
634 * Remove from the list and return SSH_AGENT_SUCCESS, or
635 * perhaps SSH_AGENT_FAILURE if it wasn't in the list to
639 struct ssh2_userkey
*key
;
642 b
.len
= GET_32BIT(p
);
646 key
= find234(ssh2keys
, &b
, cmpkeys_ssh2_asymm
);
651 ret
[4] = SSH_AGENT_FAILURE
;
653 del234(ssh2keys
, key
);
655 key
->alg
->freekey(key
->data
);
657 ret
[4] = SSH_AGENT_SUCCESS
;
661 case SSH1_AGENTC_REMOVE_ALL_RSA_IDENTITIES
:
663 * Remove all SSH1 keys. Always returns success.
669 while ( (rkey
= first234(rsakeys
, &e
)) != NULL
) {
670 del234(rsakeys
, rkey
);
677 ret
[4] = SSH_AGENT_SUCCESS
;
680 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES
:
682 * Remove all SSH2 keys. Always returns success.
685 struct ssh2_userkey
*skey
;
688 while ( (skey
= first234(ssh2keys
, &e
)) != NULL
) {
689 del234(ssh2keys
, skey
);
690 skey
->alg
->freekey(skey
->data
);
696 ret
[4] = SSH_AGENT_SUCCESS
;
702 * Unrecognised message. Return SSH_AGENT_FAILURE.
705 ret
[4] = SSH_AGENT_FAILURE
;
711 * Key comparison function for the 2-3-4 tree of RSA keys.
713 static int cmpkeys_rsa(void *av
, void *bv
) {
714 struct RSAKey
*a
= (struct RSAKey
*)av
;
715 struct RSAKey
*b
= (struct RSAKey
*)bv
;
722 * Compare by length of moduli.
724 alen
= ssh1_bignum_bitcount(am
);
725 blen
= ssh1_bignum_bitcount(bm
);
726 if (alen
> blen
) return +1; else if (alen
< blen
) return -1;
728 * Now compare by moduli themselves.
730 alen
= (alen
+ 7) / 8; /* byte count */
733 abyte
= bignum_byte(am
, alen
);
734 bbyte
= bignum_byte(bm
, alen
);
735 if (abyte
> bbyte
) return +1; else if (abyte
< bbyte
) return -1;
744 * Key comparison function for the 2-3-4 tree of SSH2 keys.
746 static int cmpkeys_ssh2(void *av
, void *bv
) {
747 struct ssh2_userkey
*a
= (struct ssh2_userkey
*)av
;
748 struct ssh2_userkey
*b
= (struct ssh2_userkey
*)bv
;
751 unsigned char *ablob
, *bblob
;
755 * Compare purely by public blob.
757 ablob
= a
->alg
->public_blob(a
->data
, &alen
);
758 bblob
= b
->alg
->public_blob(b
->data
, &blen
);
761 for (i
= 0; i
< alen
&& i
< blen
; i
++) {
762 if (ablob
[i
] < bblob
[i
]) {
764 } else if (ablob
[i
] > bblob
[i
]) {
768 if (c
== 0 && i
< alen
) c
= +1; /* a is longer */
769 if (c
== 0 && i
< blen
) c
= -1; /* a is longer */
778 * Key comparison function for looking up a blob in the 2-3-4 tree
781 static int cmpkeys_ssh2_asymm(void *av
, void *bv
) {
782 struct blob
*a
= (struct blob
*)av
;
783 struct ssh2_userkey
*b
= (struct ssh2_userkey
*)bv
;
786 unsigned char *ablob
, *bblob
;
790 * Compare purely by public blob.
794 bblob
= b
->alg
->public_blob(b
->data
, &blen
);
797 for (i
= 0; i
< alen
&& i
< blen
; i
++) {
798 if (ablob
[i
] < bblob
[i
]) {
800 } else if (ablob
[i
] > bblob
[i
]) {
804 if (c
== 0 && i
< alen
) c
= +1; /* a is longer */
805 if (c
== 0 && i
< blen
) c
= -1; /* a is longer */
812 static void error(char *s
) {
813 MessageBox(hwnd
, s
, APPNAME
, MB_OK
| MB_ICONERROR
);
817 * Prompt for a key file to add, and add it.
819 static void prompt_add_keyfile(void) {
821 char filename
[FILENAME_MAX
];
822 memset(&of
, 0, sizeof(of
));
823 #ifdef OPENFILENAME_SIZE_VERSION_400
824 of
.lStructSize
= OPENFILENAME_SIZE_VERSION_400
;
826 of
.lStructSize
= sizeof(of
);
829 of
.lpstrFilter
= "All Files\0*\0\0\0";
830 of
.lpstrCustomFilter
= NULL
;
832 of
.lpstrFile
= filename
; *filename
= '\0';
833 of
.nMaxFile
= sizeof(filename
);
834 of
.lpstrFileTitle
= NULL
;
835 of
.lpstrInitialDir
= NULL
;
836 of
.lpstrTitle
= "Select Private Key File";
838 if (GetOpenFileName(&of
)) {
839 add_keyfile(filename
);
845 * Dialog-box function for the key list box.
847 static int CALLBACK
KeyListProc(HWND hwnd
, UINT msg
,
848 WPARAM wParam
, LPARAM lParam
) {
851 struct ssh2_userkey
*skey
;
858 { /* centre the window */
862 hw
= GetDesktopWindow();
863 if (GetWindowRect (hw
, &rs
) && GetWindowRect (hwnd
, &rd
))
864 MoveWindow (hwnd
, (rs
.right
+ rs
.left
+ rd
.left
- rd
.right
)/2,
865 (rs
.bottom
+ rs
.top
+ rd
.top
- rd
.bottom
)/2,
866 rd
.right
-rd
.left
, rd
.bottom
-rd
.top
, TRUE
);
871 static int tabs
[] = {35, 60, 210};
872 SendDlgItemMessage (hwnd
, 100, LB_SETTABSTOPS
,
873 sizeof(tabs
)/sizeof(*tabs
), (LPARAM
) tabs
);
878 switch (LOWORD(wParam
)) {
884 case 101: /* add key */
885 if (HIWORD(wParam
) == BN_CLICKED
||
886 HIWORD(wParam
) == BN_DOUBLECLICKED
) {
887 prompt_add_keyfile();
890 case 102: /* remove key */
891 if (HIWORD(wParam
) == BN_CLICKED
||
892 HIWORD(wParam
) == BN_DOUBLECLICKED
) {
893 int n
= SendDlgItemMessage (hwnd
, 100, LB_GETCURSEL
, 0, 0);
898 for (rkey
= first234(rsakeys
, &e
); rkey
; rkey
= next234(&e
))
902 del234(rsakeys
, rkey
);
906 for (skey
= first234(ssh2keys
, &e
); skey
; skey
= next234(&e
))
910 del234(ssh2keys
, skey
);
911 skey
->alg
->freekey(skey
->data
);
928 static LRESULT CALLBACK
WndProc (HWND hwnd
, UINT message
,
929 WPARAM wParam
, LPARAM lParam
) {
931 static int menuinprogress
;
935 if (lParam
== WM_RBUTTONUP
) {
937 GetCursorPos(&cursorpos
);
938 PostMessage(hwnd
, WM_SYSTRAY2
, cursorpos
.x
, cursorpos
.y
);
939 } else if (lParam
== WM_LBUTTONDBLCLK
) {
940 /* Equivalent to IDM_VIEWKEYS. */
941 PostMessage(hwnd
, WM_COMMAND
, IDM_VIEWKEYS
, 0);
945 if (!menuinprogress
) {
947 SetForegroundWindow(hwnd
);
948 ret
= TrackPopupMenu(systray_menu
,
949 TPM_RIGHTALIGN
| TPM_BOTTOMALIGN
|
951 wParam
, lParam
, 0, hwnd
, NULL
);
957 switch (wParam
& ~0xF) { /* low 4 bits reserved to Windows */
959 SendMessage(hwnd
, WM_CLOSE
, 0, 0);
963 keylist
= CreateDialog (instance
, MAKEINTRESOURCE(211),
965 ShowWindow (keylist
, SW_SHOWNORMAL
);
967 * Sometimes the window comes up minimised / hidden
968 * for no obvious reason. Prevent this.
970 SetForegroundWindow(keylist
);
971 SetWindowPos (keylist
, HWND_TOP
, 0, 0, 0, 0,
972 SWP_NOMOVE
| SWP_NOSIZE
| SWP_SHOWWINDOW
);
976 prompt_add_keyfile();
980 aboutbox
= CreateDialog (instance
, MAKEINTRESOURCE(213),
982 ShowWindow (aboutbox
, SW_SHOWNORMAL
);
984 * Sometimes the window comes up minimised / hidden
985 * for no obvious reason. Prevent this.
987 SetForegroundWindow(aboutbox
);
988 SetWindowPos (aboutbox
, HWND_TOP
, 0, 0, 0, 0,
989 SWP_NOMOVE
| SWP_NOSIZE
| SWP_SHOWWINDOW
);
1002 HANDLE filemap
, proc
;
1003 PSID mapowner
, procowner
;
1004 PSECURITY_DESCRIPTOR psd1
= NULL
, psd2
= NULL
;
1007 cds
= (COPYDATASTRUCT
*)lParam
;
1008 if (cds
->dwData
!= AGENT_COPYDATA_ID
)
1009 return 0; /* not our message, mate */
1010 mapname
= (char *)cds
->lpData
;
1011 if (mapname
[cds
->cbData
- 1] != '\0')
1012 return 0; /* failure to be ASCIZ! */
1014 debug(("mapname is :%s:\r\n", mapname
));
1016 filemap
= OpenFileMapping(FILE_MAP_ALL_ACCESS
, FALSE
, mapname
);
1018 debug(("filemap is %p\r\n", filemap
));
1020 if (filemap
!= NULL
&& filemap
!= INVALID_HANDLE_VALUE
) {
1024 if ((proc
= OpenProcess(MAXIMUM_ALLOWED
, FALSE
,
1025 GetCurrentProcessId())) == NULL
) {
1027 debug(("couldn't get handle for process\r\n"));
1031 if (getsecurityinfo(proc
, SE_KERNEL_OBJECT
,
1032 OWNER_SECURITY_INFORMATION
,
1033 &procowner
, NULL
, NULL
, NULL
,
1034 &psd2
) != ERROR_SUCCESS
) {
1036 debug(("couldn't get owner info for process\r\n"));
1039 return 0; /* unable to get security info */
1042 if ((rc
= getsecurityinfo(filemap
, SE_KERNEL_OBJECT
,
1043 OWNER_SECURITY_INFORMATION
,
1044 &mapowner
, NULL
, NULL
, NULL
,
1045 &psd1
) != ERROR_SUCCESS
)) {
1047 debug(("couldn't get owner info for filemap: %d\r\n", rc
));
1052 debug(("got security stuff\r\n"));
1054 if (!EqualSid(mapowner
, procowner
))
1055 return 0; /* security ID mismatch! */
1057 debug(("security stuff matched\r\n"));
1063 debug(("security APIs not present\r\n"));
1067 p
= MapViewOfFile(filemap
, FILE_MAP_WRITE
, 0, 0, 0);
1069 debug(("p is %p\r\n", p
));
1070 {int i
; for(i
=0;i
<5;i
++)debug(("p[%d]=%02x\r\n", i
, ((unsigned char *)p
)[i
]));}
1076 CloseHandle(filemap
);
1081 return DefWindowProc (hwnd
, message
, wParam
, lParam
);
1084 int WINAPI
WinMain(HINSTANCE inst
, HINSTANCE prev
, LPSTR cmdline
, int show
) {
1091 * Determine whether we're an NT system (should have security
1092 * APIs) or a non-NT system (don't do security).
1094 memset(&osi
, 0, sizeof(OSVERSIONINFO
));
1095 osi
.dwOSVersionInfoSize
= sizeof(OSVERSIONINFO
);
1096 if (GetVersionEx(&osi
) && osi
.dwPlatformId
==VER_PLATFORM_WIN32_NT
) {
1097 has_security
= TRUE
;
1099 has_security
= FALSE
;
1104 * Attempt to ge the security API we need.
1106 advapi
= LoadLibrary("ADVAPI32.DLL");
1107 getsecurityinfo
= (gsi_fn_t
)GetProcAddress(advapi
, "GetSecurityInfo");
1108 if (!getsecurityinfo
) {
1110 "Unable to access security APIs. Pageant will\n"
1111 "not run, in case it causes a security breach.",
1112 "Pageant Fatal Error", MB_ICONERROR
| MB_OK
);
1117 "This program has been compiled for Win9X and will\n"
1118 "not run on NT, in case it causes a security breach.",
1119 "Pageant Fatal Error", MB_ICONERROR
| MB_OK
);
1126 * First bomb out totally if we are already running.
1128 if (FindWindow("Pageant", "Pageant")) {
1129 MessageBox(NULL
, "Pageant is already running", "Pageant Error",
1130 MB_ICONERROR
| MB_OK
);
1131 if (advapi
) FreeLibrary(advapi
);
1139 wndclass
.lpfnWndProc
= WndProc
;
1140 wndclass
.cbClsExtra
= 0;
1141 wndclass
.cbWndExtra
= 0;
1142 wndclass
.hInstance
= inst
;
1143 wndclass
.hIcon
= LoadIcon (inst
,
1144 MAKEINTRESOURCE(IDI_MAINICON
));
1145 wndclass
.hCursor
= LoadCursor (NULL
, IDC_IBEAM
);
1146 wndclass
.hbrBackground
= GetStockObject (BLACK_BRUSH
);
1147 wndclass
.lpszMenuName
= NULL
;
1148 wndclass
.lpszClassName
= APPNAME
;
1150 RegisterClass (&wndclass
);
1153 hwnd
= keylist
= NULL
;
1155 hwnd
= CreateWindow (APPNAME
, APPNAME
,
1156 WS_OVERLAPPEDWINDOW
| WS_VSCROLL
,
1157 CW_USEDEFAULT
, CW_USEDEFAULT
,
1158 100, 100, NULL
, NULL
, inst
, NULL
);
1160 /* Set up a system tray icon */
1163 NOTIFYICONDATA tnid
;
1166 #ifdef NIM_SETVERSION
1168 res
= Shell_NotifyIcon(NIM_SETVERSION
, &tnid
);
1171 tnid
.cbSize
= sizeof(NOTIFYICONDATA
);
1173 tnid
.uID
= 1; /* unique within this systray use */
1174 tnid
.uFlags
= NIF_MESSAGE
| NIF_ICON
| NIF_TIP
;
1175 tnid
.uCallbackMessage
= WM_SYSTRAY
;
1176 tnid
.hIcon
= hicon
= LoadIcon (instance
, MAKEINTRESOURCE(201));
1177 strcpy(tnid
.szTip
, "Pageant (PuTTY authentication agent)");
1179 res
= Shell_NotifyIcon(NIM_ADD
, &tnid
);
1184 systray_menu
= CreatePopupMenu();
1185 /* accelerators used: vkxa */
1186 AppendMenu (systray_menu
, MF_ENABLED
, IDM_VIEWKEYS
, "&View Keys");
1187 AppendMenu (systray_menu
, MF_ENABLED
, IDM_ADDKEY
, "Add &Key");
1188 AppendMenu (systray_menu
, MF_ENABLED
, IDM_ABOUT
, "&About");
1189 AppendMenu (systray_menu
, MF_ENABLED
, IDM_CLOSE
, "E&xit");
1192 ShowWindow (hwnd
, SW_HIDE
);
1195 * Initialise storage for RSA keys.
1197 rsakeys
= newtree234(cmpkeys_rsa
);
1198 ssh2keys
= newtree234(cmpkeys_ssh2
);
1201 * Process the command line and add keys as listed on it.
1208 while (*p
&& isspace(*p
)) p
++;
1209 if (*p
&& !isspace(*p
)) {
1210 char *q
= p
, *pp
= p
;
1211 while (*p
&& (inquotes
|| !isspace(*p
)))
1214 inquotes
= !inquotes
;
1230 * Main message loop.
1232 while (GetMessage(&msg
, NULL
, 0, 0) == 1) {
1233 TranslateMessage(&msg
);
1234 DispatchMessage(&msg
);
1237 /* Clean up the system tray icon */
1239 NOTIFYICONDATA tnid
;
1241 tnid
.cbSize
= sizeof(NOTIFYICONDATA
);
1245 Shell_NotifyIcon(NIM_DELETE
, &tnid
);
1247 DestroyMenu(systray_menu
);
1250 if (advapi
) FreeLibrary(advapi
);