2 * Pageant: the PuTTY Authentication Agent.
17 #define IDI_MAINICON 200
18 #define IDI_TRAYICON 201
20 #define WM_XUSER (WM_USER + 0x2000)
21 #define WM_SYSTRAY (WM_XUSER + 6)
22 #define WM_SYSTRAY2 (WM_XUSER + 7)
24 #define AGENT_COPYDATA_ID 0x804e50ba /* random goop */
27 * FIXME: maybe some day we can sort this out ...
29 #define AGENT_MAX_MSGLEN 8192
31 #define IDM_CLOSE 0x0010
32 #define IDM_VIEWKEYS 0x0020
33 #define IDM_ADDKEY 0x0030
34 #define IDM_ABOUT 0x0040
36 #define APPNAME "Pageant"
40 static HINSTANCE instance
;
44 static HMENU systray_menu
;
45 static int already_running
;
47 static tree234
*rsakeys
, *ssh2keys
;
49 static int has_security
;
51 typedef DWORD (WINAPI
*gsi_fn_t
)
52 (HANDLE
, SE_OBJECT_TYPE
, SECURITY_INFORMATION
,
53 PSID
*, PSID
*, PACL
*, PACL
*,
54 PSECURITY_DESCRIPTOR
*);
55 static gsi_fn_t getsecurityinfo
;
59 * Exports from pageantc.c
61 void agent_query(void *in
, int inlen
, void **out
, int *outlen
);
62 int agent_exists(void);
65 * We need this to link with the RSA code, because rsaencrypt()
66 * pads its data with random bytes. Since we only use rsadecrypt()
67 * and the signing functions, which are deterministic, this should
70 * If it _is_ called, there is a _serious_ problem, because it
71 * won't generate true random numbers. So we must scream, panic,
72 * and exit immediately if that should happen.
74 int random_byte(void) {
75 MessageBox(hwnd
, "Internal Error", APPNAME
, MB_OK
| MB_ICONERROR
);
80 * Blob structure for passing to the asymmetric SSH2 key compare
81 * function, prototyped here.
87 static int cmpkeys_ssh2_asymm(void *av
, void *bv
);
90 * This function is needed to link with the DES code. We need not
91 * have it do anything at all.
93 void logevent(char *msg
) {
96 #define GET_32BIT(cp) \
97 (((unsigned long)(unsigned char)(cp)[0] << 24) | \
98 ((unsigned long)(unsigned char)(cp)[1] << 16) | \
99 ((unsigned long)(unsigned char)(cp)[2] << 8) | \
100 ((unsigned long)(unsigned char)(cp)[3]))
102 #define PUT_32BIT(cp, value) { \
103 (cp)[0] = (unsigned char)((value) >> 24); \
104 (cp)[1] = (unsigned char)((value) >> 16); \
105 (cp)[2] = (unsigned char)((value) >> 8); \
106 (cp)[3] = (unsigned char)(value); }
108 #define PASSPHRASE_MAXLEN 512
110 struct PassphraseProcStruct
{
116 * Dialog-box function for the Licence box.
118 static int CALLBACK
LicenceProc (HWND hwnd
, UINT msg
,
119 WPARAM wParam
, LPARAM lParam
) {
124 switch (LOWORD(wParam
)) {
138 * Dialog-box function for the About box.
140 static int CALLBACK
AboutProc (HWND hwnd
, UINT msg
,
141 WPARAM wParam
, LPARAM lParam
) {
144 SetDlgItemText (hwnd
, 100, ver
);
147 switch (LOWORD(wParam
)) {
150 DestroyWindow (hwnd
);
153 EnableWindow(hwnd
, 0);
154 DialogBox (instance
, MAKEINTRESOURCE(214), NULL
, LicenceProc
);
155 EnableWindow(hwnd
, 1);
156 SetActiveWindow(hwnd
);
162 DestroyWindow (hwnd
);
169 * Dialog-box function for the passphrase box.
171 static int CALLBACK
PassphraseProc(HWND hwnd
, UINT msg
,
172 WPARAM wParam
, LPARAM lParam
) {
173 static char *passphrase
= NULL
;
174 struct PassphraseProcStruct
*p
;
181 { /* centre the window */
185 hw
= GetDesktopWindow();
186 if (GetWindowRect (hw
, &rs
) && GetWindowRect (hwnd
, &rd
))
187 MoveWindow (hwnd
, (rs
.right
+ rs
.left
+ rd
.left
- rd
.right
)/2,
188 (rs
.bottom
+ rs
.top
+ rd
.top
- rd
.bottom
)/2,
189 rd
.right
-rd
.left
, rd
.bottom
-rd
.top
, TRUE
);
192 SetForegroundWindow(hwnd
);
193 SetWindowPos (hwnd
, HWND_TOP
, 0, 0, 0, 0,
194 SWP_NOMOVE
| SWP_NOSIZE
| SWP_SHOWWINDOW
);
195 p
= (struct PassphraseProcStruct
*)lParam
;
196 passphrase
= p
->passphrase
;
198 SetDlgItemText(hwnd
, 101, p
->comment
);
200 SetDlgItemText (hwnd
, 102, passphrase
);
203 switch (LOWORD(wParam
)) {
213 case 102: /* edit box */
214 if ((HIWORD(wParam
) == EN_CHANGE
) && passphrase
) {
215 GetDlgItemText (hwnd
, 102, passphrase
, PASSPHRASE_MAXLEN
-1);
216 passphrase
[PASSPHRASE_MAXLEN
-1] = '\0';
229 * Update the visible key list.
231 static void keylist_update(void) {
233 struct ssh2_userkey
*skey
;
237 SendDlgItemMessage(keylist
, 100, LB_RESETCONTENT
, 0, 0);
238 for (i
= 0; NULL
!= (rkey
= index234(rsakeys
, i
)); i
++) {
239 char listentry
[512], *p
;
241 * Replace two spaces in the fingerprint with tabs, for
242 * nice alignment in the box.
244 strcpy(listentry
, "ssh1\t");
245 p
= listentry
+strlen(listentry
);
246 rsa_fingerprint(p
, sizeof(listentry
)-(p
-listentry
), rkey
);
247 p
= strchr(listentry
, ' '); if (p
) *p
= '\t';
248 p
= strchr(listentry
, ' '); if (p
) *p
= '\t';
249 SendDlgItemMessage (keylist
, 100, LB_ADDSTRING
,
250 0, (LPARAM
)listentry
);
252 for (i
= 0; NULL
!= (skey
= index234(ssh2keys
, i
)); i
++) {
253 char listentry
[512], *p
;
256 * Replace two spaces in the fingerprint with tabs, for
257 * nice alignment in the box.
259 p
= skey
->alg
->fingerprint(skey
->data
);
260 strncpy(listentry
, p
, sizeof(listentry
));
261 p
= strchr(listentry
, ' '); if (p
) *p
= '\t';
262 p
= strchr(listentry
, ' '); if (p
) *p
= '\t';
263 len
= strlen(listentry
);
264 if (len
< sizeof(listentry
)-2) {
265 listentry
[len
] = '\t';
266 strncpy(listentry
+len
+1, skey
->comment
, sizeof(listentry
)-len
-1);
268 SendDlgItemMessage (keylist
, 100, LB_ADDSTRING
,
269 0, (LPARAM
)listentry
);
271 SendDlgItemMessage (keylist
, 100, LB_SETCURSEL
, (WPARAM
) -1, 0);
276 * This function loads a key from a file and adds it.
278 static void add_keyfile(char *filename
) {
279 char passphrase
[PASSPHRASE_MAXLEN
];
281 struct ssh2_userkey
*skey
;
286 struct PassphraseProcStruct pps
;
289 ver
= keyfile_version(filename
);
291 MessageBox(NULL
, "Couldn't load private key.", APPNAME
,
292 MB_OK
| MB_ICONERROR
);
297 needs_pass
= rsakey_encrypted(filename
, &comment
);
299 needs_pass
= ssh2_userkey_encrypted(filename
, &comment
);
302 rkey
= smalloc(sizeof(*rkey
));
303 pps
.passphrase
= passphrase
;
304 pps
.comment
= comment
;
308 dlgret
= DialogBoxParam(instance
, MAKEINTRESOURCE(210),
309 NULL
, PassphraseProc
,
312 if (comment
) sfree(comment
);
315 return; /* operation cancelled */
320 ret
= loadrsakey(filename
, rkey
, passphrase
);
322 skey
= ssh2_load_userkey(filename
, passphrase
);
323 if (skey
== SSH2_WRONG_PASSPHRASE
)
332 if (comment
) sfree(comment
);
334 MessageBox(NULL
, "Couldn't load private key.", APPNAME
,
335 MB_OK
| MB_ICONERROR
);
341 if (already_running
) {
342 unsigned char *request
, *response
;
343 int reqlen
, clen
, resplen
;
345 clen
= strlen(rkey
->comment
);
347 reqlen
= 4 + 1 + /* length, message type */
349 ssh1_bignum_length(rkey
->modulus
) +
350 ssh1_bignum_length(rkey
->exponent
) +
351 ssh1_bignum_length(rkey
->private_exponent
) +
352 ssh1_bignum_length(rkey
->iqmp
) +
353 ssh1_bignum_length(rkey
->p
) +
354 ssh1_bignum_length(rkey
->q
) +
355 4 + clen
/* comment */
358 request
= smalloc(reqlen
);
360 request
[4] = SSH1_AGENTC_ADD_RSA_IDENTITY
;
362 PUT_32BIT(request
+reqlen
, bignum_bitcount(rkey
->modulus
));
364 reqlen
+= ssh1_write_bignum(request
+reqlen
, rkey
->modulus
);
365 reqlen
+= ssh1_write_bignum(request
+reqlen
, rkey
->exponent
);
366 reqlen
+= ssh1_write_bignum(request
+reqlen
, rkey
->private_exponent
);
367 reqlen
+= ssh1_write_bignum(request
+reqlen
, rkey
->iqmp
);
368 reqlen
+= ssh1_write_bignum(request
+reqlen
, rkey
->p
);
369 reqlen
+= ssh1_write_bignum(request
+reqlen
, rkey
->q
);
370 PUT_32BIT(request
+reqlen
, clen
);
371 memcpy(request
+reqlen
+4, rkey
->comment
, clen
);
373 PUT_32BIT(request
, reqlen
-4);
375 agent_query(request
, reqlen
, &response
, &resplen
);
376 if (resplen
< 5 || response
[4] != SSH_AGENT_SUCCESS
)
377 MessageBox(NULL
, "The already running Pageant "
378 "refused to add the key.", APPNAME
,
379 MB_OK
| MB_ICONERROR
);
381 if (add234(rsakeys
, rkey
) != rkey
)
382 sfree(rkey
); /* already present, don't waste RAM */
385 if (already_running
) {
386 unsigned char *request
, *response
;
387 int reqlen
, alglen
, clen
, keybloblen
, resplen
;
388 alglen
= strlen(skey
->alg
->name
);
389 clen
= strlen(skey
->comment
);
391 keybloblen
= skey
->alg
->openssh_fmtkey(skey
->data
, NULL
, 0);
393 reqlen
= 4 + 1 + /* length, message type */
394 4 + alglen
+ /* algorithm name */
395 keybloblen
+ /* key data */
396 4 + clen
/* comment */
399 request
= smalloc(reqlen
);
401 request
[4] = SSH2_AGENTC_ADD_IDENTITY
;
403 PUT_32BIT(request
+reqlen
, alglen
);
405 memcpy(request
+reqlen
, skey
->alg
->name
, alglen
);
407 reqlen
+= skey
->alg
->openssh_fmtkey(skey
->data
,
408 request
+reqlen
, keybloblen
);
409 PUT_32BIT(request
+reqlen
, clen
);
410 memcpy(request
+reqlen
+4, skey
->comment
, clen
);
411 PUT_32BIT(request
, reqlen
-4);
414 agent_query(request
, reqlen
, &response
, &resplen
);
415 if (resplen
< 5 || response
[4] != SSH_AGENT_SUCCESS
)
416 MessageBox(NULL
, "The already running Pageant"
417 "refused to add the key.", APPNAME
,
418 MB_OK
| MB_ICONERROR
);
420 if (add234(ssh2keys
, skey
) != skey
) {
421 skey
->alg
->freekey(skey
->data
);
422 sfree(skey
); /* already present, don't waste RAM */
429 * This is the main agent function that answers messages.
431 static void answer_msg(void *msg
) {
432 unsigned char *p
= msg
;
433 unsigned char *ret
= msg
;
437 * Get the message type.
443 case SSH1_AGENTC_REQUEST_RSA_IDENTITIES
:
445 * Reply with SSH1_AGENT_RSA_IDENTITIES_ANSWER.
453 * Count up the number and length of keys we hold.
456 for (i
= 0; NULL
!= (key
= index234(rsakeys
, i
)); i
++) {
458 len
+= 4; /* length field */
459 len
+= ssh1_bignum_length(key
->exponent
);
460 len
+= ssh1_bignum_length(key
->modulus
);
461 len
+= 4 + strlen(key
->comment
);
465 * Packet header is the obvious five bytes, plus four
466 * bytes for the key count.
469 if (len
> AGENT_MAX_MSGLEN
)
470 goto failure
; /* aaargh! too much stuff! */
471 PUT_32BIT(ret
, len
-4);
472 ret
[4] = SSH1_AGENT_RSA_IDENTITIES_ANSWER
;
473 PUT_32BIT(ret
+5, nkeys
);
475 for (i
= 0; NULL
!= (key
= index234(rsakeys
, i
)); i
++) {
476 PUT_32BIT(p
, bignum_bitcount(key
->modulus
));
478 p
+= ssh1_write_bignum(p
, key
->exponent
);
479 p
+= ssh1_write_bignum(p
, key
->modulus
);
480 PUT_32BIT(p
, strlen(key
->comment
));
481 memcpy(p
+4, key
->comment
, strlen(key
->comment
));
482 p
+= 4 + strlen(key
->comment
);
486 case SSH2_AGENTC_REQUEST_IDENTITIES
:
488 * Reply with SSH2_AGENT_IDENTITIES_ANSWER.
491 struct ssh2_userkey
*key
;
498 * Count up the number and length of keys we hold.
501 for (i
= 0; NULL
!= (key
= index234(ssh2keys
, i
)); i
++) {
503 len
+= 4; /* length field */
504 blob
= key
->alg
->public_blob(key
->data
, &bloblen
);
507 len
+= 4 + strlen(key
->comment
);
511 * Packet header is the obvious five bytes, plus four
512 * bytes for the key count.
515 if (len
> AGENT_MAX_MSGLEN
)
516 goto failure
; /* aaargh! too much stuff! */
517 PUT_32BIT(ret
, len
-4);
518 ret
[4] = SSH2_AGENT_IDENTITIES_ANSWER
;
519 PUT_32BIT(ret
+5, nkeys
);
521 for (i
= 0; NULL
!= (key
= index234(ssh2keys
, i
)); i
++) {
522 blob
= key
->alg
->public_blob(key
->data
, &bloblen
);
523 PUT_32BIT(p
, bloblen
);
525 memcpy(p
, blob
, bloblen
);
528 PUT_32BIT(p
, strlen(key
->comment
));
529 memcpy(p
+4, key
->comment
, strlen(key
->comment
));
530 p
+= 4 + strlen(key
->comment
);
534 case SSH1_AGENTC_RSA_CHALLENGE
:
536 * Reply with either SSH1_AGENT_RSA_RESPONSE or
537 * SSH_AGENT_FAILURE, depending on whether we have that key
541 struct RSAKey reqkey
, *key
;
542 Bignum challenge
, response
;
543 unsigned char response_source
[48], response_md5
[16];
544 struct MD5Context md5c
;
548 p
+= ssh1_read_bignum(p
, &reqkey
.exponent
);
549 p
+= ssh1_read_bignum(p
, &reqkey
.modulus
);
550 p
+= ssh1_read_bignum(p
, &challenge
);
551 memcpy(response_source
+32, p
, 16); p
+= 16;
552 if (GET_32BIT(p
) != 1 ||
553 (key
= find234(rsakeys
, &reqkey
, NULL
)) == NULL
) {
554 freebn(reqkey
.exponent
);
555 freebn(reqkey
.modulus
);
559 response
= rsadecrypt(challenge
, key
);
560 for (i
= 0; i
< 32; i
++)
561 response_source
[i
] = bignum_byte(response
, 31-i
);
564 MD5Update(&md5c
, response_source
, 48);
565 MD5Final(response_md5
, &md5c
);
566 memset(response_source
, 0, 48); /* burn the evidence */
567 freebn(response
); /* and that evidence */
568 freebn(challenge
); /* yes, and that evidence */
569 freebn(reqkey
.exponent
); /* and free some memory ... */
570 freebn(reqkey
.modulus
); /* ... while we're at it. */
573 * Packet is the obvious five byte header, plus sixteen
577 PUT_32BIT(ret
, len
-4);
578 ret
[4] = SSH1_AGENT_RSA_RESPONSE
;
579 memcpy(ret
+5, response_md5
, 16);
582 case SSH2_AGENTC_SIGN_REQUEST
:
584 * Reply with either SSH2_AGENT_RSA_RESPONSE or
585 * SSH_AGENT_FAILURE, depending on whether we have that key
589 struct ssh2_userkey
*key
;
591 unsigned char *data
, *signature
;
592 int datalen
, siglen
, len
;
594 b
.len
= GET_32BIT(p
);
598 datalen
= GET_32BIT(p
);
601 key
= find234(ssh2keys
, &b
, cmpkeys_ssh2_asymm
);
604 signature
= key
->alg
->sign(key
->data
, data
, datalen
, &siglen
);
606 PUT_32BIT(ret
, len
-4);
607 ret
[4] = SSH2_AGENT_SIGN_RESPONSE
;
608 PUT_32BIT(ret
+5, siglen
);
609 memcpy(ret
+5+4, signature
, siglen
);
613 case SSH1_AGENTC_ADD_RSA_IDENTITY
:
615 * Add to the list and return SSH_AGENT_SUCCESS, or
616 * SSH_AGENT_FAILURE if the key was malformed.
621 key
= smalloc(sizeof(struct RSAKey
));
622 memset(key
, 0, sizeof(key
));
623 p
+= makekey(p
, key
, NULL
, 1);
624 p
+= makeprivate(p
, key
);
625 p
+= ssh1_read_bignum(p
, key
->iqmp
); /* p^-1 mod q */
626 p
+= ssh1_read_bignum(p
, key
->p
); /* p */
627 p
+= ssh1_read_bignum(p
, key
->q
); /* q */
628 comment
= smalloc(GET_32BIT(p
));
630 memcpy(comment
, p
+4, GET_32BIT(p
));
631 key
->comment
= comment
;
634 ret
[4] = SSH_AGENT_FAILURE
;
635 if (add234(rsakeys
, key
) == key
) {
637 ret
[4] = SSH_AGENT_SUCCESS
;
644 case SSH2_AGENTC_ADD_IDENTITY
:
646 * Add to the list and return SSH_AGENT_SUCCESS, or
647 * SSH_AGENT_FAILURE if the key was malformed.
650 struct ssh2_userkey
*key
;
655 key
= smalloc(sizeof(struct ssh2_userkey
));
657 alglen
= GET_32BIT(p
); p
+= 4;
658 alg
= p
; p
+= alglen
;
659 /* Add further algorithm names here. */
660 if (alglen
== 7 && !memcmp(alg
, "ssh-rsa", 7))
667 bloblen
= GET_32BIT((unsigned char *)msg
) - (p
-(unsigned char *)msg
-4);
668 key
->data
= key
->alg
->openssh_createkey(&p
, &bloblen
);
673 commlen
= GET_32BIT(p
); p
+= 4;
675 comment
= smalloc(commlen
+1);
677 memcpy(comment
, p
, commlen
);
678 comment
[commlen
] = '\0';
680 key
->comment
= comment
;
683 ret
[4] = SSH_AGENT_FAILURE
;
684 if (add234(ssh2keys
, key
) == key
) {
686 ret
[4] = SSH_AGENT_SUCCESS
;
688 key
->alg
->freekey(key
->data
);
694 case SSH1_AGENTC_REMOVE_RSA_IDENTITY
:
696 * Remove from the list and return SSH_AGENT_SUCCESS, or
697 * perhaps SSH_AGENT_FAILURE if it wasn't in the list to
701 struct RSAKey reqkey
, *key
;
703 p
+= makekey(p
, &reqkey
, NULL
, 0);
704 key
= find234(rsakeys
, &reqkey
, NULL
);
705 freebn(reqkey
.exponent
);
706 freebn(reqkey
.modulus
);
708 ret
[4] = SSH_AGENT_FAILURE
;
710 del234(rsakeys
, key
);
714 ret
[4] = SSH_AGENT_SUCCESS
;
718 case SSH2_AGENTC_REMOVE_IDENTITY
:
720 * Remove from the list and return SSH_AGENT_SUCCESS, or
721 * perhaps SSH_AGENT_FAILURE if it wasn't in the list to
725 struct ssh2_userkey
*key
;
728 b
.len
= GET_32BIT(p
);
732 key
= find234(ssh2keys
, &b
, cmpkeys_ssh2_asymm
);
737 ret
[4] = SSH_AGENT_FAILURE
;
739 del234(ssh2keys
, key
);
741 key
->alg
->freekey(key
->data
);
743 ret
[4] = SSH_AGENT_SUCCESS
;
747 case SSH1_AGENTC_REMOVE_ALL_RSA_IDENTITIES
:
749 * Remove all SSH1 keys. Always returns success.
754 while ( (rkey
= index234(rsakeys
, 0)) != NULL
) {
755 del234(rsakeys
, rkey
);
762 ret
[4] = SSH_AGENT_SUCCESS
;
765 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES
:
767 * Remove all SSH2 keys. Always returns success.
770 struct ssh2_userkey
*skey
;
772 while ( (skey
= index234(ssh2keys
, 0)) != NULL
) {
773 del234(ssh2keys
, skey
);
774 skey
->alg
->freekey(skey
->data
);
780 ret
[4] = SSH_AGENT_SUCCESS
;
786 * Unrecognised message. Return SSH_AGENT_FAILURE.
789 ret
[4] = SSH_AGENT_FAILURE
;
795 * Key comparison function for the 2-3-4 tree of RSA keys.
797 static int cmpkeys_rsa(void *av
, void *bv
) {
798 struct RSAKey
*a
= (struct RSAKey
*)av
;
799 struct RSAKey
*b
= (struct RSAKey
*)bv
;
806 * Compare by length of moduli.
808 alen
= bignum_bitcount(am
);
809 blen
= bignum_bitcount(bm
);
810 if (alen
> blen
) return +1; else if (alen
< blen
) return -1;
812 * Now compare by moduli themselves.
814 alen
= (alen
+ 7) / 8; /* byte count */
817 abyte
= bignum_byte(am
, alen
);
818 bbyte
= bignum_byte(bm
, alen
);
819 if (abyte
> bbyte
) return +1; else if (abyte
< bbyte
) return -1;
828 * Key comparison function for the 2-3-4 tree of SSH2 keys.
830 static int cmpkeys_ssh2(void *av
, void *bv
) {
831 struct ssh2_userkey
*a
= (struct ssh2_userkey
*)av
;
832 struct ssh2_userkey
*b
= (struct ssh2_userkey
*)bv
;
835 unsigned char *ablob
, *bblob
;
839 * Compare purely by public blob.
841 ablob
= a
->alg
->public_blob(a
->data
, &alen
);
842 bblob
= b
->alg
->public_blob(b
->data
, &blen
);
845 for (i
= 0; i
< alen
&& i
< blen
; i
++) {
846 if (ablob
[i
] < bblob
[i
]) {
848 } else if (ablob
[i
] > bblob
[i
]) {
852 if (c
== 0 && i
< alen
) c
= +1; /* a is longer */
853 if (c
== 0 && i
< blen
) c
= -1; /* a is longer */
862 * Key comparison function for looking up a blob in the 2-3-4 tree
865 static int cmpkeys_ssh2_asymm(void *av
, void *bv
) {
866 struct blob
*a
= (struct blob
*)av
;
867 struct ssh2_userkey
*b
= (struct ssh2_userkey
*)bv
;
870 unsigned char *ablob
, *bblob
;
874 * Compare purely by public blob.
878 bblob
= b
->alg
->public_blob(b
->data
, &blen
);
881 for (i
= 0; i
< alen
&& i
< blen
; i
++) {
882 if (ablob
[i
] < bblob
[i
]) {
884 } else if (ablob
[i
] > bblob
[i
]) {
888 if (c
== 0 && i
< alen
) c
= +1; /* a is longer */
889 if (c
== 0 && i
< blen
) c
= -1; /* a is longer */
896 static void error(char *s
) {
897 MessageBox(hwnd
, s
, APPNAME
, MB_OK
| MB_ICONERROR
);
901 * Prompt for a key file to add, and add it.
903 static void prompt_add_keyfile(void) {
905 char filename
[FILENAME_MAX
];
906 memset(&of
, 0, sizeof(of
));
907 #ifdef OPENFILENAME_SIZE_VERSION_400
908 of
.lStructSize
= OPENFILENAME_SIZE_VERSION_400
;
910 of
.lStructSize
= sizeof(of
);
913 of
.lpstrFilter
= "All Files\0*\0\0\0";
914 of
.lpstrCustomFilter
= NULL
;
916 of
.lpstrFile
= filename
; *filename
= '\0';
917 of
.nMaxFile
= sizeof(filename
);
918 of
.lpstrFileTitle
= NULL
;
919 of
.lpstrInitialDir
= NULL
;
920 of
.lpstrTitle
= "Select Private Key File";
922 if (GetOpenFileName(&of
)) {
923 add_keyfile(filename
);
929 * Dialog-box function for the key list box.
931 static int CALLBACK
KeyListProc(HWND hwnd
, UINT msg
,
932 WPARAM wParam
, LPARAM lParam
) {
934 struct ssh2_userkey
*skey
;
941 { /* centre the window */
945 hw
= GetDesktopWindow();
946 if (GetWindowRect (hw
, &rs
) && GetWindowRect (hwnd
, &rd
))
947 MoveWindow (hwnd
, (rs
.right
+ rs
.left
+ rd
.left
- rd
.right
)/2,
948 (rs
.bottom
+ rs
.top
+ rd
.top
- rd
.bottom
)/2,
949 rd
.right
-rd
.left
, rd
.bottom
-rd
.top
, TRUE
);
954 static int tabs
[] = {35, 60, 210};
955 SendDlgItemMessage (hwnd
, 100, LB_SETTABSTOPS
,
956 sizeof(tabs
)/sizeof(*tabs
), (LPARAM
) tabs
);
961 switch (LOWORD(wParam
)) {
967 case 101: /* add key */
968 if (HIWORD(wParam
) == BN_CLICKED
||
969 HIWORD(wParam
) == BN_DOUBLECLICKED
) {
970 prompt_add_keyfile();
973 case 102: /* remove key */
974 if (HIWORD(wParam
) == BN_CLICKED
||
975 HIWORD(wParam
) == BN_DOUBLECLICKED
) {
976 int n
= SendDlgItemMessage (hwnd
, 100, LB_GETCURSEL
, 0, 0);
982 for (i
= 0; NULL
!= (rkey
= index234(rsakeys
, i
)); i
++)
986 del234(rsakeys
, rkey
);
990 for (i
= 0; NULL
!= (skey
= index234(ssh2keys
, i
)); i
++)
994 del234(ssh2keys
, skey
);
995 skey
->alg
->freekey(skey
->data
);
1006 DestroyWindow(hwnd
);
1012 static LRESULT CALLBACK
WndProc (HWND hwnd
, UINT message
,
1013 WPARAM wParam
, LPARAM lParam
) {
1015 static int menuinprogress
;
1019 if (lParam
== WM_RBUTTONUP
) {
1021 GetCursorPos(&cursorpos
);
1022 PostMessage(hwnd
, WM_SYSTRAY2
, cursorpos
.x
, cursorpos
.y
);
1023 } else if (lParam
== WM_LBUTTONDBLCLK
) {
1024 /* Equivalent to IDM_VIEWKEYS. */
1025 PostMessage(hwnd
, WM_COMMAND
, IDM_VIEWKEYS
, 0);
1029 if (!menuinprogress
) {
1031 SetForegroundWindow(hwnd
);
1032 ret
= TrackPopupMenu(systray_menu
,
1033 TPM_RIGHTALIGN
| TPM_BOTTOMALIGN
|
1035 wParam
, lParam
, 0, hwnd
, NULL
);
1041 switch (wParam
& ~0xF) { /* low 4 bits reserved to Windows */
1043 SendMessage(hwnd
, WM_CLOSE
, 0, 0);
1047 keylist
= CreateDialog (instance
, MAKEINTRESOURCE(211),
1049 ShowWindow (keylist
, SW_SHOWNORMAL
);
1051 * Sometimes the window comes up minimised / hidden
1052 * for no obvious reason. Prevent this.
1054 SetForegroundWindow(keylist
);
1055 SetWindowPos (keylist
, HWND_TOP
, 0, 0, 0, 0,
1056 SWP_NOMOVE
| SWP_NOSIZE
| SWP_SHOWWINDOW
);
1060 prompt_add_keyfile();
1064 aboutbox
= CreateDialog (instance
, MAKEINTRESOURCE(213),
1066 ShowWindow (aboutbox
, SW_SHOWNORMAL
);
1068 * Sometimes the window comes up minimised / hidden
1069 * for no obvious reason. Prevent this.
1071 SetForegroundWindow(aboutbox
);
1072 SetWindowPos (aboutbox
, HWND_TOP
, 0, 0, 0, 0,
1073 SWP_NOMOVE
| SWP_NOSIZE
| SWP_SHOWWINDOW
);
1079 PostQuitMessage (0);
1083 COPYDATASTRUCT
*cds
;
1086 HANDLE filemap
, proc
;
1087 PSID mapowner
, procowner
;
1088 PSECURITY_DESCRIPTOR psd1
= NULL
, psd2
= NULL
;
1091 cds
= (COPYDATASTRUCT
*)lParam
;
1092 if (cds
->dwData
!= AGENT_COPYDATA_ID
)
1093 return 0; /* not our message, mate */
1094 mapname
= (char *)cds
->lpData
;
1095 if (mapname
[cds
->cbData
- 1] != '\0')
1096 return 0; /* failure to be ASCIZ! */
1098 debug(("mapname is :%s:\r\n", mapname
));
1100 filemap
= OpenFileMapping(FILE_MAP_ALL_ACCESS
, FALSE
, mapname
);
1102 debug(("filemap is %p\r\n", filemap
));
1104 if (filemap
!= NULL
&& filemap
!= INVALID_HANDLE_VALUE
) {
1108 if ((proc
= OpenProcess(MAXIMUM_ALLOWED
, FALSE
,
1109 GetCurrentProcessId())) == NULL
) {
1111 debug(("couldn't get handle for process\r\n"));
1115 if (getsecurityinfo(proc
, SE_KERNEL_OBJECT
,
1116 OWNER_SECURITY_INFORMATION
,
1117 &procowner
, NULL
, NULL
, NULL
,
1118 &psd2
) != ERROR_SUCCESS
) {
1120 debug(("couldn't get owner info for process\r\n"));
1123 return 0; /* unable to get security info */
1126 if ((rc
= getsecurityinfo(filemap
, SE_KERNEL_OBJECT
,
1127 OWNER_SECURITY_INFORMATION
,
1128 &mapowner
, NULL
, NULL
, NULL
,
1129 &psd1
) != ERROR_SUCCESS
)) {
1131 debug(("couldn't get owner info for filemap: %d\r\n", rc
));
1136 debug(("got security stuff\r\n"));
1138 if (!EqualSid(mapowner
, procowner
))
1139 return 0; /* security ID mismatch! */
1141 debug(("security stuff matched\r\n"));
1147 debug(("security APIs not present\r\n"));
1151 p
= MapViewOfFile(filemap
, FILE_MAP_WRITE
, 0, 0, 0);
1153 debug(("p is %p\r\n", p
));
1154 {int i
; for(i
=0;i
<5;i
++)debug(("p[%d]=%02x\r\n", i
, ((unsigned char *)p
)[i
]));}
1160 CloseHandle(filemap
);
1165 return DefWindowProc (hwnd
, message
, wParam
, lParam
);
1169 * Fork and Exec the command in cmdline. [DBW]
1171 void spawn_cmd(char *cmdline
, int show
) {
1172 if (ShellExecute(NULL
, _T("open"), cmdline
,
1173 NULL
, NULL
, show
) <= (HINSTANCE
) 32) {
1175 sprintf(sMsg
, _T("Failed to run \"%.100s\", Error: %d"), cmdline
,
1177 MessageBox(NULL
, sMsg
, APPNAME
, MB_OK
| MB_ICONEXCLAMATION
);
1181 int WINAPI
WinMain(HINSTANCE inst
, HINSTANCE prev
, LPSTR cmdline
, int show
) {
1186 char *command
= NULL
;
1190 * Determine whether we're an NT system (should have security
1191 * APIs) or a non-NT system (don't do security).
1193 memset(&osi
, 0, sizeof(OSVERSIONINFO
));
1194 osi
.dwOSVersionInfoSize
= sizeof(OSVERSIONINFO
);
1195 if (GetVersionEx(&osi
) && osi
.dwPlatformId
==VER_PLATFORM_WIN32_NT
) {
1196 has_security
= TRUE
;
1198 has_security
= FALSE
;
1203 * Attempt to get the security API we need.
1205 advapi
= LoadLibrary("ADVAPI32.DLL");
1206 getsecurityinfo
= (gsi_fn_t
)GetProcAddress(advapi
, "GetSecurityInfo");
1207 if (!getsecurityinfo
) {
1209 "Unable to access security APIs. Pageant will\n"
1210 "not run, in case it causes a security breach.",
1211 "Pageant Fatal Error", MB_ICONERROR
| MB_OK
);
1216 "This program has been compiled for Win9X and will\n"
1217 "not run on NT, in case it causes a security breach.",
1218 "Pageant Fatal Error", MB_ICONERROR
| MB_OK
);
1227 * Find out if Pageant is already running.
1229 already_running
= FALSE
;
1231 already_running
= TRUE
;
1236 wndclass
.lpfnWndProc
= WndProc
;
1237 wndclass
.cbClsExtra
= 0;
1238 wndclass
.cbWndExtra
= 0;
1239 wndclass
.hInstance
= inst
;
1240 wndclass
.hIcon
= LoadIcon (inst
,
1241 MAKEINTRESOURCE(IDI_MAINICON
));
1242 wndclass
.hCursor
= LoadCursor (NULL
, IDC_IBEAM
);
1243 wndclass
.hbrBackground
= GetStockObject (BLACK_BRUSH
);
1244 wndclass
.lpszMenuName
= NULL
;
1245 wndclass
.lpszClassName
= APPNAME
;
1247 RegisterClass (&wndclass
);
1250 hwnd
= keylist
= NULL
;
1252 hwnd
= CreateWindow (APPNAME
, APPNAME
,
1253 WS_OVERLAPPEDWINDOW
| WS_VSCROLL
,
1254 CW_USEDEFAULT
, CW_USEDEFAULT
,
1255 100, 100, NULL
, NULL
, inst
, NULL
);
1257 /* Set up a system tray icon */
1260 NOTIFYICONDATA tnid
;
1263 #ifdef NIM_SETVERSION
1265 res
= Shell_NotifyIcon(NIM_SETVERSION
, &tnid
);
1268 tnid
.cbSize
= sizeof(NOTIFYICONDATA
);
1270 tnid
.uID
= 1; /* unique within this systray use */
1271 tnid
.uFlags
= NIF_MESSAGE
| NIF_ICON
| NIF_TIP
;
1272 tnid
.uCallbackMessage
= WM_SYSTRAY
;
1273 tnid
.hIcon
= hicon
= LoadIcon (instance
, MAKEINTRESOURCE(201));
1274 strcpy(tnid
.szTip
, "Pageant (PuTTY authentication agent)");
1276 res
= Shell_NotifyIcon(NIM_ADD
, &tnid
);
1281 systray_menu
= CreatePopupMenu();
1282 /* accelerators used: vkxa */
1283 AppendMenu (systray_menu
, MF_ENABLED
, IDM_VIEWKEYS
, "&View Keys");
1284 AppendMenu (systray_menu
, MF_ENABLED
, IDM_ADDKEY
, "Add &Key");
1285 AppendMenu (systray_menu
, MF_ENABLED
, IDM_ABOUT
, "&About");
1286 AppendMenu (systray_menu
, MF_ENABLED
, IDM_CLOSE
, "E&xit");
1289 ShowWindow (hwnd
, SW_HIDE
);
1292 * Initialise storage for RSA keys.
1294 rsakeys
= newtree234(cmpkeys_rsa
);
1295 ssh2keys
= newtree234(cmpkeys_ssh2
);
1300 * Process the command line and add keys as listed on it.
1301 * If we already determined that we need to spawn a program from above we
1302 * need to ignore the first two arguments. [DBW]
1310 while (*p
&& isspace(*p
)) p
++;
1311 if (*p
&& !isspace(*p
)) {
1312 char *q
= p
, *pp
= p
;
1313 while (*p
&& (inquotes
|| !isspace(*p
)))
1316 inquotes
= !inquotes
;
1326 if (!strcmp(q
, "-c")) {
1328 * If we see `-c', then the rest of the
1329 * command line should be treated as a
1330 * command to be spawned.
1332 while (*p
&& isspace(*p
)) p
++;
1343 if (command
) spawn_cmd (command
, show
);
1346 * If Pageant was already running, we leave now. If we haven't
1347 * even taken any auxiliary action (spawned a command or added
1350 if (already_running
) {
1351 if (!command
&& !added_keys
) {
1352 MessageBox(NULL
, "Pageant is already running", "Pageant Error",
1353 MB_ICONERROR
| MB_OK
);
1355 if (advapi
) FreeLibrary(advapi
);
1360 * Main message loop.
1362 while (GetMessage(&msg
, NULL
, 0, 0) == 1) {
1363 TranslateMessage(&msg
);
1364 DispatchMessage(&msg
);
1367 /* Clean up the system tray icon */
1369 NOTIFYICONDATA tnid
;
1371 tnid
.cbSize
= sizeof(NOTIFYICONDATA
);
1375 Shell_NotifyIcon(NIM_DELETE
, &tnid
);
1377 DestroyMenu(systray_menu
);
1380 if (advapi
) FreeLibrary(advapi
);