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);
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)
97 #define GET_32BIT(cp) \
98 (((unsigned long)(unsigned char)(cp)[0] << 24) | \
99 ((unsigned long)(unsigned char)(cp)[1] << 16) | \
100 ((unsigned long)(unsigned char)(cp)[2] << 8) | \
101 ((unsigned long)(unsigned char)(cp)[3]))
103 #define PUT_32BIT(cp, value) { \
104 (cp)[0] = (unsigned char)((value) >> 24); \
105 (cp)[1] = (unsigned char)((value) >> 16); \
106 (cp)[2] = (unsigned char)((value) >> 8); \
107 (cp)[3] = (unsigned char)(value); }
109 #define PASSPHRASE_MAXLEN 512
111 struct PassphraseProcStruct {
117 * Dialog-box function for the Licence box.
119 static int CALLBACK LicenceProc(HWND hwnd, UINT msg,
120 WPARAM wParam, LPARAM lParam)
126 switch (LOWORD(wParam)) {
140 * Dialog-box function for the About box.
142 static int CALLBACK AboutProc(HWND hwnd, UINT msg,
143 WPARAM wParam, LPARAM lParam)
147 SetDlgItemText(hwnd, 100, ver);
150 switch (LOWORD(wParam)) {
156 EnableWindow(hwnd, 0);
157 DialogBox(instance, MAKEINTRESOURCE(214), NULL, LicenceProc);
158 EnableWindow(hwnd, 1);
159 SetActiveWindow(hwnd);
172 * Dialog-box function for the passphrase box.
174 static int CALLBACK PassphraseProc(HWND hwnd, UINT msg,
175 WPARAM wParam, LPARAM lParam)
177 static char *passphrase = NULL;
178 struct PassphraseProcStruct *p;
185 { /* centre the window */
189 hw = GetDesktopWindow();
190 if (GetWindowRect(hw, &rs) && GetWindowRect(hwnd, &rd))
192 (rs.right + rs.left + rd.left - rd.right) / 2,
193 (rs.bottom + rs.top + rd.top - rd.bottom) / 2,
194 rd.right - rd.left, rd.bottom - rd.top, TRUE);
197 SetForegroundWindow(hwnd);
198 SetWindowPos(hwnd, HWND_TOP, 0, 0, 0, 0,
199 SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
200 p = (struct PassphraseProcStruct *) lParam;
201 passphrase = p->passphrase;
203 SetDlgItemText(hwnd, 101, p->comment);
205 SetDlgItemText(hwnd, 102, passphrase);
208 switch (LOWORD(wParam)) {
218 case 102: /* edit box */
219 if ((HIWORD(wParam) == EN_CHANGE) && passphrase) {
220 GetDlgItemText(hwnd, 102, passphrase,
221 PASSPHRASE_MAXLEN - 1);
222 passphrase[PASSPHRASE_MAXLEN - 1] = '\0';
235 * Update the visible key list.
237 static void keylist_update(void)
240 struct ssh2_userkey *skey;
244 SendDlgItemMessage(keylist, 100, LB_RESETCONTENT, 0, 0);
245 for (i = 0; NULL != (rkey = index234(rsakeys, i)); i++) {
246 char listentry[512], *p;
248 * Replace two spaces in the fingerprint with tabs, for
249 * nice alignment in the box.
251 strcpy(listentry, "ssh1\t");
252 p = listentry + strlen(listentry);
253 rsa_fingerprint(p, sizeof(listentry) - (p - listentry), rkey);
254 p = strchr(listentry, ' ');
257 p = strchr(listentry, ' ');
260 SendDlgItemMessage(keylist, 100, LB_ADDSTRING,
261 0, (LPARAM) listentry);
263 for (i = 0; NULL != (skey = index234(ssh2keys, i)); i++) {
264 char listentry[512], *p;
267 * Replace two spaces in the fingerprint with tabs, for
268 * nice alignment in the box.
270 p = skey->alg->fingerprint(skey->data);
271 strncpy(listentry, p, sizeof(listentry));
272 p = strchr(listentry, ' ');
275 p = strchr(listentry, ' ');
278 len = strlen(listentry);
279 if (len < sizeof(listentry) - 2) {
280 listentry[len] = '\t';
281 strncpy(listentry + len + 1, skey->comment,
282 sizeof(listentry) - len - 1);
284 SendDlgItemMessage(keylist, 100, LB_ADDSTRING, 0,
287 SendDlgItemMessage(keylist, 100, LB_SETCURSEL, (WPARAM) - 1, 0);
292 * This function loads a key from a file and adds it.
294 static void add_keyfile(char *filename)
296 char passphrase[PASSPHRASE_MAXLEN];
297 struct RSAKey *rkey = NULL;
298 struct ssh2_userkey *skey = NULL;
303 struct PassphraseProcStruct pps;
306 ver = keyfile_version(filename);
308 MessageBox(NULL, "Couldn't load private key.", APPNAME,
309 MB_OK | MB_ICONERROR);
314 needs_pass = rsakey_encrypted(filename, &comment);
316 needs_pass = ssh2_userkey_encrypted(filename, &comment);
319 rkey = smalloc(sizeof(*rkey));
320 pps.passphrase = passphrase;
321 pps.comment = comment;
325 dlgret = DialogBoxParam(instance, MAKEINTRESOURCE(210),
326 NULL, PassphraseProc, (LPARAM) & pps);
332 return; /* operation cancelled */
337 ret = loadrsakey(filename, rkey, passphrase);
339 skey = ssh2_load_userkey(filename, passphrase);
340 if (skey == SSH2_WRONG_PASSPHRASE)
352 MessageBox(NULL, "Couldn't load private key.", APPNAME,
353 MB_OK | MB_ICONERROR);
359 if (already_running) {
360 unsigned char *request, *response;
362 int reqlen, clen, resplen;
364 clen = strlen(rkey->comment);
366 reqlen = 4 + 1 + /* length, message type */
368 ssh1_bignum_length(rkey->modulus) +
369 ssh1_bignum_length(rkey->exponent) +
370 ssh1_bignum_length(rkey->private_exponent) +
371 ssh1_bignum_length(rkey->iqmp) +
372 ssh1_bignum_length(rkey->p) +
373 ssh1_bignum_length(rkey->q) + 4 + clen /* comment */
376 request = smalloc(reqlen);
378 request[4] = SSH1_AGENTC_ADD_RSA_IDENTITY;
380 PUT_32BIT(request + reqlen, bignum_bitcount(rkey->modulus));
382 reqlen += ssh1_write_bignum(request + reqlen, rkey->modulus);
383 reqlen += ssh1_write_bignum(request + reqlen, rkey->exponent);
385 ssh1_write_bignum(request + reqlen,
386 rkey->private_exponent);
387 reqlen += ssh1_write_bignum(request + reqlen, rkey->iqmp);
388 reqlen += ssh1_write_bignum(request + reqlen, rkey->p);
389 reqlen += ssh1_write_bignum(request + reqlen, rkey->q);
390 PUT_32BIT(request + reqlen, clen);
391 memcpy(request + reqlen + 4, rkey->comment, clen);
393 PUT_32BIT(request, reqlen - 4);
395 agent_query(request, reqlen, &vresponse, &resplen);
396 response = vresponse;
397 if (resplen < 5 || response[4] != SSH_AGENT_SUCCESS)
398 MessageBox(NULL, "The already running Pageant "
399 "refused to add the key.", APPNAME,
400 MB_OK | MB_ICONERROR);
402 if (add234(rsakeys, rkey) != rkey)
403 sfree(rkey); /* already present, don't waste RAM */
406 if (already_running) {
407 unsigned char *request, *response;
409 int reqlen, alglen, clen, keybloblen, resplen;
410 alglen = strlen(skey->alg->name);
411 clen = strlen(skey->comment);
413 keybloblen = skey->alg->openssh_fmtkey(skey->data, NULL, 0);
415 reqlen = 4 + 1 + /* length, message type */
416 4 + alglen + /* algorithm name */
417 keybloblen + /* key data */
418 4 + clen /* comment */
421 request = smalloc(reqlen);
423 request[4] = SSH2_AGENTC_ADD_IDENTITY;
425 PUT_32BIT(request + reqlen, alglen);
427 memcpy(request + reqlen, skey->alg->name, alglen);
429 reqlen += skey->alg->openssh_fmtkey(skey->data,
432 PUT_32BIT(request + reqlen, clen);
433 memcpy(request + reqlen + 4, skey->comment, clen);
434 PUT_32BIT(request, reqlen - 4);
437 agent_query(request, reqlen, &vresponse, &resplen);
438 response = vresponse;
439 if (resplen < 5 || response[4] != SSH_AGENT_SUCCESS)
440 MessageBox(NULL, "The already running Pageant"
441 "refused to add the key.", APPNAME,
442 MB_OK | MB_ICONERROR);
444 if (add234(ssh2keys, skey) != skey) {
445 skey->alg->freekey(skey->data);
446 sfree(skey); /* already present, don't waste RAM */
453 * This is the main agent function that answers messages.
455 static void answer_msg(void *msg)
457 unsigned char *p = msg;
458 unsigned char *ret = msg;
462 * Get the message type.
468 case SSH1_AGENTC_REQUEST_RSA_IDENTITIES:
470 * Reply with SSH1_AGENT_RSA_IDENTITIES_ANSWER.
478 * Count up the number and length of keys we hold.
481 for (i = 0; NULL != (key = index234(rsakeys, i)); i++) {
483 len += 4; /* length field */
484 len += ssh1_bignum_length(key->exponent);
485 len += ssh1_bignum_length(key->modulus);
486 len += 4 + strlen(key->comment);
490 * Packet header is the obvious five bytes, plus four
491 * bytes for the key count.
494 if (len > AGENT_MAX_MSGLEN)
495 goto failure; /* aaargh! too much stuff! */
496 PUT_32BIT(ret, len - 4);
497 ret[4] = SSH1_AGENT_RSA_IDENTITIES_ANSWER;
498 PUT_32BIT(ret + 5, nkeys);
500 for (i = 0; NULL != (key = index234(rsakeys, i)); i++) {
501 PUT_32BIT(p, bignum_bitcount(key->modulus));
503 p += ssh1_write_bignum(p, key->exponent);
504 p += ssh1_write_bignum(p, key->modulus);
505 PUT_32BIT(p, strlen(key->comment));
506 memcpy(p + 4, key->comment, strlen(key->comment));
507 p += 4 + strlen(key->comment);
511 case SSH2_AGENTC_REQUEST_IDENTITIES:
513 * Reply with SSH2_AGENT_IDENTITIES_ANSWER.
516 struct ssh2_userkey *key;
523 * Count up the number and length of keys we hold.
526 for (i = 0; NULL != (key = index234(ssh2keys, i)); i++) {
528 len += 4; /* length field */
529 blob = key->alg->public_blob(key->data, &bloblen);
532 len += 4 + strlen(key->comment);
536 * Packet header is the obvious five bytes, plus four
537 * bytes for the key count.
540 if (len > AGENT_MAX_MSGLEN)
541 goto failure; /* aaargh! too much stuff! */
542 PUT_32BIT(ret, len - 4);
543 ret[4] = SSH2_AGENT_IDENTITIES_ANSWER;
544 PUT_32BIT(ret + 5, nkeys);
546 for (i = 0; NULL != (key = index234(ssh2keys, i)); i++) {
547 blob = key->alg->public_blob(key->data, &bloblen);
548 PUT_32BIT(p, bloblen);
550 memcpy(p, blob, bloblen);
553 PUT_32BIT(p, strlen(key->comment));
554 memcpy(p + 4, key->comment, strlen(key->comment));
555 p += 4 + strlen(key->comment);
559 case SSH1_AGENTC_RSA_CHALLENGE:
561 * Reply with either SSH1_AGENT_RSA_RESPONSE or
562 * SSH_AGENT_FAILURE, depending on whether we have that key
566 struct RSAKey reqkey, *key;
567 Bignum challenge, response;
568 unsigned char response_source[48], response_md5[16];
569 struct MD5Context md5c;
573 p += ssh1_read_bignum(p, &reqkey.exponent);
574 p += ssh1_read_bignum(p, &reqkey.modulus);
575 p += ssh1_read_bignum(p, &challenge);
576 memcpy(response_source + 32, p, 16);
578 if (GET_32BIT(p) != 1 ||
579 (key = find234(rsakeys, &reqkey, NULL)) == NULL) {
580 freebn(reqkey.exponent);
581 freebn(reqkey.modulus);
585 response = rsadecrypt(challenge, key);
586 for (i = 0; i < 32; i++)
587 response_source[i] = bignum_byte(response, 31 - i);
590 MD5Update(&md5c, response_source, 48);
591 MD5Final(response_md5, &md5c);
592 memset(response_source, 0, 48); /* burn the evidence */
593 freebn(response); /* and that evidence */
594 freebn(challenge); /* yes, and that evidence */
595 freebn(reqkey.exponent); /* and free some memory ... */
596 freebn(reqkey.modulus); /* ... while we're at it. */
599 * Packet is the obvious five byte header, plus sixteen
603 PUT_32BIT(ret, len - 4);
604 ret[4] = SSH1_AGENT_RSA_RESPONSE;
605 memcpy(ret + 5, response_md5, 16);
608 case SSH2_AGENTC_SIGN_REQUEST:
610 * Reply with either SSH2_AGENT_RSA_RESPONSE or
611 * SSH_AGENT_FAILURE, depending on whether we have that key
615 struct ssh2_userkey *key;
617 unsigned char *data, *signature;
618 int datalen, siglen, len;
620 b.len = GET_32BIT(p);
624 datalen = GET_32BIT(p);
627 key = find234(ssh2keys, &b, cmpkeys_ssh2_asymm);
630 signature = key->alg->sign(key->data, data, datalen, &siglen);
631 len = 5 + 4 + siglen;
632 PUT_32BIT(ret, len - 4);
633 ret[4] = SSH2_AGENT_SIGN_RESPONSE;
634 PUT_32BIT(ret + 5, siglen);
635 memcpy(ret + 5 + 4, signature, siglen);
639 case SSH1_AGENTC_ADD_RSA_IDENTITY:
641 * Add to the list and return SSH_AGENT_SUCCESS, or
642 * SSH_AGENT_FAILURE if the key was malformed.
647 key = smalloc(sizeof(struct RSAKey));
648 memset(key, 0, sizeof(key));
649 p += makekey(p, key, NULL, 1);
650 p += makeprivate(p, key);
651 p += ssh1_read_bignum(p, key->iqmp); /* p^-1 mod q */
652 p += ssh1_read_bignum(p, key->p); /* p */
653 p += ssh1_read_bignum(p, key->q); /* q */
654 comment = smalloc(GET_32BIT(p));
656 memcpy(comment, p + 4, GET_32BIT(p));
657 key->comment = comment;
660 ret[4] = SSH_AGENT_FAILURE;
661 if (add234(rsakeys, key) == key) {
663 ret[4] = SSH_AGENT_SUCCESS;
670 case SSH2_AGENTC_ADD_IDENTITY:
672 * Add to the list and return SSH_AGENT_SUCCESS, or
673 * SSH_AGENT_FAILURE if the key was malformed.
676 struct ssh2_userkey *key;
681 key = smalloc(sizeof(struct ssh2_userkey));
683 alglen = GET_32BIT(p);
687 /* Add further algorithm names here. */
688 if (alglen == 7 && !memcmp(alg, "ssh-rsa", 7))
696 GET_32BIT((unsigned char *) msg) - (p -
697 (unsigned char *) msg -
699 key->data = key->alg->openssh_createkey(&p, &bloblen);
704 commlen = GET_32BIT(p);
707 comment = smalloc(commlen + 1);
709 memcpy(comment, p, commlen);
710 comment[commlen] = '\0';
712 key->comment = comment;
715 ret[4] = SSH_AGENT_FAILURE;
716 if (add234(ssh2keys, key) == key) {
718 ret[4] = SSH_AGENT_SUCCESS;
720 key->alg->freekey(key->data);
726 case SSH1_AGENTC_REMOVE_RSA_IDENTITY:
728 * Remove from the list and return SSH_AGENT_SUCCESS, or
729 * perhaps SSH_AGENT_FAILURE if it wasn't in the list to
733 struct RSAKey reqkey, *key;
735 p += makekey(p, &reqkey, NULL, 0);
736 key = find234(rsakeys, &reqkey, NULL);
737 freebn(reqkey.exponent);
738 freebn(reqkey.modulus);
740 ret[4] = SSH_AGENT_FAILURE;
742 del234(rsakeys, key);
746 ret[4] = SSH_AGENT_SUCCESS;
750 case SSH2_AGENTC_REMOVE_IDENTITY:
752 * Remove from the list and return SSH_AGENT_SUCCESS, or
753 * perhaps SSH_AGENT_FAILURE if it wasn't in the list to
757 struct ssh2_userkey *key;
760 b.len = GET_32BIT(p);
764 key = find234(ssh2keys, &b, cmpkeys_ssh2_asymm);
769 ret[4] = SSH_AGENT_FAILURE;
771 del234(ssh2keys, key);
773 key->alg->freekey(key->data);
775 ret[4] = SSH_AGENT_SUCCESS;
779 case SSH1_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
781 * Remove all SSH1 keys. Always returns success.
786 while ((rkey = index234(rsakeys, 0)) != NULL) {
787 del234(rsakeys, rkey);
794 ret[4] = SSH_AGENT_SUCCESS;
797 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
799 * Remove all SSH2 keys. Always returns success.
802 struct ssh2_userkey *skey;
804 while ((skey = index234(ssh2keys, 0)) != NULL) {
805 del234(ssh2keys, skey);
806 skey->alg->freekey(skey->data);
812 ret[4] = SSH_AGENT_SUCCESS;
818 * Unrecognised message. Return SSH_AGENT_FAILURE.
821 ret[4] = SSH_AGENT_FAILURE;
827 * Key comparison function for the 2-3-4 tree of RSA keys.
829 static int cmpkeys_rsa(void *av, void *bv)
831 struct RSAKey *a = (struct RSAKey *) av;
832 struct RSAKey *b = (struct RSAKey *) bv;
839 * Compare by length of moduli.
841 alen = bignum_bitcount(am);
842 blen = bignum_bitcount(bm);
845 else if (alen < blen)
848 * Now compare by moduli themselves.
850 alen = (alen + 7) / 8; /* byte count */
853 abyte = bignum_byte(am, alen);
854 bbyte = bignum_byte(bm, alen);
857 else if (abyte < bbyte)
867 * Key comparison function for the 2-3-4 tree of SSH2 keys.
869 static int cmpkeys_ssh2(void *av, void *bv)
871 struct ssh2_userkey *a = (struct ssh2_userkey *) av;
872 struct ssh2_userkey *b = (struct ssh2_userkey *) bv;
875 unsigned char *ablob, *bblob;
879 * Compare purely by public blob.
881 ablob = a->alg->public_blob(a->data, &alen);
882 bblob = b->alg->public_blob(b->data, &blen);
885 for (i = 0; i < alen && i < blen; i++) {
886 if (ablob[i] < bblob[i]) {
889 } else if (ablob[i] > bblob[i]) {
894 if (c == 0 && i < alen)
895 c = +1; /* a is longer */
896 if (c == 0 && i < blen)
897 c = -1; /* a is longer */
906 * Key comparison function for looking up a blob in the 2-3-4 tree
909 static int cmpkeys_ssh2_asymm(void *av, void *bv)
911 struct blob *a = (struct blob *) av;
912 struct ssh2_userkey *b = (struct ssh2_userkey *) bv;
915 unsigned char *ablob, *bblob;
919 * Compare purely by public blob.
923 bblob = b->alg->public_blob(b->data, &blen);
926 for (i = 0; i < alen && i < blen; i++) {
927 if (ablob[i] < bblob[i]) {
930 } else if (ablob[i] > bblob[i]) {
935 if (c == 0 && i < alen)
936 c = +1; /* a is longer */
937 if (c == 0 && i < blen)
938 c = -1; /* a is longer */
946 * Prompt for a key file to add, and add it.
948 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;
964 of.nMaxFile = sizeof(filename);
965 of.lpstrFileTitle = NULL;
966 of.lpstrInitialDir = NULL;
967 of.lpstrTitle = "Select Private Key File";
969 if (GetOpenFileName(&of)) {
970 add_keyfile(filename);
976 * Dialog-box function for the key list box.
978 static int CALLBACK KeyListProc(HWND hwnd, UINT msg,
979 WPARAM wParam, LPARAM lParam)
982 struct ssh2_userkey *skey;
989 { /* centre the window */
993 hw = GetDesktopWindow();
994 if (GetWindowRect(hw, &rs) && GetWindowRect(hwnd, &rd))
996 (rs.right + rs.left + rd.left - rd.right) / 2,
997 (rs.bottom + rs.top + rd.top - rd.bottom) / 2,
998 rd.right - rd.left, rd.bottom - rd.top, TRUE);
1003 static int tabs[] = { 35, 60, 210 };
1004 SendDlgItemMessage(hwnd, 100, LB_SETTABSTOPS,
1005 sizeof(tabs) / sizeof(*tabs),
1011 switch (LOWORD(wParam)) {
1015 DestroyWindow(hwnd);
1017 case 101: /* add key */
1018 if (HIWORD(wParam) == BN_CLICKED ||
1019 HIWORD(wParam) == BN_DOUBLECLICKED) {
1020 prompt_add_keyfile();
1023 case 102: /* remove key */
1024 if (HIWORD(wParam) == BN_CLICKED ||
1025 HIWORD(wParam) == BN_DOUBLECLICKED) {
1026 int n = SendDlgItemMessage(hwnd, 100, LB_GETCURSEL, 0, 0);
1032 for (i = 0; NULL != (rkey = index234(rsakeys, i)); i++)
1036 del234(rsakeys, rkey);
1040 for (i = 0; NULL != (skey = index234(ssh2keys, i));
1044 del234(ssh2keys, skey);
1045 skey->alg->freekey(skey->data);
1056 DestroyWindow(hwnd);
1062 static LRESULT CALLBACK WndProc(HWND hwnd, UINT message,
1063 WPARAM wParam, LPARAM lParam)
1066 static int menuinprogress;
1070 if (lParam == WM_RBUTTONUP) {
1072 GetCursorPos(&cursorpos);
1073 PostMessage(hwnd, WM_SYSTRAY2, cursorpos.x, cursorpos.y);
1074 } else if (lParam == WM_LBUTTONDBLCLK) {
1075 /* Equivalent to IDM_VIEWKEYS. */
1076 PostMessage(hwnd, WM_COMMAND, IDM_VIEWKEYS, 0);
1080 if (!menuinprogress) {
1082 SetForegroundWindow(hwnd);
1083 ret = TrackPopupMenu(systray_menu,
1084 TPM_RIGHTALIGN | TPM_BOTTOMALIGN |
1086 wParam, lParam, 0, hwnd, NULL);
1092 switch (wParam & ~0xF) { /* low 4 bits reserved to Windows */
1094 SendMessage(hwnd, WM_CLOSE, 0, 0);
1098 keylist = CreateDialog(instance, MAKEINTRESOURCE(211),
1100 ShowWindow(keylist, SW_SHOWNORMAL);
1102 * Sometimes the window comes up minimised / hidden
1103 * for no obvious reason. Prevent this.
1105 SetForegroundWindow(keylist);
1106 SetWindowPos(keylist, HWND_TOP, 0, 0, 0, 0,
1107 SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
1111 prompt_add_keyfile();
1115 aboutbox = CreateDialog(instance, MAKEINTRESOURCE(213),
1117 ShowWindow(aboutbox, SW_SHOWNORMAL);
1119 * Sometimes the window comes up minimised / hidden
1120 * for no obvious reason. Prevent this.
1122 SetForegroundWindow(aboutbox);
1123 SetWindowPos(aboutbox, HWND_TOP, 0, 0, 0, 0,
1124 SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
1134 COPYDATASTRUCT *cds;
1140 PSID mapowner, procowner;
1141 PSECURITY_DESCRIPTOR psd1 = NULL, psd2 = NULL;
1145 cds = (COPYDATASTRUCT *) lParam;
1146 if (cds->dwData != AGENT_COPYDATA_ID)
1147 return 0; /* not our message, mate */
1148 mapname = (char *) cds->lpData;
1149 if (mapname[cds->cbData - 1] != '\0')
1150 return 0; /* failure to be ASCIZ! */
1152 debug(("mapname is :%s:\n", mapname));
1154 filemap = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, mapname);
1156 debug(("filemap is %p\n", filemap));
1158 if (filemap != NULL && filemap != INVALID_HANDLE_VALUE) {
1162 if ((proc = OpenProcess(MAXIMUM_ALLOWED, FALSE,
1163 GetCurrentProcessId())) ==
1166 debug(("couldn't get handle for process\n"));
1170 if (getsecurityinfo(proc, SE_KERNEL_OBJECT,
1171 OWNER_SECURITY_INFORMATION,
1172 &procowner, NULL, NULL, NULL,
1173 &psd2) != ERROR_SUCCESS) {
1175 debug(("couldn't get owner info for process\n"));
1178 return 0; /* unable to get security info */
1181 if ((rc = getsecurityinfo(filemap, SE_KERNEL_OBJECT,
1182 OWNER_SECURITY_INFORMATION,
1183 &mapowner, NULL, NULL, NULL,
1184 &psd1) != ERROR_SUCCESS)) {
1187 ("couldn't get owner info for filemap: %d\n",
1193 debug(("got security stuff\n"));
1195 if (!EqualSid(mapowner, procowner))
1196 return 0; /* security ID mismatch! */
1198 debug(("security stuff matched\n"));
1204 debug(("security APIs not present\n"));
1208 p = MapViewOfFile(filemap, FILE_MAP_WRITE, 0, 0, 0);
1210 debug(("p is %p\n", p));
1213 for (i = 0; i < 5; i++)
1216 ((unsigned char *) p)[i]));}
1222 CloseHandle(filemap);
1227 return DefWindowProc(hwnd, message, wParam, lParam);
1231 * Fork and Exec the command in cmdline. [DBW]
1233 void spawn_cmd(char *cmdline, int show)
1235 if (ShellExecute(NULL, _T("open"), cmdline,
1236 NULL, NULL, show) <= (HINSTANCE) 32) {
1238 sprintf(sMsg, _T("Failed to run \"%.100s\", Error: %d"), cmdline,
1239 (int)GetLastError());
1240 MessageBox(NULL, sMsg, APPNAME, MB_OK | MB_ICONEXCLAMATION);
1244 int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
1250 char *command = NULL;
1254 * Determine whether we're an NT system (should have security
1255 * APIs) or a non-NT system (don't do security).
1257 memset(&osi, 0, sizeof(OSVERSIONINFO));
1258 osi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
1259 if (GetVersionEx(&osi) && osi.dwPlatformId == VER_PLATFORM_WIN32_NT) {
1260 has_security = TRUE;
1262 has_security = FALSE;
1267 * Attempt to get the security API we need.
1269 advapi = LoadLibrary("ADVAPI32.DLL");
1271 (gsi_fn_t) GetProcAddress(advapi, "GetSecurityInfo");
1272 if (!getsecurityinfo) {
1274 "Unable to access security APIs. Pageant will\n"
1275 "not run, in case it causes a security breach.",
1276 "Pageant Fatal Error", MB_ICONERROR | MB_OK);
1281 "This program has been compiled for Win9X and will\n"
1282 "not run on NT, in case it causes a security breach.",
1283 "Pageant Fatal Error", MB_ICONERROR | MB_OK);
1292 * Find out if Pageant is already running.
1294 already_running = FALSE;
1296 already_running = TRUE;
1301 wndclass.lpfnWndProc = WndProc;
1302 wndclass.cbClsExtra = 0;
1303 wndclass.cbWndExtra = 0;
1304 wndclass.hInstance = inst;
1305 wndclass.hIcon = LoadIcon(inst, MAKEINTRESOURCE(IDI_MAINICON));
1306 wndclass.hCursor = LoadCursor(NULL, IDC_IBEAM);
1307 wndclass.hbrBackground = GetStockObject(BLACK_BRUSH);
1308 wndclass.lpszMenuName = NULL;
1309 wndclass.lpszClassName = APPNAME;
1311 RegisterClass(&wndclass);
1314 hwnd = keylist = NULL;
1316 hwnd = CreateWindow(APPNAME, APPNAME,
1317 WS_OVERLAPPEDWINDOW | WS_VSCROLL,
1318 CW_USEDEFAULT, CW_USEDEFAULT,
1319 100, 100, NULL, NULL, inst, NULL);
1321 /* Set up a system tray icon */
1324 NOTIFYICONDATA tnid;
1327 #ifdef NIM_SETVERSION
1329 res = Shell_NotifyIcon(NIM_SETVERSION, &tnid);
1332 tnid.cbSize = sizeof(NOTIFYICONDATA);
1334 tnid.uID = 1; /* unique within this systray use */
1335 tnid.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
1336 tnid.uCallbackMessage = WM_SYSTRAY;
1337 tnid.hIcon = hicon = LoadIcon(instance, MAKEINTRESOURCE(201));
1338 strcpy(tnid.szTip, "Pageant (PuTTY authentication agent)");
1340 res = Shell_NotifyIcon(NIM_ADD, &tnid);
1345 systray_menu = CreatePopupMenu();
1346 /* accelerators used: vkxa */
1347 AppendMenu(systray_menu, MF_ENABLED, IDM_VIEWKEYS,
1349 AppendMenu(systray_menu, MF_ENABLED, IDM_ADDKEY, "Add &Key");
1350 AppendMenu(systray_menu, MF_ENABLED, IDM_ABOUT, "&About");
1351 AppendMenu(systray_menu, MF_ENABLED, IDM_CLOSE, "E&xit");
1354 ShowWindow(hwnd, SW_HIDE);
1357 * Initialise storage for RSA keys.
1359 rsakeys = newtree234(cmpkeys_rsa);
1360 ssh2keys = newtree234(cmpkeys_ssh2);
1365 * Process the command line and add keys as listed on it.
1366 * If we already determined that we need to spawn a program from above we
1367 * need to ignore the first two arguments. [DBW]
1374 while (*p && isspace(*p))
1376 if (*p && !isspace(*p)) {
1377 char *q = p, *pp = p;
1378 while (*p && (inquotes || !isspace(*p))) {
1380 inquotes = !inquotes;
1391 if (!strcmp(q, "-c")) {
1393 * If we see `-c', then the rest of the
1394 * command line should be treated as a
1395 * command to be spawned.
1397 while (*p && isspace(*p))
1410 spawn_cmd(command, show);
1413 * If Pageant was already running, we leave now. If we haven't
1414 * even taken any auxiliary action (spawned a command or added
1417 if (already_running) {
1418 if (!command && !added_keys) {
1419 MessageBox(NULL, "Pageant is already running", "Pageant Error",
1420 MB_ICONERROR | MB_OK);
1423 FreeLibrary(advapi);
1428 * Main message loop.
1430 while (GetMessage(&msg, NULL, 0, 0) == 1) {
1431 TranslateMessage(&msg);
1432 DispatchMessage(&msg);
1435 /* Clean up the system tray icon */
1437 NOTIFYICONDATA tnid;
1439 tnid.cbSize = sizeof(NOTIFYICONDATA);
1443 Shell_NotifyIcon(NIM_DELETE, &tnid);
1445 DestroyMenu(systray_menu);
1449 FreeLibrary(advapi);