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
*, PSECURITY_DESCRIPTOR
*);
54 static gsi_fn_t getsecurityinfo
;
58 * Exports from pageantc.c
60 void agent_query(void *in
, int inlen
, void **out
, int *outlen
);
61 int agent_exists(void);
64 * We need this to link with the RSA code, because rsaencrypt()
65 * pads its data with random bytes. Since we only use rsadecrypt()
66 * and the signing functions, which are deterministic, this should
69 * If it _is_ called, there is a _serious_ problem, because it
70 * won't generate true random numbers. So we must scream, panic,
71 * and exit immediately if that should happen.
75 MessageBox(hwnd
, "Internal Error", APPNAME
, MB_OK
| MB_ICONERROR
);
77 /* this line can't be reached but it placates MSVC's warnings :-) */
82 * Blob structure for passing to the asymmetric SSH2 key compare
83 * function, prototyped here.
89 static int cmpkeys_ssh2_asymm(void *av
, void *bv
);
92 * This function is needed to link with the DES code. We need not
93 * have it do anything at all.
95 void logevent(char *msg
)
99 #define GET_32BIT(cp) \
100 (((unsigned long)(unsigned char)(cp)[0] << 24) | \
101 ((unsigned long)(unsigned char)(cp)[1] << 16) | \
102 ((unsigned long)(unsigned char)(cp)[2] << 8) | \
103 ((unsigned long)(unsigned char)(cp)[3]))
105 #define PUT_32BIT(cp, value) { \
106 (cp)[0] = (unsigned char)((value) >> 24); \
107 (cp)[1] = (unsigned char)((value) >> 16); \
108 (cp)[2] = (unsigned char)((value) >> 8); \
109 (cp)[3] = (unsigned char)(value); }
111 #define PASSPHRASE_MAXLEN 512
113 struct PassphraseProcStruct
{
119 * Dialog-box function for the Licence box.
121 static int CALLBACK
LicenceProc(HWND hwnd
, UINT msg
,
122 WPARAM wParam
, LPARAM lParam
)
128 switch (LOWORD(wParam
)) {
142 * Dialog-box function for the About box.
144 static int CALLBACK
AboutProc(HWND hwnd
, UINT msg
,
145 WPARAM wParam
, LPARAM lParam
)
149 SetDlgItemText(hwnd
, 100, ver
);
152 switch (LOWORD(wParam
)) {
158 EnableWindow(hwnd
, 0);
159 DialogBox(instance
, MAKEINTRESOURCE(214), NULL
, LicenceProc
);
160 EnableWindow(hwnd
, 1);
161 SetActiveWindow(hwnd
);
173 static HWND passphrase_box
;
176 * Dialog-box function for the passphrase box.
178 static int CALLBACK
PassphraseProc(HWND hwnd
, UINT msg
,
179 WPARAM wParam
, LPARAM lParam
)
181 static char *passphrase
= NULL
;
182 struct PassphraseProcStruct
*p
;
186 passphrase_box
= hwnd
;
190 { /* centre the window */
194 hw
= GetDesktopWindow();
195 if (GetWindowRect(hw
, &rs
) && GetWindowRect(hwnd
, &rd
))
197 (rs
.right
+ rs
.left
+ rd
.left
- rd
.right
) / 2,
198 (rs
.bottom
+ rs
.top
+ rd
.top
- rd
.bottom
) / 2,
199 rd
.right
- rd
.left
, rd
.bottom
- rd
.top
, TRUE
);
202 SetForegroundWindow(hwnd
);
203 SetWindowPos(hwnd
, HWND_TOP
, 0, 0, 0, 0,
204 SWP_NOMOVE
| SWP_NOSIZE
| SWP_SHOWWINDOW
);
205 p
= (struct PassphraseProcStruct
*) lParam
;
206 passphrase
= p
->passphrase
;
208 SetDlgItemText(hwnd
, 101, p
->comment
);
210 SetDlgItemText(hwnd
, 102, passphrase
);
213 switch (LOWORD(wParam
)) {
223 case 102: /* edit box */
224 if ((HIWORD(wParam
) == EN_CHANGE
) && passphrase
) {
225 GetDlgItemText(hwnd
, 102, passphrase
,
226 PASSPHRASE_MAXLEN
- 1);
227 passphrase
[PASSPHRASE_MAXLEN
- 1] = '\0';
240 * Update the visible key list.
242 static void keylist_update(void)
245 struct ssh2_userkey
*skey
;
249 SendDlgItemMessage(keylist
, 100, LB_RESETCONTENT
, 0, 0);
250 for (i
= 0; NULL
!= (rkey
= index234(rsakeys
, i
)); i
++) {
251 char listentry
[512], *p
;
253 * Replace two spaces in the fingerprint with tabs, for
254 * nice alignment in the box.
256 strcpy(listentry
, "ssh1\t");
257 p
= listentry
+ strlen(listentry
);
258 rsa_fingerprint(p
, sizeof(listentry
) - (p
- listentry
), rkey
);
259 p
= strchr(listentry
, ' ');
262 p
= strchr(listentry
, ' ');
265 SendDlgItemMessage(keylist
, 100, LB_ADDSTRING
,
266 0, (LPARAM
) listentry
);
268 for (i
= 0; NULL
!= (skey
= index234(ssh2keys
, i
)); i
++) {
269 char listentry
[512], *p
;
272 * Replace two spaces in the fingerprint with tabs, for
273 * nice alignment in the box.
275 p
= skey
->alg
->fingerprint(skey
->data
);
276 strncpy(listentry
, p
, sizeof(listentry
));
277 p
= strchr(listentry
, ' ');
280 p
= strchr(listentry
, ' ');
283 len
= strlen(listentry
);
284 if (len
< sizeof(listentry
) - 2) {
285 listentry
[len
] = '\t';
286 strncpy(listentry
+ len
+ 1, skey
->comment
,
287 sizeof(listentry
) - len
- 1);
289 SendDlgItemMessage(keylist
, 100, LB_ADDSTRING
, 0,
292 SendDlgItemMessage(keylist
, 100, LB_SETCURSEL
, (WPARAM
) - 1, 0);
297 * This function loads a key from a file and adds it.
299 static void add_keyfile(char *filename
)
301 char passphrase
[PASSPHRASE_MAXLEN
];
302 struct RSAKey
*rkey
= NULL
;
303 struct ssh2_userkey
*skey
= NULL
;
308 struct PassphraseProcStruct pps
;
311 ver
= keyfile_version(filename
);
313 MessageBox(NULL
, "Couldn't load private key.", APPNAME
,
314 MB_OK
| MB_ICONERROR
);
319 needs_pass
= rsakey_encrypted(filename
, &comment
);
321 needs_pass
= ssh2_userkey_encrypted(filename
, &comment
);
324 rkey
= smalloc(sizeof(*rkey
));
325 pps
.passphrase
= passphrase
;
326 pps
.comment
= comment
;
330 dlgret
= DialogBoxParam(instance
, MAKEINTRESOURCE(210),
331 NULL
, PassphraseProc
, (LPARAM
) & pps
);
332 passphrase_box
= NULL
;
338 return; /* operation cancelled */
343 ret
= loadrsakey(filename
, rkey
, passphrase
);
345 skey
= ssh2_load_userkey(filename
, passphrase
);
346 if (skey
== SSH2_WRONG_PASSPHRASE
)
358 MessageBox(NULL
, "Couldn't load private key.", APPNAME
,
359 MB_OK
| MB_ICONERROR
);
365 if (already_running
) {
366 unsigned char *request
, *response
;
368 int reqlen
, clen
, resplen
;
370 clen
= strlen(rkey
->comment
);
372 reqlen
= 4 + 1 + /* length, message type */
374 ssh1_bignum_length(rkey
->modulus
) +
375 ssh1_bignum_length(rkey
->exponent
) +
376 ssh1_bignum_length(rkey
->private_exponent
) +
377 ssh1_bignum_length(rkey
->iqmp
) +
378 ssh1_bignum_length(rkey
->p
) +
379 ssh1_bignum_length(rkey
->q
) + 4 + clen
/* comment */
382 request
= smalloc(reqlen
);
384 request
[4] = SSH1_AGENTC_ADD_RSA_IDENTITY
;
386 PUT_32BIT(request
+ reqlen
, bignum_bitcount(rkey
->modulus
));
388 reqlen
+= ssh1_write_bignum(request
+ reqlen
, rkey
->modulus
);
389 reqlen
+= ssh1_write_bignum(request
+ reqlen
, rkey
->exponent
);
391 ssh1_write_bignum(request
+ reqlen
,
392 rkey
->private_exponent
);
393 reqlen
+= ssh1_write_bignum(request
+ reqlen
, rkey
->iqmp
);
394 reqlen
+= ssh1_write_bignum(request
+ reqlen
, rkey
->p
);
395 reqlen
+= ssh1_write_bignum(request
+ reqlen
, rkey
->q
);
396 PUT_32BIT(request
+ reqlen
, clen
);
397 memcpy(request
+ reqlen
+ 4, rkey
->comment
, clen
);
399 PUT_32BIT(request
, reqlen
- 4);
401 agent_query(request
, reqlen
, &vresponse
, &resplen
);
402 response
= vresponse
;
403 if (resplen
< 5 || response
[4] != SSH_AGENT_SUCCESS
)
404 MessageBox(NULL
, "The already running Pageant "
405 "refused to add the key.", APPNAME
,
406 MB_OK
| MB_ICONERROR
);
408 if (add234(rsakeys
, rkey
) != rkey
)
409 sfree(rkey
); /* already present, don't waste RAM */
412 if (already_running
) {
413 unsigned char *request
, *response
;
415 int reqlen
, alglen
, clen
, keybloblen
, resplen
;
416 alglen
= strlen(skey
->alg
->name
);
417 clen
= strlen(skey
->comment
);
419 keybloblen
= skey
->alg
->openssh_fmtkey(skey
->data
, NULL
, 0);
421 reqlen
= 4 + 1 + /* length, message type */
422 4 + alglen
+ /* algorithm name */
423 keybloblen
+ /* key data */
424 4 + clen
/* comment */
427 request
= smalloc(reqlen
);
429 request
[4] = SSH2_AGENTC_ADD_IDENTITY
;
431 PUT_32BIT(request
+ reqlen
, alglen
);
433 memcpy(request
+ reqlen
, skey
->alg
->name
, alglen
);
435 reqlen
+= skey
->alg
->openssh_fmtkey(skey
->data
,
438 PUT_32BIT(request
+ reqlen
, clen
);
439 memcpy(request
+ reqlen
+ 4, skey
->comment
, clen
);
440 PUT_32BIT(request
, reqlen
- 4);
443 agent_query(request
, reqlen
, &vresponse
, &resplen
);
444 response
= vresponse
;
445 if (resplen
< 5 || response
[4] != SSH_AGENT_SUCCESS
)
446 MessageBox(NULL
, "The already running Pageant"
447 "refused to add the key.", APPNAME
,
448 MB_OK
| MB_ICONERROR
);
450 if (add234(ssh2keys
, skey
) != skey
) {
451 skey
->alg
->freekey(skey
->data
);
452 sfree(skey
); /* already present, don't waste RAM */
459 * This is the main agent function that answers messages.
461 static void answer_msg(void *msg
)
463 unsigned char *p
= msg
;
464 unsigned char *ret
= msg
;
468 * Get the message type.
474 case SSH1_AGENTC_REQUEST_RSA_IDENTITIES
:
476 * Reply with SSH1_AGENT_RSA_IDENTITIES_ANSWER.
484 * Count up the number and length of keys we hold.
487 for (i
= 0; NULL
!= (key
= index234(rsakeys
, i
)); i
++) {
489 len
+= 4; /* length field */
490 len
+= ssh1_bignum_length(key
->exponent
);
491 len
+= ssh1_bignum_length(key
->modulus
);
492 len
+= 4 + strlen(key
->comment
);
496 * Packet header is the obvious five bytes, plus four
497 * bytes for the key count.
500 if (len
> AGENT_MAX_MSGLEN
)
501 goto failure
; /* aaargh! too much stuff! */
502 PUT_32BIT(ret
, len
- 4);
503 ret
[4] = SSH1_AGENT_RSA_IDENTITIES_ANSWER
;
504 PUT_32BIT(ret
+ 5, nkeys
);
506 for (i
= 0; NULL
!= (key
= index234(rsakeys
, i
)); i
++) {
507 PUT_32BIT(p
, bignum_bitcount(key
->modulus
));
509 p
+= ssh1_write_bignum(p
, key
->exponent
);
510 p
+= ssh1_write_bignum(p
, key
->modulus
);
511 PUT_32BIT(p
, strlen(key
->comment
));
512 memcpy(p
+ 4, key
->comment
, strlen(key
->comment
));
513 p
+= 4 + strlen(key
->comment
);
517 case SSH2_AGENTC_REQUEST_IDENTITIES
:
519 * Reply with SSH2_AGENT_IDENTITIES_ANSWER.
522 struct ssh2_userkey
*key
;
529 * Count up the number and length of keys we hold.
532 for (i
= 0; NULL
!= (key
= index234(ssh2keys
, i
)); i
++) {
534 len
+= 4; /* length field */
535 blob
= key
->alg
->public_blob(key
->data
, &bloblen
);
538 len
+= 4 + strlen(key
->comment
);
542 * Packet header is the obvious five bytes, plus four
543 * bytes for the key count.
546 if (len
> AGENT_MAX_MSGLEN
)
547 goto failure
; /* aaargh! too much stuff! */
548 PUT_32BIT(ret
, len
- 4);
549 ret
[4] = SSH2_AGENT_IDENTITIES_ANSWER
;
550 PUT_32BIT(ret
+ 5, nkeys
);
552 for (i
= 0; NULL
!= (key
= index234(ssh2keys
, i
)); i
++) {
553 blob
= key
->alg
->public_blob(key
->data
, &bloblen
);
554 PUT_32BIT(p
, bloblen
);
556 memcpy(p
, blob
, bloblen
);
559 PUT_32BIT(p
, strlen(key
->comment
));
560 memcpy(p
+ 4, key
->comment
, strlen(key
->comment
));
561 p
+= 4 + strlen(key
->comment
);
565 case SSH1_AGENTC_RSA_CHALLENGE
:
567 * Reply with either SSH1_AGENT_RSA_RESPONSE or
568 * SSH_AGENT_FAILURE, depending on whether we have that key
572 struct RSAKey reqkey
, *key
;
573 Bignum challenge
, response
;
574 unsigned char response_source
[48], response_md5
[16];
575 struct MD5Context md5c
;
579 p
+= ssh1_read_bignum(p
, &reqkey
.exponent
);
580 p
+= ssh1_read_bignum(p
, &reqkey
.modulus
);
581 p
+= ssh1_read_bignum(p
, &challenge
);
582 memcpy(response_source
+ 32, p
, 16);
584 if (GET_32BIT(p
) != 1 ||
585 (key
= find234(rsakeys
, &reqkey
, NULL
)) == NULL
) {
586 freebn(reqkey
.exponent
);
587 freebn(reqkey
.modulus
);
591 response
= rsadecrypt(challenge
, key
);
592 for (i
= 0; i
< 32; i
++)
593 response_source
[i
] = bignum_byte(response
, 31 - i
);
596 MD5Update(&md5c
, response_source
, 48);
597 MD5Final(response_md5
, &md5c
);
598 memset(response_source
, 0, 48); /* burn the evidence */
599 freebn(response
); /* and that evidence */
600 freebn(challenge
); /* yes, and that evidence */
601 freebn(reqkey
.exponent
); /* and free some memory ... */
602 freebn(reqkey
.modulus
); /* ... while we're at it. */
605 * Packet is the obvious five byte header, plus sixteen
609 PUT_32BIT(ret
, len
- 4);
610 ret
[4] = SSH1_AGENT_RSA_RESPONSE
;
611 memcpy(ret
+ 5, response_md5
, 16);
614 case SSH2_AGENTC_SIGN_REQUEST
:
616 * Reply with either SSH2_AGENT_RSA_RESPONSE or
617 * SSH_AGENT_FAILURE, depending on whether we have that key
621 struct ssh2_userkey
*key
;
623 unsigned char *data
, *signature
;
624 int datalen
, siglen
, len
;
626 b
.len
= GET_32BIT(p
);
630 datalen
= GET_32BIT(p
);
633 key
= find234(ssh2keys
, &b
, cmpkeys_ssh2_asymm
);
636 signature
= key
->alg
->sign(key
->data
, data
, datalen
, &siglen
);
637 len
= 5 + 4 + siglen
;
638 PUT_32BIT(ret
, len
- 4);
639 ret
[4] = SSH2_AGENT_SIGN_RESPONSE
;
640 PUT_32BIT(ret
+ 5, siglen
);
641 memcpy(ret
+ 5 + 4, signature
, siglen
);
645 case SSH1_AGENTC_ADD_RSA_IDENTITY
:
647 * Add to the list and return SSH_AGENT_SUCCESS, or
648 * SSH_AGENT_FAILURE if the key was malformed.
654 key
= smalloc(sizeof(struct RSAKey
));
655 memset(key
, 0, sizeof(struct RSAKey
));
656 p
+= makekey(p
, key
, NULL
, 1);
657 p
+= makeprivate(p
, key
);
658 p
+= ssh1_read_bignum(p
, &key
->iqmp
); /* p^-1 mod q */
659 p
+= ssh1_read_bignum(p
, &key
->p
); /* p */
660 p
+= ssh1_read_bignum(p
, &key
->q
); /* q */
661 commentlen
= GET_32BIT(p
);
662 comment
= smalloc(commentlen
+1);
664 memcpy(comment
, p
+ 4, commentlen
);
665 comment
[commentlen
] = '\0';
666 key
->comment
= comment
;
669 ret
[4] = SSH_AGENT_FAILURE
;
670 if (add234(rsakeys
, key
) == key
) {
672 ret
[4] = SSH_AGENT_SUCCESS
;
679 case SSH2_AGENTC_ADD_IDENTITY
:
681 * Add to the list and return SSH_AGENT_SUCCESS, or
682 * SSH_AGENT_FAILURE if the key was malformed.
685 struct ssh2_userkey
*key
;
690 key
= smalloc(sizeof(struct ssh2_userkey
));
692 alglen
= GET_32BIT(p
);
696 /* Add further algorithm names here. */
697 if (alglen
== 7 && !memcmp(alg
, "ssh-rsa", 7))
705 GET_32BIT((unsigned char *) msg
) - (p
-
706 (unsigned char *) msg
-
708 key
->data
= key
->alg
->openssh_createkey(&p
, &bloblen
);
713 commlen
= GET_32BIT(p
);
716 comment
= smalloc(commlen
+ 1);
718 memcpy(comment
, p
, commlen
);
719 comment
[commlen
] = '\0';
721 key
->comment
= comment
;
724 ret
[4] = SSH_AGENT_FAILURE
;
725 if (add234(ssh2keys
, key
) == key
) {
727 ret
[4] = SSH_AGENT_SUCCESS
;
729 key
->alg
->freekey(key
->data
);
735 case SSH1_AGENTC_REMOVE_RSA_IDENTITY
:
737 * Remove from the list and return SSH_AGENT_SUCCESS, or
738 * perhaps SSH_AGENT_FAILURE if it wasn't in the list to
742 struct RSAKey reqkey
, *key
;
744 p
+= makekey(p
, &reqkey
, NULL
, 0);
745 key
= find234(rsakeys
, &reqkey
, NULL
);
746 freebn(reqkey
.exponent
);
747 freebn(reqkey
.modulus
);
749 ret
[4] = SSH_AGENT_FAILURE
;
751 del234(rsakeys
, key
);
755 ret
[4] = SSH_AGENT_SUCCESS
;
759 case SSH2_AGENTC_REMOVE_IDENTITY
:
761 * Remove from the list and return SSH_AGENT_SUCCESS, or
762 * perhaps SSH_AGENT_FAILURE if it wasn't in the list to
766 struct ssh2_userkey
*key
;
769 b
.len
= GET_32BIT(p
);
773 key
= find234(ssh2keys
, &b
, cmpkeys_ssh2_asymm
);
778 ret
[4] = SSH_AGENT_FAILURE
;
780 del234(ssh2keys
, key
);
782 key
->alg
->freekey(key
->data
);
784 ret
[4] = SSH_AGENT_SUCCESS
;
788 case SSH1_AGENTC_REMOVE_ALL_RSA_IDENTITIES
:
790 * Remove all SSH1 keys. Always returns success.
795 while ((rkey
= index234(rsakeys
, 0)) != NULL
) {
796 del234(rsakeys
, rkey
);
803 ret
[4] = SSH_AGENT_SUCCESS
;
806 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES
:
808 * Remove all SSH2 keys. Always returns success.
811 struct ssh2_userkey
*skey
;
813 while ((skey
= index234(ssh2keys
, 0)) != NULL
) {
814 del234(ssh2keys
, skey
);
815 skey
->alg
->freekey(skey
->data
);
821 ret
[4] = SSH_AGENT_SUCCESS
;
827 * Unrecognised message. Return SSH_AGENT_FAILURE.
830 ret
[4] = SSH_AGENT_FAILURE
;
836 * Key comparison function for the 2-3-4 tree of RSA keys.
838 static int cmpkeys_rsa(void *av
, void *bv
)
840 struct RSAKey
*a
= (struct RSAKey
*) av
;
841 struct RSAKey
*b
= (struct RSAKey
*) bv
;
848 * Compare by length of moduli.
850 alen
= bignum_bitcount(am
);
851 blen
= bignum_bitcount(bm
);
854 else if (alen
< blen
)
857 * Now compare by moduli themselves.
859 alen
= (alen
+ 7) / 8; /* byte count */
862 abyte
= bignum_byte(am
, alen
);
863 bbyte
= bignum_byte(bm
, alen
);
866 else if (abyte
< bbyte
)
876 * Key comparison function for the 2-3-4 tree of SSH2 keys.
878 static int cmpkeys_ssh2(void *av
, void *bv
)
880 struct ssh2_userkey
*a
= (struct ssh2_userkey
*) av
;
881 struct ssh2_userkey
*b
= (struct ssh2_userkey
*) bv
;
884 unsigned char *ablob
, *bblob
;
888 * Compare purely by public blob.
890 ablob
= a
->alg
->public_blob(a
->data
, &alen
);
891 bblob
= b
->alg
->public_blob(b
->data
, &blen
);
894 for (i
= 0; i
< alen
&& i
< blen
; i
++) {
895 if (ablob
[i
] < bblob
[i
]) {
898 } else if (ablob
[i
] > bblob
[i
]) {
903 if (c
== 0 && i
< alen
)
904 c
= +1; /* a is longer */
905 if (c
== 0 && i
< blen
)
906 c
= -1; /* a is longer */
915 * Key comparison function for looking up a blob in the 2-3-4 tree
918 static int cmpkeys_ssh2_asymm(void *av
, void *bv
)
920 struct blob
*a
= (struct blob
*) av
;
921 struct ssh2_userkey
*b
= (struct ssh2_userkey
*) bv
;
924 unsigned char *ablob
, *bblob
;
928 * Compare purely by public blob.
932 bblob
= b
->alg
->public_blob(b
->data
, &blen
);
935 for (i
= 0; i
< alen
&& i
< blen
; i
++) {
936 if (ablob
[i
] < bblob
[i
]) {
939 } else if (ablob
[i
] > bblob
[i
]) {
944 if (c
== 0 && i
< alen
)
945 c
= +1; /* a is longer */
946 if (c
== 0 && i
< blen
)
947 c
= -1; /* a is longer */
955 * Prompt for a key file to add, and add it.
957 static void prompt_add_keyfile(void)
960 char filename
[FILENAME_MAX
];
961 memset(&of
, 0, sizeof(of
));
962 #ifdef OPENFILENAME_SIZE_VERSION_400
963 of
.lStructSize
= OPENFILENAME_SIZE_VERSION_400
;
965 of
.lStructSize
= sizeof(of
);
968 of
.lpstrFilter
= "All Files\0*\0\0\0";
969 of
.lpstrCustomFilter
= NULL
;
971 of
.lpstrFile
= filename
;
973 of
.nMaxFile
= sizeof(filename
);
974 of
.lpstrFileTitle
= NULL
;
975 of
.lpstrInitialDir
= NULL
;
976 of
.lpstrTitle
= "Select Private Key File";
978 if (GetOpenFileName(&of
)) {
979 add_keyfile(filename
);
985 * Dialog-box function for the key list box.
987 static int CALLBACK
KeyListProc(HWND hwnd
, UINT msg
,
988 WPARAM wParam
, LPARAM lParam
)
991 struct ssh2_userkey
*skey
;
998 { /* centre the window */
1002 hw
= GetDesktopWindow();
1003 if (GetWindowRect(hw
, &rs
) && GetWindowRect(hwnd
, &rd
))
1005 (rs
.right
+ rs
.left
+ rd
.left
- rd
.right
) / 2,
1006 (rs
.bottom
+ rs
.top
+ rd
.top
- rd
.bottom
) / 2,
1007 rd
.right
- rd
.left
, rd
.bottom
- rd
.top
, TRUE
);
1012 static int tabs
[] = { 35, 60, 210 };
1013 SendDlgItemMessage(hwnd
, 100, LB_SETTABSTOPS
,
1014 sizeof(tabs
) / sizeof(*tabs
),
1020 switch (LOWORD(wParam
)) {
1024 DestroyWindow(hwnd
);
1026 case 101: /* add key */
1027 if (HIWORD(wParam
) == BN_CLICKED
||
1028 HIWORD(wParam
) == BN_DOUBLECLICKED
) {
1029 if (passphrase_box
) {
1030 MessageBeep(MB_ICONERROR
);
1031 SetForegroundWindow(passphrase_box
);
1034 prompt_add_keyfile();
1037 case 102: /* remove key */
1038 if (HIWORD(wParam
) == BN_CLICKED
||
1039 HIWORD(wParam
) == BN_DOUBLECLICKED
) {
1040 int n
= SendDlgItemMessage(hwnd
, 100, LB_GETCURSEL
, 0, 0);
1046 for (i
= 0; NULL
!= (rkey
= index234(rsakeys
, i
)); i
++)
1050 del234(rsakeys
, rkey
);
1054 for (i
= 0; NULL
!= (skey
= index234(ssh2keys
, i
));
1058 del234(ssh2keys
, skey
);
1059 skey
->alg
->freekey(skey
->data
);
1070 DestroyWindow(hwnd
);
1076 static LRESULT CALLBACK
WndProc(HWND hwnd
, UINT message
,
1077 WPARAM wParam
, LPARAM lParam
)
1080 static int menuinprogress
;
1084 if (lParam
== WM_RBUTTONUP
) {
1086 GetCursorPos(&cursorpos
);
1087 PostMessage(hwnd
, WM_SYSTRAY2
, cursorpos
.x
, cursorpos
.y
);
1088 } else if (lParam
== WM_LBUTTONDBLCLK
) {
1089 /* Equivalent to IDM_VIEWKEYS. */
1090 PostMessage(hwnd
, WM_COMMAND
, IDM_VIEWKEYS
, 0);
1094 if (!menuinprogress
) {
1096 SetForegroundWindow(hwnd
);
1097 ret
= TrackPopupMenu(systray_menu
,
1098 TPM_RIGHTALIGN
| TPM_BOTTOMALIGN
|
1100 wParam
, lParam
, 0, hwnd
, NULL
);
1106 switch (wParam
& ~0xF) { /* low 4 bits reserved to Windows */
1109 SendMessage(passphrase_box
, WM_CLOSE
, 0, 0);
1110 SendMessage(hwnd
, WM_CLOSE
, 0, 0);
1114 keylist
= CreateDialog(instance
, MAKEINTRESOURCE(211),
1116 ShowWindow(keylist
, SW_SHOWNORMAL
);
1118 * Sometimes the window comes up minimised / hidden
1119 * for no obvious reason. Prevent this.
1121 SetForegroundWindow(keylist
);
1122 SetWindowPos(keylist
, HWND_TOP
, 0, 0, 0, 0,
1123 SWP_NOMOVE
| SWP_NOSIZE
| SWP_SHOWWINDOW
);
1127 if (passphrase_box
) {
1128 MessageBeep(MB_ICONERROR
);
1129 SetForegroundWindow(passphrase_box
);
1132 prompt_add_keyfile();
1136 aboutbox
= CreateDialog(instance
, MAKEINTRESOURCE(213),
1138 ShowWindow(aboutbox
, SW_SHOWNORMAL
);
1140 * Sometimes the window comes up minimised / hidden
1141 * for no obvious reason. Prevent this.
1143 SetForegroundWindow(aboutbox
);
1144 SetWindowPos(aboutbox
, HWND_TOP
, 0, 0, 0, 0,
1145 SWP_NOMOVE
| SWP_NOSIZE
| SWP_SHOWWINDOW
);
1155 COPYDATASTRUCT
*cds
;
1161 PSID mapowner
, procowner
;
1162 PSECURITY_DESCRIPTOR psd1
= NULL
, psd2
= NULL
;
1166 cds
= (COPYDATASTRUCT
*) lParam
;
1167 if (cds
->dwData
!= AGENT_COPYDATA_ID
)
1168 return 0; /* not our message, mate */
1169 mapname
= (char *) cds
->lpData
;
1170 if (mapname
[cds
->cbData
- 1] != '\0')
1171 return 0; /* failure to be ASCIZ! */
1173 debug(("mapname is :%s:\n", mapname
));
1175 filemap
= OpenFileMapping(FILE_MAP_ALL_ACCESS
, FALSE
, mapname
);
1177 debug(("filemap is %p\n", filemap
));
1179 if (filemap
!= NULL
&& filemap
!= INVALID_HANDLE_VALUE
) {
1183 if ((proc
= OpenProcess(MAXIMUM_ALLOWED
, FALSE
,
1184 GetCurrentProcessId())) ==
1187 debug(("couldn't get handle for process\n"));
1191 if (getsecurityinfo(proc
, SE_KERNEL_OBJECT
,
1192 OWNER_SECURITY_INFORMATION
,
1193 &procowner
, NULL
, NULL
, NULL
,
1194 &psd2
) != ERROR_SUCCESS
) {
1196 debug(("couldn't get owner info for process\n"));
1199 return 0; /* unable to get security info */
1202 if ((rc
= getsecurityinfo(filemap
, SE_KERNEL_OBJECT
,
1203 OWNER_SECURITY_INFORMATION
,
1204 &mapowner
, NULL
, NULL
, NULL
,
1205 &psd1
) != ERROR_SUCCESS
)) {
1208 ("couldn't get owner info for filemap: %d\n",
1214 debug(("got security stuff\n"));
1216 if (!EqualSid(mapowner
, procowner
))
1217 return 0; /* security ID mismatch! */
1219 debug(("security stuff matched\n"));
1225 debug(("security APIs not present\n"));
1229 p
= MapViewOfFile(filemap
, FILE_MAP_WRITE
, 0, 0, 0);
1231 debug(("p is %p\n", p
));
1234 for (i
= 0; i
< 5; i
++)
1237 ((unsigned char *) p
)[i
]));}
1243 CloseHandle(filemap
);
1248 return DefWindowProc(hwnd
, message
, wParam
, lParam
);
1252 * Fork and Exec the command in cmdline. [DBW]
1254 void spawn_cmd(char *cmdline
, int show
)
1256 if (ShellExecute(NULL
, _T("open"), cmdline
,
1257 NULL
, NULL
, show
) <= (HINSTANCE
) 32) {
1259 sprintf(sMsg
, _T("Failed to run \"%.100s\", Error: %d"), cmdline
,
1260 (int)GetLastError());
1261 MessageBox(NULL
, sMsg
, APPNAME
, MB_OK
| MB_ICONEXCLAMATION
);
1265 int WINAPI
WinMain(HINSTANCE inst
, HINSTANCE prev
, LPSTR cmdline
, int show
)
1271 char *command
= NULL
;
1275 * Determine whether we're an NT system (should have security
1276 * APIs) or a non-NT system (don't do security).
1278 memset(&osi
, 0, sizeof(OSVERSIONINFO
));
1279 osi
.dwOSVersionInfoSize
= sizeof(OSVERSIONINFO
);
1280 if (GetVersionEx(&osi
) && osi
.dwPlatformId
== VER_PLATFORM_WIN32_NT
) {
1281 has_security
= TRUE
;
1283 has_security
= FALSE
;
1288 * Attempt to get the security API we need.
1290 advapi
= LoadLibrary("ADVAPI32.DLL");
1292 (gsi_fn_t
) GetProcAddress(advapi
, "GetSecurityInfo");
1293 if (!getsecurityinfo
) {
1295 "Unable to access security APIs. Pageant will\n"
1296 "not run, in case it causes a security breach.",
1297 "Pageant Fatal Error", MB_ICONERROR
| MB_OK
);
1302 "This program has been compiled for Win9X and will\n"
1303 "not run on NT, in case it causes a security breach.",
1304 "Pageant Fatal Error", MB_ICONERROR
| MB_OK
);
1313 * Find out if Pageant is already running.
1315 already_running
= FALSE
;
1317 already_running
= TRUE
;
1322 wndclass
.lpfnWndProc
= WndProc
;
1323 wndclass
.cbClsExtra
= 0;
1324 wndclass
.cbWndExtra
= 0;
1325 wndclass
.hInstance
= inst
;
1326 wndclass
.hIcon
= LoadIcon(inst
, MAKEINTRESOURCE(IDI_MAINICON
));
1327 wndclass
.hCursor
= LoadCursor(NULL
, IDC_IBEAM
);
1328 wndclass
.hbrBackground
= GetStockObject(BLACK_BRUSH
);
1329 wndclass
.lpszMenuName
= NULL
;
1330 wndclass
.lpszClassName
= APPNAME
;
1332 RegisterClass(&wndclass
);
1335 hwnd
= keylist
= NULL
;
1337 hwnd
= CreateWindow(APPNAME
, APPNAME
,
1338 WS_OVERLAPPEDWINDOW
| WS_VSCROLL
,
1339 CW_USEDEFAULT
, CW_USEDEFAULT
,
1340 100, 100, NULL
, NULL
, inst
, NULL
);
1342 /* Set up a system tray icon */
1345 NOTIFYICONDATA tnid
;
1348 #ifdef NIM_SETVERSION
1350 res
= Shell_NotifyIcon(NIM_SETVERSION
, &tnid
);
1353 tnid
.cbSize
= sizeof(NOTIFYICONDATA
);
1355 tnid
.uID
= 1; /* unique within this systray use */
1356 tnid
.uFlags
= NIF_MESSAGE
| NIF_ICON
| NIF_TIP
;
1357 tnid
.uCallbackMessage
= WM_SYSTRAY
;
1358 tnid
.hIcon
= hicon
= LoadIcon(instance
, MAKEINTRESOURCE(201));
1359 strcpy(tnid
.szTip
, "Pageant (PuTTY authentication agent)");
1361 res
= Shell_NotifyIcon(NIM_ADD
, &tnid
);
1366 systray_menu
= CreatePopupMenu();
1367 /* accelerators used: vkxa */
1368 AppendMenu(systray_menu
, MF_ENABLED
, IDM_VIEWKEYS
,
1370 AppendMenu(systray_menu
, MF_ENABLED
, IDM_ADDKEY
, "Add &Key");
1371 AppendMenu(systray_menu
, MF_ENABLED
, IDM_ABOUT
, "&About");
1372 AppendMenu(systray_menu
, MF_ENABLED
, IDM_CLOSE
, "E&xit");
1375 ShowWindow(hwnd
, SW_HIDE
);
1378 * Initialise storage for RSA keys.
1380 rsakeys
= newtree234(cmpkeys_rsa
);
1381 ssh2keys
= newtree234(cmpkeys_ssh2
);
1386 * Process the command line and add keys as listed on it.
1387 * If we already determined that we need to spawn a program from above we
1388 * need to ignore the first two arguments. [DBW]
1395 while (*p
&& isspace(*p
))
1397 if (*p
&& !isspace(*p
)) {
1398 char *q
= p
, *pp
= p
;
1399 while (*p
&& (inquotes
|| !isspace(*p
))) {
1401 inquotes
= !inquotes
;
1412 if (!strcmp(q
, "-c")) {
1414 * If we see `-c', then the rest of the
1415 * command line should be treated as a
1416 * command to be spawned.
1418 while (*p
&& isspace(*p
))
1431 spawn_cmd(command
, show
);
1434 * If Pageant was already running, we leave now. If we haven't
1435 * even taken any auxiliary action (spawned a command or added
1438 if (already_running
) {
1439 if (!command
&& !added_keys
) {
1440 MessageBox(NULL
, "Pageant is already running", "Pageant Error",
1441 MB_ICONERROR
| MB_OK
);
1444 FreeLibrary(advapi
);
1449 * Main message loop.
1451 while (GetMessage(&msg
, NULL
, 0, 0) == 1) {
1452 TranslateMessage(&msg
);
1453 DispatchMessage(&msg
);
1456 /* Clean up the system tray icon */
1458 NOTIFYICONDATA tnid
;
1460 tnid
.cbSize
= sizeof(NOTIFYICONDATA
);
1464 Shell_NotifyIcon(NIM_DELETE
, &tnid
);
1466 DestroyMenu(systray_menu
);
1470 FreeLibrary(advapi
);