2 * Pageant: the PuTTY Authentication Agent.
17 void dprintf(char *fmt
, ...);
18 #define debug(x) (dprintf x)
27 #define IDI_MAINICON 200
28 #define IDI_TRAYICON 201
30 #define WM_XUSER (WM_USER + 0x2000)
31 #define WM_SYSTRAY (WM_XUSER + 6)
32 #define WM_SYSTRAY2 (WM_XUSER + 7)
34 #define AGENT_COPYDATA_ID 0x804e50ba /* random goop */
37 * FIXME: maybe some day we can sort this out ...
39 #define AGENT_MAX_MSGLEN 8192
41 #define IDM_CLOSE 0x0010
42 #define IDM_VIEWKEYS 0x0020
43 #define IDM_ADDKEY 0x0030
44 #define IDM_ABOUT 0x0040
46 #define APPNAME "Pageant"
50 static HINSTANCE instance
;
54 static HMENU systray_menu
;
55 static int already_running
;
57 static tree234
*rsakeys
, *ssh2keys
;
59 static int has_security
;
61 typedef DWORD (WINAPI
*gsi_fn_t
)
62 (HANDLE
, SE_OBJECT_TYPE
, SECURITY_INFORMATION
,
63 PSID
*, PSID
*, PACL
*, PACL
*,
64 PSECURITY_DESCRIPTOR
*);
65 static gsi_fn_t getsecurityinfo
;
69 * We need this to link with the RSA code, because rsaencrypt()
70 * pads its data with random bytes. Since we only use rsadecrypt()
71 * and the signing functions, which are deterministic, this should
74 * If it _is_ called, there is a _serious_ problem, because it
75 * won't generate true random numbers. So we must scream, panic,
76 * and exit immediately if that should happen.
78 int random_byte(void) {
79 MessageBox(hwnd
, "Internal Error", APPNAME
, MB_OK
| MB_ICONERROR
);
84 * Blob structure for passing to the asymmetric SSH2 key compare
85 * function, prototyped here.
91 static int cmpkeys_ssh2_asymm(void *av
, void *bv
);
94 * This function is needed to link with the DES code. We need not
95 * have it do anything at all.
97 void logevent(char *msg
) {
100 #define GET_32BIT(cp) \
101 (((unsigned long)(unsigned char)(cp)[0] << 24) | \
102 ((unsigned long)(unsigned char)(cp)[1] << 16) | \
103 ((unsigned long)(unsigned char)(cp)[2] << 8) | \
104 ((unsigned long)(unsigned char)(cp)[3]))
106 #define PUT_32BIT(cp, value) { \
107 (cp)[0] = (unsigned char)((value) >> 24); \
108 (cp)[1] = (unsigned char)((value) >> 16); \
109 (cp)[2] = (unsigned char)((value) >> 8); \
110 (cp)[3] = (unsigned char)(value); }
112 #define PASSPHRASE_MAXLEN 512
114 struct PassphraseProcStruct
{
120 * Dialog-box function for the Licence box.
122 static int CALLBACK
LicenceProc (HWND hwnd
, UINT msg
,
123 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
) {
148 SetDlgItemText (hwnd
, 100, ver
);
151 switch (LOWORD(wParam
)) {
154 DestroyWindow (hwnd
);
157 EnableWindow(hwnd
, 0);
158 DialogBox (instance
, MAKEINTRESOURCE(214), NULL
, LicenceProc
);
159 EnableWindow(hwnd
, 1);
160 SetActiveWindow(hwnd
);
166 DestroyWindow (hwnd
);
173 * Dialog-box function for the passphrase box.
175 static int CALLBACK
PassphraseProc(HWND hwnd
, UINT msg
,
176 WPARAM wParam
, LPARAM lParam
) {
177 static char *passphrase
;
178 struct PassphraseProcStruct
*p
;
185 { /* centre the window */
189 hw
= GetDesktopWindow();
190 if (GetWindowRect (hw
, &rs
) && GetWindowRect (hwnd
, &rd
))
191 MoveWindow (hwnd
, (rs
.right
+ rs
.left
+ rd
.left
- rd
.right
)/2,
192 (rs
.bottom
+ rs
.top
+ rd
.top
- rd
.bottom
)/2,
193 rd
.right
-rd
.left
, rd
.bottom
-rd
.top
, TRUE
);
196 SetForegroundWindow(hwnd
);
197 SetWindowPos (hwnd
, HWND_TOP
, 0, 0, 0, 0,
198 SWP_NOMOVE
| SWP_NOSIZE
| SWP_SHOWWINDOW
);
199 p
= (struct PassphraseProcStruct
*)lParam
;
200 passphrase
= p
->passphrase
;
202 SetDlgItemText(hwnd
, 101, p
->comment
);
206 switch (LOWORD(wParam
)) {
216 case 102: /* edit box */
217 if (HIWORD(wParam
) == EN_CHANGE
) {
218 GetDlgItemText (hwnd
, 102, passphrase
, PASSPHRASE_MAXLEN
-1);
219 passphrase
[PASSPHRASE_MAXLEN
-1] = '\0';
232 * Update the visible key list.
234 static void keylist_update(void) {
236 struct ssh2_userkey
*skey
;
240 SendDlgItemMessage(keylist
, 100, LB_RESETCONTENT
, 0, 0);
241 for (rkey
= first234(rsakeys
, &e
); rkey
; rkey
= next234(&e
)) {
242 char listentry
[512], *p
;
244 * Replace two spaces in the fingerprint with tabs, for
245 * nice alignment in the box.
247 strcpy(listentry
, "ssh1\t");
248 p
= listentry
+strlen(listentry
);
249 rsa_fingerprint(p
, sizeof(listentry
)-(p
-listentry
), rkey
);
250 p
= strchr(listentry
, ' '); if (p
) *p
= '\t';
251 p
= strchr(listentry
, ' '); if (p
) *p
= '\t';
252 SendDlgItemMessage (keylist
, 100, LB_ADDSTRING
,
253 0, (LPARAM
)listentry
);
255 for (skey
= first234(ssh2keys
, &e
); skey
; skey
= next234(&e
)) {
256 char listentry
[512], *p
;
259 * Replace two spaces in the fingerprint with tabs, for
260 * nice alignment in the box.
262 p
= skey
->alg
->fingerprint(skey
->data
);
263 strncpy(listentry
, p
, sizeof(listentry
));
264 p
= strchr(listentry
, ' '); if (p
) *p
= '\t';
265 p
= strchr(listentry
, ' '); if (p
) *p
= '\t';
266 len
= strlen(listentry
);
267 if (len
< sizeof(listentry
)-2) {
268 listentry
[len
] = '\t';
269 strncpy(listentry
+len
+1, skey
->comment
, sizeof(listentry
)-len
-1);
271 SendDlgItemMessage (keylist
, 100, LB_ADDSTRING
,
272 0, (LPARAM
)listentry
);
274 SendDlgItemMessage (keylist
, 100, LB_SETCURSEL
, (WPARAM
) -1, 0);
279 * This function loads a key from a file and adds it.
281 static void add_keyfile(char *filename
) {
282 char passphrase
[PASSPHRASE_MAXLEN
];
284 struct ssh2_userkey
*skey
;
289 struct PassphraseProcStruct pps
;
292 ver
= keyfile_version(filename
);
294 MessageBox(NULL
, "Couldn't load private key.", APPNAME
,
295 MB_OK
| MB_ICONERROR
);
299 debug(("ooh %d\n", __LINE__
));
301 needs_pass
= rsakey_encrypted(filename
, &comment
);
303 needs_pass
= ssh2_userkey_encrypted(filename
, &comment
);
304 debug(("ooh %d\n", __LINE__
));
306 debug(("ooh %d\n", __LINE__
));
308 rkey
= smalloc(sizeof(*rkey
));
309 debug(("ooh %d\n", __LINE__
));
310 pps
.passphrase
= passphrase
;
311 pps
.comment
= comment
;
312 debug(("ooh %d\n", __LINE__
));
314 debug(("ooh %d\n", __LINE__
));
317 dlgret
= DialogBoxParam(instance
, MAKEINTRESOURCE(210),
318 NULL
, PassphraseProc
,
321 if (comment
) sfree(comment
);
324 return; /* operation cancelled */
328 debug(("ooh %d\n", __LINE__
));
330 ret
= loadrsakey(filename
, rkey
, passphrase
);
332 debug(("ooh %d\n", __LINE__
));
333 skey
= ssh2_load_userkey(filename
, passphrase
);
334 debug(("ooh %d\n", __LINE__
));
335 if (skey
== SSH2_WRONG_PASSPHRASE
)
344 debug(("ooh %d\n", __LINE__
));
345 if (comment
) sfree(comment
);
346 debug(("ooh %d\n", __LINE__
));
348 MessageBox(NULL
, "Couldn't load private key.", APPNAME
,
349 MB_OK
| MB_ICONERROR
);
354 debug(("ooh %d\n", __LINE__
));
356 if (already_running
) {
357 unsigned char *request
, *response
;
358 int reqlen
, clen
, resplen
;
360 debug(("ooh %d\n", __LINE__
));
361 clen
= strlen(rkey
->comment
);
362 debug(("ooh %d\n", __LINE__
));
364 reqlen
= 4 + 1 + /* length, message type */
366 ssh1_bignum_length(rkey
->modulus
) +
367 ssh1_bignum_length(rkey
->exponent
) +
368 ssh1_bignum_length(rkey
->private_exponent
) +
369 ssh1_bignum_length(rkey
->iqmp
) +
370 ssh1_bignum_length(rkey
->p
) +
371 ssh1_bignum_length(rkey
->q
) +
372 4 + clen
/* comment */
374 debug(("ooh %d %d\n", __LINE__
, reqlen
));
376 request
= smalloc(reqlen
);
377 debug(("ooh %d\n", __LINE__
));
379 debug(("ooh %d\n", __LINE__
));
380 request
[4] = SSH1_AGENTC_ADD_RSA_IDENTITY
;
381 debug(("ooh %d\n", __LINE__
));
383 PUT_32BIT(request
+reqlen
, bignum_bitcount(rkey
->modulus
));
385 reqlen
+= ssh1_write_bignum(request
+reqlen
, rkey
->modulus
);
386 reqlen
+= ssh1_write_bignum(request
+reqlen
, rkey
->exponent
);
387 reqlen
+= ssh1_write_bignum(request
+reqlen
, rkey
->private_exponent
);
388 reqlen
+= ssh1_write_bignum(request
+reqlen
, rkey
->iqmp
);
389 reqlen
+= ssh1_write_bignum(request
+reqlen
, rkey
->p
);
390 reqlen
+= ssh1_write_bignum(request
+reqlen
, rkey
->q
);
391 PUT_32BIT(request
+reqlen
, clen
);
392 memcpy(request
+reqlen
+4, rkey
->comment
, clen
);
394 PUT_32BIT(request
, reqlen
-4);
396 debug(("ooh %d\n", __LINE__
));
397 agent_query(request
, reqlen
, &response
, &resplen
);
398 debug(("ooh %d\n", __LINE__
));
399 if (resplen
< 5 || response
[4] != SSH_AGENT_SUCCESS
)
400 MessageBox(NULL
, "The already running Pageant "
401 "refused to add the key.", APPNAME
,
402 MB_OK
| MB_ICONERROR
);
404 if (add234(rsakeys
, rkey
) != rkey
)
405 sfree(rkey
); /* already present, don't waste RAM */
408 if (already_running
) {
409 unsigned char *request
, *response
;
410 int reqlen
, alglen
, clen
, keybloblen
, resplen
;
411 debug(("ooh %d\n", __LINE__
));
412 alglen
= strlen(skey
->alg
->name
);
413 debug(("ooh %d\n", __LINE__
));
414 clen
= strlen(skey
->comment
);
415 debug(("ooh %d\n", __LINE__
));
417 debug(("ooh %d\n", __LINE__
));
418 keybloblen
= skey
->alg
->openssh_fmtkey(skey
->data
, NULL
, 0);
419 debug(("ooh %d\n", __LINE__
));
421 debug(("ooh %d\n", __LINE__
));
422 reqlen
= 4 + 1 + /* length, message type */
423 4 + alglen
+ /* algorithm name */
424 keybloblen
+ /* key data */
425 4 + clen
/* comment */
427 debug(("ooh %d\n", __LINE__
));
429 debug(("ooh %d\n", __LINE__
));
430 request
= smalloc(reqlen
);
431 debug(("ooh %d\n", __LINE__
));
433 request
[4] = SSH2_AGENTC_ADD_IDENTITY
;
434 debug(("ooh %d\n", __LINE__
));
436 debug(("ooh %d\n", __LINE__
));
437 PUT_32BIT(request
+reqlen
, alglen
);
438 debug(("ooh %d\n", __LINE__
));
440 debug(("ooh %d\n", __LINE__
));
441 memcpy(request
+reqlen
, skey
->alg
->name
, alglen
);
442 debug(("ooh %d\n", __LINE__
));
444 debug(("ooh %d\n", __LINE__
));
445 reqlen
+= skey
->alg
->openssh_fmtkey(skey
->data
,
446 request
+reqlen
, keybloblen
);
447 debug(("ooh %d\n", __LINE__
));
448 PUT_32BIT(request
+reqlen
, clen
);
449 debug(("ooh %d\n", __LINE__
));
450 memcpy(request
+reqlen
+4, skey
->comment
, clen
);
451 debug(("ooh %d\n", __LINE__
));
452 PUT_32BIT(request
, reqlen
-4);
453 debug(("ooh %d\n", __LINE__
));
456 agent_query(request
, reqlen
, &response
, &resplen
);
457 debug(("ooh %d\n", __LINE__
));
458 if (resplen
< 5 || response
[4] != SSH_AGENT_SUCCESS
)
459 MessageBox(NULL
, "The already running Pageant"
460 "refused to add the key.", APPNAME
,
461 MB_OK
| MB_ICONERROR
);
462 debug(("ooh %d\n", __LINE__
));
464 if (add234(ssh2keys
, skey
) != skey
) {
465 skey
->alg
->freekey(skey
->data
);
466 sfree(skey
); /* already present, don't waste RAM */
473 * This is the main agent function that answers messages.
475 static void answer_msg(void *msg
) {
476 unsigned char *p
= msg
;
477 unsigned char *ret
= msg
;
481 * Get the message type.
487 case SSH1_AGENTC_REQUEST_RSA_IDENTITIES
:
489 * Reply with SSH1_AGENT_RSA_IDENTITIES_ANSWER.
497 * Count up the number and length of keys we hold.
500 for (key
= first234(rsakeys
, &e
); key
; key
= next234(&e
)) {
502 len
+= 4; /* length field */
503 len
+= ssh1_bignum_length(key
->exponent
);
504 len
+= ssh1_bignum_length(key
->modulus
);
505 len
+= 4 + strlen(key
->comment
);
509 * Packet header is the obvious five bytes, plus four
510 * bytes for the key count.
513 if (len
> AGENT_MAX_MSGLEN
)
514 goto failure
; /* aaargh! too much stuff! */
515 PUT_32BIT(ret
, len
-4);
516 ret
[4] = SSH1_AGENT_RSA_IDENTITIES_ANSWER
;
517 PUT_32BIT(ret
+5, nkeys
);
519 for (key
= first234(rsakeys
, &e
); key
; key
= next234(&e
)) {
520 PUT_32BIT(p
, bignum_bitcount(key
->modulus
));
522 p
+= ssh1_write_bignum(p
, key
->exponent
);
523 p
+= ssh1_write_bignum(p
, key
->modulus
);
524 PUT_32BIT(p
, strlen(key
->comment
));
525 memcpy(p
+4, key
->comment
, strlen(key
->comment
));
526 p
+= 4 + strlen(key
->comment
);
530 case SSH2_AGENTC_REQUEST_IDENTITIES
:
532 * Reply with SSH2_AGENT_IDENTITIES_ANSWER.
536 struct ssh2_userkey
*key
;
542 * Count up the number and length of keys we hold.
545 for (key
= first234(ssh2keys
, &e
); key
; key
= next234(&e
)) {
547 len
+= 4; /* length field */
548 blob
= key
->alg
->public_blob(key
->data
, &bloblen
);
551 len
+= 4 + strlen(key
->comment
);
555 * Packet header is the obvious five bytes, plus four
556 * bytes for the key count.
559 if (len
> AGENT_MAX_MSGLEN
)
560 goto failure
; /* aaargh! too much stuff! */
561 PUT_32BIT(ret
, len
-4);
562 ret
[4] = SSH2_AGENT_IDENTITIES_ANSWER
;
563 PUT_32BIT(ret
+5, nkeys
);
565 for (key
= first234(ssh2keys
, &e
); key
; key
= next234(&e
)) {
566 blob
= key
->alg
->public_blob(key
->data
, &bloblen
);
567 PUT_32BIT(p
, bloblen
);
569 memcpy(p
, blob
, bloblen
);
572 PUT_32BIT(p
, strlen(key
->comment
));
573 memcpy(p
+4, key
->comment
, strlen(key
->comment
));
574 p
+= 4 + strlen(key
->comment
);
578 case SSH1_AGENTC_RSA_CHALLENGE
:
580 * Reply with either SSH1_AGENT_RSA_RESPONSE or
581 * SSH_AGENT_FAILURE, depending on whether we have that key
585 struct RSAKey reqkey
, *key
;
586 Bignum challenge
, response
;
587 unsigned char response_source
[48], response_md5
[16];
588 struct MD5Context md5c
;
592 p
+= ssh1_read_bignum(p
, &reqkey
.exponent
);
593 p
+= ssh1_read_bignum(p
, &reqkey
.modulus
);
594 p
+= ssh1_read_bignum(p
, &challenge
);
595 memcpy(response_source
+32, p
, 16); p
+= 16;
596 if (GET_32BIT(p
) != 1 ||
597 (key
= find234(rsakeys
, &reqkey
, NULL
)) == NULL
) {
598 freebn(reqkey
.exponent
);
599 freebn(reqkey
.modulus
);
603 response
= rsadecrypt(challenge
, key
);
604 for (i
= 0; i
< 32; i
++)
605 response_source
[i
] = bignum_byte(response
, 31-i
);
608 MD5Update(&md5c
, response_source
, 48);
609 MD5Final(response_md5
, &md5c
);
610 memset(response_source
, 0, 48); /* burn the evidence */
611 freebn(response
); /* and that evidence */
612 freebn(challenge
); /* yes, and that evidence */
613 freebn(reqkey
.exponent
); /* and free some memory ... */
614 freebn(reqkey
.modulus
); /* ... while we're at it. */
617 * Packet is the obvious five byte header, plus sixteen
621 PUT_32BIT(ret
, len
-4);
622 ret
[4] = SSH1_AGENT_RSA_RESPONSE
;
623 memcpy(ret
+5, response_md5
, 16);
626 case SSH2_AGENTC_SIGN_REQUEST
:
628 * Reply with either SSH2_AGENT_RSA_RESPONSE or
629 * SSH_AGENT_FAILURE, depending on whether we have that key
633 struct ssh2_userkey
*key
;
635 unsigned char *data
, *signature
;
636 int datalen
, siglen
, len
;
638 b
.len
= GET_32BIT(p
);
642 datalen
= GET_32BIT(p
);
645 key
= find234(ssh2keys
, &b
, cmpkeys_ssh2_asymm
);
648 signature
= key
->alg
->sign(key
->data
, data
, datalen
, &siglen
);
650 PUT_32BIT(ret
, len
-4);
651 ret
[4] = SSH2_AGENT_SIGN_RESPONSE
;
652 PUT_32BIT(ret
+5, siglen
);
653 memcpy(ret
+5+4, signature
, siglen
);
657 case SSH1_AGENTC_ADD_RSA_IDENTITY
:
659 * Add to the list and return SSH_AGENT_SUCCESS, or
660 * SSH_AGENT_FAILURE if the key was malformed.
665 key
= smalloc(sizeof(struct RSAKey
));
666 memset(key
, 0, sizeof(key
));
667 p
+= makekey(p
, key
, NULL
, 1);
668 p
+= makeprivate(p
, key
);
669 p
+= ssh1_read_bignum(p
, key
->iqmp
); /* p^-1 mod q */
670 p
+= ssh1_read_bignum(p
, key
->p
); /* p */
671 p
+= ssh1_read_bignum(p
, key
->q
); /* q */
672 comment
= smalloc(GET_32BIT(p
));
674 memcpy(comment
, p
+4, GET_32BIT(p
));
675 key
->comment
= comment
;
678 ret
[4] = SSH_AGENT_FAILURE
;
679 if (add234(rsakeys
, key
) == key
) {
681 ret
[4] = SSH_AGENT_SUCCESS
;
688 case SSH2_AGENTC_ADD_IDENTITY
:
690 * Add to the list and return SSH_AGENT_SUCCESS, or
691 * SSH_AGENT_FAILURE if the key was malformed.
694 struct ssh2_userkey
*key
;
699 key
= smalloc(sizeof(struct ssh2_userkey
));
701 alglen
= GET_32BIT(p
); p
+= 4;
702 alg
= p
; p
+= alglen
;
703 /* Add further algorithm names here. */
704 if (alglen
== 7 && !memcmp(alg
, "ssh-rsa", 7))
711 bloblen
= GET_32BIT((unsigned char *)msg
) - (p
-(unsigned char *)msg
-4);
712 key
->data
= key
->alg
->openssh_createkey(&p
, &bloblen
);
717 commlen
= GET_32BIT(p
); p
+= 4;
719 comment
= smalloc(commlen
+1);
721 memcpy(comment
, p
, commlen
);
722 comment
[commlen
] = '\0';
724 key
->comment
= comment
;
727 ret
[4] = SSH_AGENT_FAILURE
;
728 if (add234(ssh2keys
, key
) == key
) {
730 ret
[4] = SSH_AGENT_SUCCESS
;
732 key
->alg
->freekey(key
->data
);
738 case SSH1_AGENTC_REMOVE_RSA_IDENTITY
:
740 * Remove from the list and return SSH_AGENT_SUCCESS, or
741 * perhaps SSH_AGENT_FAILURE if it wasn't in the list to
745 struct RSAKey reqkey
, *key
;
747 p
+= makekey(p
, &reqkey
, NULL
, 0);
748 key
= find234(rsakeys
, &reqkey
, NULL
);
749 freebn(reqkey
.exponent
);
750 freebn(reqkey
.modulus
);
752 ret
[4] = SSH_AGENT_FAILURE
;
754 del234(rsakeys
, key
);
758 ret
[4] = SSH_AGENT_SUCCESS
;
762 case SSH2_AGENTC_REMOVE_IDENTITY
:
764 * Remove from the list and return SSH_AGENT_SUCCESS, or
765 * perhaps SSH_AGENT_FAILURE if it wasn't in the list to
769 struct ssh2_userkey
*key
;
772 b
.len
= GET_32BIT(p
);
776 key
= find234(ssh2keys
, &b
, cmpkeys_ssh2_asymm
);
781 ret
[4] = SSH_AGENT_FAILURE
;
783 del234(ssh2keys
, key
);
785 key
->alg
->freekey(key
->data
);
787 ret
[4] = SSH_AGENT_SUCCESS
;
791 case SSH1_AGENTC_REMOVE_ALL_RSA_IDENTITIES
:
793 * Remove all SSH1 keys. Always returns success.
799 while ( (rkey
= first234(rsakeys
, &e
)) != NULL
) {
800 del234(rsakeys
, rkey
);
807 ret
[4] = SSH_AGENT_SUCCESS
;
810 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES
:
812 * Remove all SSH2 keys. Always returns success.
815 struct ssh2_userkey
*skey
;
818 while ( (skey
= first234(ssh2keys
, &e
)) != NULL
) {
819 del234(ssh2keys
, skey
);
820 skey
->alg
->freekey(skey
->data
);
826 ret
[4] = SSH_AGENT_SUCCESS
;
832 * Unrecognised message. Return SSH_AGENT_FAILURE.
835 ret
[4] = SSH_AGENT_FAILURE
;
841 * Key comparison function for the 2-3-4 tree of RSA keys.
843 static int cmpkeys_rsa(void *av
, void *bv
) {
844 struct RSAKey
*a
= (struct RSAKey
*)av
;
845 struct RSAKey
*b
= (struct RSAKey
*)bv
;
852 * Compare by length of moduli.
854 alen
= bignum_bitcount(am
);
855 blen
= bignum_bitcount(bm
);
856 if (alen
> blen
) return +1; else if (alen
< blen
) return -1;
858 * Now compare by moduli themselves.
860 alen
= (alen
+ 7) / 8; /* byte count */
863 abyte
= bignum_byte(am
, alen
);
864 bbyte
= bignum_byte(bm
, alen
);
865 if (abyte
> bbyte
) return +1; else if (abyte
< bbyte
) return -1;
874 * Key comparison function for the 2-3-4 tree of SSH2 keys.
876 static int cmpkeys_ssh2(void *av
, void *bv
) {
877 struct ssh2_userkey
*a
= (struct ssh2_userkey
*)av
;
878 struct ssh2_userkey
*b
= (struct ssh2_userkey
*)bv
;
881 unsigned char *ablob
, *bblob
;
885 * Compare purely by public blob.
887 ablob
= a
->alg
->public_blob(a
->data
, &alen
);
888 bblob
= b
->alg
->public_blob(b
->data
, &blen
);
891 for (i
= 0; i
< alen
&& i
< blen
; i
++) {
892 if (ablob
[i
] < bblob
[i
]) {
894 } else if (ablob
[i
] > bblob
[i
]) {
898 if (c
== 0 && i
< alen
) c
= +1; /* a is longer */
899 if (c
== 0 && i
< blen
) c
= -1; /* a is longer */
908 * Key comparison function for looking up a blob in the 2-3-4 tree
911 static int cmpkeys_ssh2_asymm(void *av
, void *bv
) {
912 struct blob
*a
= (struct blob
*)av
;
913 struct ssh2_userkey
*b
= (struct ssh2_userkey
*)bv
;
916 unsigned char *ablob
, *bblob
;
920 * Compare purely by public blob.
924 bblob
= b
->alg
->public_blob(b
->data
, &blen
);
927 for (i
= 0; i
< alen
&& i
< blen
; i
++) {
928 if (ablob
[i
] < bblob
[i
]) {
930 } else if (ablob
[i
] > bblob
[i
]) {
934 if (c
== 0 && i
< alen
) c
= +1; /* a is longer */
935 if (c
== 0 && i
< blen
) c
= -1; /* a is longer */
942 static void error(char *s
) {
943 MessageBox(hwnd
, s
, APPNAME
, MB_OK
| MB_ICONERROR
);
947 * Prompt for a key file to add, and add it.
949 static void prompt_add_keyfile(void) {
951 char filename
[FILENAME_MAX
];
952 memset(&of
, 0, sizeof(of
));
953 #ifdef OPENFILENAME_SIZE_VERSION_400
954 of
.lStructSize
= OPENFILENAME_SIZE_VERSION_400
;
956 of
.lStructSize
= sizeof(of
);
959 of
.lpstrFilter
= "All Files\0*\0\0\0";
960 of
.lpstrCustomFilter
= NULL
;
962 of
.lpstrFile
= filename
; *filename
= '\0';
963 of
.nMaxFile
= sizeof(filename
);
964 of
.lpstrFileTitle
= NULL
;
965 of
.lpstrInitialDir
= NULL
;
966 of
.lpstrTitle
= "Select Private Key File";
968 if (GetOpenFileName(&of
)) {
969 add_keyfile(filename
);
975 * Dialog-box function for the key list box.
977 static int CALLBACK
KeyListProc(HWND hwnd
, UINT msg
,
978 WPARAM wParam
, LPARAM lParam
) {
981 struct ssh2_userkey
*skey
;
988 { /* centre the window */
992 hw
= GetDesktopWindow();
993 if (GetWindowRect (hw
, &rs
) && GetWindowRect (hwnd
, &rd
))
994 MoveWindow (hwnd
, (rs
.right
+ rs
.left
+ rd
.left
- rd
.right
)/2,
995 (rs
.bottom
+ rs
.top
+ rd
.top
- rd
.bottom
)/2,
996 rd
.right
-rd
.left
, rd
.bottom
-rd
.top
, TRUE
);
1001 static int tabs
[] = {35, 60, 210};
1002 SendDlgItemMessage (hwnd
, 100, LB_SETTABSTOPS
,
1003 sizeof(tabs
)/sizeof(*tabs
), (LPARAM
) tabs
);
1008 switch (LOWORD(wParam
)) {
1012 DestroyWindow(hwnd
);
1014 case 101: /* add key */
1015 if (HIWORD(wParam
) == BN_CLICKED
||
1016 HIWORD(wParam
) == BN_DOUBLECLICKED
) {
1017 prompt_add_keyfile();
1020 case 102: /* remove key */
1021 if (HIWORD(wParam
) == BN_CLICKED
||
1022 HIWORD(wParam
) == BN_DOUBLECLICKED
) {
1023 int n
= SendDlgItemMessage (hwnd
, 100, LB_GETCURSEL
, 0, 0);
1028 for (rkey
= first234(rsakeys
, &e
); rkey
; rkey
= next234(&e
))
1032 del234(rsakeys
, rkey
);
1036 for (skey
= first234(ssh2keys
, &e
); skey
; skey
= next234(&e
))
1040 del234(ssh2keys
, skey
);
1041 skey
->alg
->freekey(skey
->data
);
1052 DestroyWindow(hwnd
);
1058 static LRESULT CALLBACK
WndProc (HWND hwnd
, UINT message
,
1059 WPARAM wParam
, LPARAM lParam
) {
1061 static int menuinprogress
;
1065 if (lParam
== WM_RBUTTONUP
) {
1067 GetCursorPos(&cursorpos
);
1068 PostMessage(hwnd
, WM_SYSTRAY2
, cursorpos
.x
, cursorpos
.y
);
1069 } else if (lParam
== WM_LBUTTONDBLCLK
) {
1070 /* Equivalent to IDM_VIEWKEYS. */
1071 PostMessage(hwnd
, WM_COMMAND
, IDM_VIEWKEYS
, 0);
1075 if (!menuinprogress
) {
1077 SetForegroundWindow(hwnd
);
1078 ret
= TrackPopupMenu(systray_menu
,
1079 TPM_RIGHTALIGN
| TPM_BOTTOMALIGN
|
1081 wParam
, lParam
, 0, hwnd
, NULL
);
1087 switch (wParam
& ~0xF) { /* low 4 bits reserved to Windows */
1089 SendMessage(hwnd
, WM_CLOSE
, 0, 0);
1093 keylist
= CreateDialog (instance
, MAKEINTRESOURCE(211),
1095 ShowWindow (keylist
, SW_SHOWNORMAL
);
1097 * Sometimes the window comes up minimised / hidden
1098 * for no obvious reason. Prevent this.
1100 SetForegroundWindow(keylist
);
1101 SetWindowPos (keylist
, HWND_TOP
, 0, 0, 0, 0,
1102 SWP_NOMOVE
| SWP_NOSIZE
| SWP_SHOWWINDOW
);
1106 prompt_add_keyfile();
1110 aboutbox
= CreateDialog (instance
, MAKEINTRESOURCE(213),
1112 ShowWindow (aboutbox
, SW_SHOWNORMAL
);
1114 * Sometimes the window comes up minimised / hidden
1115 * for no obvious reason. Prevent this.
1117 SetForegroundWindow(aboutbox
);
1118 SetWindowPos (aboutbox
, HWND_TOP
, 0, 0, 0, 0,
1119 SWP_NOMOVE
| SWP_NOSIZE
| SWP_SHOWWINDOW
);
1125 PostQuitMessage (0);
1129 COPYDATASTRUCT
*cds
;
1132 HANDLE filemap
, proc
;
1133 PSID mapowner
, procowner
;
1134 PSECURITY_DESCRIPTOR psd1
= NULL
, psd2
= NULL
;
1137 cds
= (COPYDATASTRUCT
*)lParam
;
1138 if (cds
->dwData
!= AGENT_COPYDATA_ID
)
1139 return 0; /* not our message, mate */
1140 mapname
= (char *)cds
->lpData
;
1141 if (mapname
[cds
->cbData
- 1] != '\0')
1142 return 0; /* failure to be ASCIZ! */
1144 debug(("mapname is :%s:\r\n", mapname
));
1146 filemap
= OpenFileMapping(FILE_MAP_ALL_ACCESS
, FALSE
, mapname
);
1148 debug(("filemap is %p\r\n", filemap
));
1150 if (filemap
!= NULL
&& filemap
!= INVALID_HANDLE_VALUE
) {
1154 if ((proc
= OpenProcess(MAXIMUM_ALLOWED
, FALSE
,
1155 GetCurrentProcessId())) == NULL
) {
1157 debug(("couldn't get handle for process\r\n"));
1161 if (getsecurityinfo(proc
, SE_KERNEL_OBJECT
,
1162 OWNER_SECURITY_INFORMATION
,
1163 &procowner
, NULL
, NULL
, NULL
,
1164 &psd2
) != ERROR_SUCCESS
) {
1166 debug(("couldn't get owner info for process\r\n"));
1169 return 0; /* unable to get security info */
1172 if ((rc
= getsecurityinfo(filemap
, SE_KERNEL_OBJECT
,
1173 OWNER_SECURITY_INFORMATION
,
1174 &mapowner
, NULL
, NULL
, NULL
,
1175 &psd1
) != ERROR_SUCCESS
)) {
1177 debug(("couldn't get owner info for filemap: %d\r\n", rc
));
1182 debug(("got security stuff\r\n"));
1184 if (!EqualSid(mapowner
, procowner
))
1185 return 0; /* security ID mismatch! */
1187 debug(("security stuff matched\r\n"));
1193 debug(("security APIs not present\r\n"));
1197 p
= MapViewOfFile(filemap
, FILE_MAP_WRITE
, 0, 0, 0);
1199 debug(("p is %p\r\n", p
));
1200 {int i
; for(i
=0;i
<5;i
++)debug(("p[%d]=%02x\r\n", i
, ((unsigned char *)p
)[i
]));}
1206 CloseHandle(filemap
);
1211 return DefWindowProc (hwnd
, message
, wParam
, lParam
);
1215 * Fork and Exec the command in cmdline. [DBW]
1217 void spawn_cmd(char *cmdline
, int show
) {
1218 if (ShellExecute(NULL
, _T("open"), cmdline
,
1219 NULL
, NULL
, show
) <= (HINSTANCE
) 32) {
1221 sprintf(sMsg
, _T("Failed to run \"%.100s\", Error: %d"), cmdline
,
1223 MessageBox(NULL
, sMsg
, APPNAME
, MB_OK
| MB_ICONEXCLAMATION
);
1227 int WINAPI
WinMain(HINSTANCE inst
, HINSTANCE prev
, LPSTR cmdline
, int show
) {
1232 char *command
= NULL
;
1236 * Determine whether we're an NT system (should have security
1237 * APIs) or a non-NT system (don't do security).
1239 memset(&osi
, 0, sizeof(OSVERSIONINFO
));
1240 osi
.dwOSVersionInfoSize
= sizeof(OSVERSIONINFO
);
1241 if (GetVersionEx(&osi
) && osi
.dwPlatformId
==VER_PLATFORM_WIN32_NT
) {
1242 has_security
= TRUE
;
1244 has_security
= FALSE
;
1249 * Attempt to get the security API we need.
1251 advapi
= LoadLibrary("ADVAPI32.DLL");
1252 getsecurityinfo
= (gsi_fn_t
)GetProcAddress(advapi
, "GetSecurityInfo");
1253 if (!getsecurityinfo
) {
1255 "Unable to access security APIs. Pageant will\n"
1256 "not run, in case it causes a security breach.",
1257 "Pageant Fatal Error", MB_ICONERROR
| MB_OK
);
1262 "This program has been compiled for Win9X and will\n"
1263 "not run on NT, in case it causes a security breach.",
1264 "Pageant Fatal Error", MB_ICONERROR
| MB_OK
);
1273 * Find out if Pageant is already running.
1275 already_running
= FALSE
;
1276 if (FindWindow("Pageant", "Pageant"))
1277 already_running
= TRUE
;
1282 wndclass
.lpfnWndProc
= WndProc
;
1283 wndclass
.cbClsExtra
= 0;
1284 wndclass
.cbWndExtra
= 0;
1285 wndclass
.hInstance
= inst
;
1286 wndclass
.hIcon
= LoadIcon (inst
,
1287 MAKEINTRESOURCE(IDI_MAINICON
));
1288 wndclass
.hCursor
= LoadCursor (NULL
, IDC_IBEAM
);
1289 wndclass
.hbrBackground
= GetStockObject (BLACK_BRUSH
);
1290 wndclass
.lpszMenuName
= NULL
;
1291 wndclass
.lpszClassName
= APPNAME
;
1293 RegisterClass (&wndclass
);
1296 hwnd
= keylist
= NULL
;
1298 hwnd
= CreateWindow (APPNAME
, APPNAME
,
1299 WS_OVERLAPPEDWINDOW
| WS_VSCROLL
,
1300 CW_USEDEFAULT
, CW_USEDEFAULT
,
1301 100, 100, NULL
, NULL
, inst
, NULL
);
1303 /* Set up a system tray icon */
1306 NOTIFYICONDATA tnid
;
1309 #ifdef NIM_SETVERSION
1311 res
= Shell_NotifyIcon(NIM_SETVERSION
, &tnid
);
1314 tnid
.cbSize
= sizeof(NOTIFYICONDATA
);
1316 tnid
.uID
= 1; /* unique within this systray use */
1317 tnid
.uFlags
= NIF_MESSAGE
| NIF_ICON
| NIF_TIP
;
1318 tnid
.uCallbackMessage
= WM_SYSTRAY
;
1319 tnid
.hIcon
= hicon
= LoadIcon (instance
, MAKEINTRESOURCE(201));
1320 strcpy(tnid
.szTip
, "Pageant (PuTTY authentication agent)");
1322 res
= Shell_NotifyIcon(NIM_ADD
, &tnid
);
1327 systray_menu
= CreatePopupMenu();
1328 /* accelerators used: vkxa */
1329 AppendMenu (systray_menu
, MF_ENABLED
, IDM_VIEWKEYS
, "&View Keys");
1330 AppendMenu (systray_menu
, MF_ENABLED
, IDM_ADDKEY
, "Add &Key");
1331 AppendMenu (systray_menu
, MF_ENABLED
, IDM_ABOUT
, "&About");
1332 AppendMenu (systray_menu
, MF_ENABLED
, IDM_CLOSE
, "E&xit");
1335 ShowWindow (hwnd
, SW_HIDE
);
1338 * Initialise storage for RSA keys.
1340 rsakeys
= newtree234(cmpkeys_rsa
);
1341 ssh2keys
= newtree234(cmpkeys_ssh2
);
1346 * Process the command line and add keys as listed on it.
1347 * If we already determined that we need to spawn a program from above we
1348 * need to ignore the first two arguments. [DBW]
1356 while (*p
&& isspace(*p
)) p
++;
1357 if (*p
&& !isspace(*p
)) {
1358 char *q
= p
, *pp
= p
;
1359 while (*p
&& (inquotes
|| !isspace(*p
)))
1362 inquotes
= !inquotes
;
1372 if (!strcmp(q
, "-c")) {
1374 * If we see `-c', then the rest of the
1375 * command line should be treated as a
1376 * command to be spawned.
1378 while (*p
&& isspace(*p
)) p
++;
1389 if (command
) spawn_cmd (command
, show
);
1392 * If Pageant was already running, we leave now. If we haven't
1393 * even taken any auxiliary action (spawned a command or added
1396 if (already_running
) {
1397 if (!command
&& !added_keys
) {
1398 MessageBox(NULL
, "Pageant is already running", "Pageant Error",
1399 MB_ICONERROR
| MB_OK
);
1401 if (advapi
) FreeLibrary(advapi
);
1406 * Main message loop.
1408 while (GetMessage(&msg
, NULL
, 0, 0) == 1) {
1409 TranslateMessage(&msg
);
1410 DispatchMessage(&msg
);
1413 /* Clean up the system tray icon */
1415 NOTIFYICONDATA tnid
;
1417 tnid
.cbSize
= sizeof(NOTIFYICONDATA
);
1421 Shell_NotifyIcon(NIM_DELETE
, &tnid
);
1423 DestroyMenu(systray_menu
);
1426 if (advapi
) FreeLibrary(advapi
);