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);
171 static HWND passphrase_box;
174 * Dialog-box function for the passphrase box.
176 static int CALLBACK PassphraseProc(HWND hwnd, UINT msg,
177 WPARAM wParam, LPARAM lParam)
179 static char *passphrase = NULL;
180 struct PassphraseProcStruct *p;
184 passphrase_box = hwnd;
188 { /* centre the window */
192 hw = GetDesktopWindow();
193 if (GetWindowRect(hw, &rs) && GetWindowRect(hwnd, &rd))
195 (rs.right + rs.left + rd.left - rd.right) / 2,
196 (rs.bottom + rs.top + rd.top - rd.bottom) / 2,
197 rd.right - rd.left, rd.bottom - rd.top, TRUE);
200 SetForegroundWindow(hwnd);
201 SetWindowPos(hwnd, HWND_TOP, 0, 0, 0, 0,
202 SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
203 p = (struct PassphraseProcStruct *) lParam;
204 passphrase = p->passphrase;
206 SetDlgItemText(hwnd, 101, p->comment);
208 SetDlgItemText(hwnd, 102, passphrase);
211 switch (LOWORD(wParam)) {
221 case 102: /* edit box */
222 if ((HIWORD(wParam) == EN_CHANGE) && passphrase) {
223 GetDlgItemText(hwnd, 102, passphrase,
224 PASSPHRASE_MAXLEN - 1);
225 passphrase[PASSPHRASE_MAXLEN - 1] = '\0';
238 * Update the visible key list.
240 static void keylist_update(void)
243 struct ssh2_userkey *skey;
247 SendDlgItemMessage(keylist, 100, LB_RESETCONTENT, 0, 0);
248 for (i = 0; NULL != (rkey = index234(rsakeys, i)); i++) {
249 char listentry[512], *p;
251 * Replace two spaces in the fingerprint with tabs, for
252 * nice alignment in the box.
254 strcpy(listentry, "ssh1\t");
255 p = listentry + strlen(listentry);
256 rsa_fingerprint(p, sizeof(listentry) - (p - listentry), rkey);
257 p = strchr(listentry, ' ');
260 p = strchr(listentry, ' ');
263 SendDlgItemMessage(keylist, 100, LB_ADDSTRING,
264 0, (LPARAM) listentry);
266 for (i = 0; NULL != (skey = index234(ssh2keys, i)); i++) {
267 char listentry[512], *p;
270 * Replace two spaces in the fingerprint with tabs, for
271 * nice alignment in the box.
273 p = skey->alg->fingerprint(skey->data);
274 strncpy(listentry, p, sizeof(listentry));
275 p = strchr(listentry, ' ');
278 p = strchr(listentry, ' ');
281 len = strlen(listentry);
282 if (len < sizeof(listentry) - 2) {
283 listentry[len] = '\t';
284 strncpy(listentry + len + 1, skey->comment,
285 sizeof(listentry) - len - 1);
287 SendDlgItemMessage(keylist, 100, LB_ADDSTRING, 0,
290 SendDlgItemMessage(keylist, 100, LB_SETCURSEL, (WPARAM) - 1, 0);
295 * This function loads a key from a file and adds it.
297 static void add_keyfile(char *filename)
299 char passphrase[PASSPHRASE_MAXLEN];
300 struct RSAKey *rkey = NULL;
301 struct ssh2_userkey *skey = NULL;
306 struct PassphraseProcStruct pps;
309 ver = keyfile_version(filename);
311 MessageBox(NULL, "Couldn't load private key.", APPNAME,
312 MB_OK | MB_ICONERROR);
317 needs_pass = rsakey_encrypted(filename, &comment);
319 needs_pass = ssh2_userkey_encrypted(filename, &comment);
322 rkey = smalloc(sizeof(*rkey));
323 pps.passphrase = passphrase;
324 pps.comment = comment;
328 dlgret = DialogBoxParam(instance, MAKEINTRESOURCE(210),
329 NULL, PassphraseProc, (LPARAM) & pps);
330 passphrase_box = NULL;
336 return; /* operation cancelled */
341 ret = loadrsakey(filename, rkey, passphrase);
343 skey = ssh2_load_userkey(filename, passphrase);
344 if (skey == SSH2_WRONG_PASSPHRASE)
356 MessageBox(NULL, "Couldn't load private key.", APPNAME,
357 MB_OK | MB_ICONERROR);
363 if (already_running) {
364 unsigned char *request, *response;
366 int reqlen, clen, resplen;
368 clen = strlen(rkey->comment);
370 reqlen = 4 + 1 + /* length, message type */
372 ssh1_bignum_length(rkey->modulus) +
373 ssh1_bignum_length(rkey->exponent) +
374 ssh1_bignum_length(rkey->private_exponent) +
375 ssh1_bignum_length(rkey->iqmp) +
376 ssh1_bignum_length(rkey->p) +
377 ssh1_bignum_length(rkey->q) + 4 + clen /* comment */
380 request = smalloc(reqlen);
382 request[4] = SSH1_AGENTC_ADD_RSA_IDENTITY;
384 PUT_32BIT(request + reqlen, bignum_bitcount(rkey->modulus));
386 reqlen += ssh1_write_bignum(request + reqlen, rkey->modulus);
387 reqlen += ssh1_write_bignum(request + reqlen, rkey->exponent);
389 ssh1_write_bignum(request + reqlen,
390 rkey->private_exponent);
391 reqlen += ssh1_write_bignum(request + reqlen, rkey->iqmp);
392 reqlen += ssh1_write_bignum(request + reqlen, rkey->p);
393 reqlen += ssh1_write_bignum(request + reqlen, rkey->q);
394 PUT_32BIT(request + reqlen, clen);
395 memcpy(request + reqlen + 4, rkey->comment, clen);
397 PUT_32BIT(request, reqlen - 4);
399 agent_query(request, reqlen, &vresponse, &resplen);
400 response = vresponse;
401 if (resplen < 5 || response[4] != SSH_AGENT_SUCCESS)
402 MessageBox(NULL, "The already running Pageant "
403 "refused to add the key.", APPNAME,
404 MB_OK | MB_ICONERROR);
406 if (add234(rsakeys, rkey) != rkey)
407 sfree(rkey); /* already present, don't waste RAM */
410 if (already_running) {
411 unsigned char *request, *response;
413 int reqlen, alglen, clen, keybloblen, resplen;
414 alglen = strlen(skey->alg->name);
415 clen = strlen(skey->comment);
417 keybloblen = skey->alg->openssh_fmtkey(skey->data, NULL, 0);
419 reqlen = 4 + 1 + /* length, message type */
420 4 + alglen + /* algorithm name */
421 keybloblen + /* key data */
422 4 + clen /* comment */
425 request = smalloc(reqlen);
427 request[4] = SSH2_AGENTC_ADD_IDENTITY;
429 PUT_32BIT(request + reqlen, alglen);
431 memcpy(request + reqlen, skey->alg->name, alglen);
433 reqlen += skey->alg->openssh_fmtkey(skey->data,
436 PUT_32BIT(request + reqlen, clen);
437 memcpy(request + reqlen + 4, skey->comment, clen);
438 PUT_32BIT(request, reqlen - 4);
441 agent_query(request, reqlen, &vresponse, &resplen);
442 response = vresponse;
443 if (resplen < 5 || response[4] != SSH_AGENT_SUCCESS)
444 MessageBox(NULL, "The already running Pageant"
445 "refused to add the key.", APPNAME,
446 MB_OK | MB_ICONERROR);
448 if (add234(ssh2keys, skey) != skey) {
449 skey->alg->freekey(skey->data);
450 sfree(skey); /* already present, don't waste RAM */
457 * This is the main agent function that answers messages.
459 static void answer_msg(void *msg)
461 unsigned char *p = msg;
462 unsigned char *ret = msg;
466 * Get the message type.
472 case SSH1_AGENTC_REQUEST_RSA_IDENTITIES:
474 * Reply with SSH1_AGENT_RSA_IDENTITIES_ANSWER.
482 * Count up the number and length of keys we hold.
485 for (i = 0; NULL != (key = index234(rsakeys, i)); i++) {
487 len += 4; /* length field */
488 len += ssh1_bignum_length(key->exponent);
489 len += ssh1_bignum_length(key->modulus);
490 len += 4 + strlen(key->comment);
494 * Packet header is the obvious five bytes, plus four
495 * bytes for the key count.
498 if (len > AGENT_MAX_MSGLEN)
499 goto failure; /* aaargh! too much stuff! */
500 PUT_32BIT(ret, len - 4);
501 ret[4] = SSH1_AGENT_RSA_IDENTITIES_ANSWER;
502 PUT_32BIT(ret + 5, nkeys);
504 for (i = 0; NULL != (key = index234(rsakeys, i)); i++) {
505 PUT_32BIT(p, bignum_bitcount(key->modulus));
507 p += ssh1_write_bignum(p, key->exponent);
508 p += ssh1_write_bignum(p, key->modulus);
509 PUT_32BIT(p, strlen(key->comment));
510 memcpy(p + 4, key->comment, strlen(key->comment));
511 p += 4 + strlen(key->comment);
515 case SSH2_AGENTC_REQUEST_IDENTITIES:
517 * Reply with SSH2_AGENT_IDENTITIES_ANSWER.
520 struct ssh2_userkey *key;
527 * Count up the number and length of keys we hold.
530 for (i = 0; NULL != (key = index234(ssh2keys, i)); i++) {
532 len += 4; /* length field */
533 blob = key->alg->public_blob(key->data, &bloblen);
536 len += 4 + strlen(key->comment);
540 * Packet header is the obvious five bytes, plus four
541 * bytes for the key count.
544 if (len > AGENT_MAX_MSGLEN)
545 goto failure; /* aaargh! too much stuff! */
546 PUT_32BIT(ret, len - 4);
547 ret[4] = SSH2_AGENT_IDENTITIES_ANSWER;
548 PUT_32BIT(ret + 5, nkeys);
550 for (i = 0; NULL != (key = index234(ssh2keys, i)); i++) {
551 blob = key->alg->public_blob(key->data, &bloblen);
552 PUT_32BIT(p, bloblen);
554 memcpy(p, blob, bloblen);
557 PUT_32BIT(p, strlen(key->comment));
558 memcpy(p + 4, key->comment, strlen(key->comment));
559 p += 4 + strlen(key->comment);
563 case SSH1_AGENTC_RSA_CHALLENGE:
565 * Reply with either SSH1_AGENT_RSA_RESPONSE or
566 * SSH_AGENT_FAILURE, depending on whether we have that key
570 struct RSAKey reqkey, *key;
571 Bignum challenge, response;
572 unsigned char response_source[48], response_md5[16];
573 struct MD5Context md5c;
577 p += ssh1_read_bignum(p, &reqkey.exponent);
578 p += ssh1_read_bignum(p, &reqkey.modulus);
579 p += ssh1_read_bignum(p, &challenge);
580 memcpy(response_source + 32, p, 16);
582 if (GET_32BIT(p) != 1 ||
583 (key = find234(rsakeys, &reqkey, NULL)) == NULL) {
584 freebn(reqkey.exponent);
585 freebn(reqkey.modulus);
589 response = rsadecrypt(challenge, key);
590 for (i = 0; i < 32; i++)
591 response_source[i] = bignum_byte(response, 31 - i);
594 MD5Update(&md5c, response_source, 48);
595 MD5Final(response_md5, &md5c);
596 memset(response_source, 0, 48); /* burn the evidence */
597 freebn(response); /* and that evidence */
598 freebn(challenge); /* yes, and that evidence */
599 freebn(reqkey.exponent); /* and free some memory ... */
600 freebn(reqkey.modulus); /* ... while we're at it. */
603 * Packet is the obvious five byte header, plus sixteen
607 PUT_32BIT(ret, len - 4);
608 ret[4] = SSH1_AGENT_RSA_RESPONSE;
609 memcpy(ret + 5, response_md5, 16);
612 case SSH2_AGENTC_SIGN_REQUEST:
614 * Reply with either SSH2_AGENT_RSA_RESPONSE or
615 * SSH_AGENT_FAILURE, depending on whether we have that key
619 struct ssh2_userkey *key;
621 unsigned char *data, *signature;
622 int datalen, siglen, len;
624 b.len = GET_32BIT(p);
628 datalen = GET_32BIT(p);
631 key = find234(ssh2keys, &b, cmpkeys_ssh2_asymm);
634 signature = key->alg->sign(key->data, data, datalen, &siglen);
635 len = 5 + 4 + siglen;
636 PUT_32BIT(ret, len - 4);
637 ret[4] = SSH2_AGENT_SIGN_RESPONSE;
638 PUT_32BIT(ret + 5, siglen);
639 memcpy(ret + 5 + 4, signature, siglen);
643 case SSH1_AGENTC_ADD_RSA_IDENTITY:
645 * Add to the list and return SSH_AGENT_SUCCESS, or
646 * SSH_AGENT_FAILURE if the key was malformed.
651 key = smalloc(sizeof(struct RSAKey));
652 memset(key, 0, sizeof(key));
653 p += makekey(p, key, NULL, 1);
654 p += makeprivate(p, key);
655 p += ssh1_read_bignum(p, key->iqmp); /* p^-1 mod q */
656 p += ssh1_read_bignum(p, key->p); /* p */
657 p += ssh1_read_bignum(p, key->q); /* q */
658 comment = smalloc(GET_32BIT(p));
660 memcpy(comment, p + 4, GET_32BIT(p));
661 key->comment = comment;
664 ret[4] = SSH_AGENT_FAILURE;
665 if (add234(rsakeys, key) == key) {
667 ret[4] = SSH_AGENT_SUCCESS;
674 case SSH2_AGENTC_ADD_IDENTITY:
676 * Add to the list and return SSH_AGENT_SUCCESS, or
677 * SSH_AGENT_FAILURE if the key was malformed.
680 struct ssh2_userkey *key;
685 key = smalloc(sizeof(struct ssh2_userkey));
687 alglen = GET_32BIT(p);
691 /* Add further algorithm names here. */
692 if (alglen == 7 && !memcmp(alg, "ssh-rsa", 7))
700 GET_32BIT((unsigned char *) msg) - (p -
701 (unsigned char *) msg -
703 key->data = key->alg->openssh_createkey(&p, &bloblen);
708 commlen = GET_32BIT(p);
711 comment = smalloc(commlen + 1);
713 memcpy(comment, p, commlen);
714 comment[commlen] = '\0';
716 key->comment = comment;
719 ret[4] = SSH_AGENT_FAILURE;
720 if (add234(ssh2keys, key) == key) {
722 ret[4] = SSH_AGENT_SUCCESS;
724 key->alg->freekey(key->data);
730 case SSH1_AGENTC_REMOVE_RSA_IDENTITY:
732 * Remove from the list and return SSH_AGENT_SUCCESS, or
733 * perhaps SSH_AGENT_FAILURE if it wasn't in the list to
737 struct RSAKey reqkey, *key;
739 p += makekey(p, &reqkey, NULL, 0);
740 key = find234(rsakeys, &reqkey, NULL);
741 freebn(reqkey.exponent);
742 freebn(reqkey.modulus);
744 ret[4] = SSH_AGENT_FAILURE;
746 del234(rsakeys, key);
750 ret[4] = SSH_AGENT_SUCCESS;
754 case SSH2_AGENTC_REMOVE_IDENTITY:
756 * Remove from the list and return SSH_AGENT_SUCCESS, or
757 * perhaps SSH_AGENT_FAILURE if it wasn't in the list to
761 struct ssh2_userkey *key;
764 b.len = GET_32BIT(p);
768 key = find234(ssh2keys, &b, cmpkeys_ssh2_asymm);
773 ret[4] = SSH_AGENT_FAILURE;
775 del234(ssh2keys, key);
777 key->alg->freekey(key->data);
779 ret[4] = SSH_AGENT_SUCCESS;
783 case SSH1_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
785 * Remove all SSH1 keys. Always returns success.
790 while ((rkey = index234(rsakeys, 0)) != NULL) {
791 del234(rsakeys, rkey);
798 ret[4] = SSH_AGENT_SUCCESS;
801 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
803 * Remove all SSH2 keys. Always returns success.
806 struct ssh2_userkey *skey;
808 while ((skey = index234(ssh2keys, 0)) != NULL) {
809 del234(ssh2keys, skey);
810 skey->alg->freekey(skey->data);
816 ret[4] = SSH_AGENT_SUCCESS;
822 * Unrecognised message. Return SSH_AGENT_FAILURE.
825 ret[4] = SSH_AGENT_FAILURE;
831 * Key comparison function for the 2-3-4 tree of RSA keys.
833 static int cmpkeys_rsa(void *av, void *bv)
835 struct RSAKey *a = (struct RSAKey *) av;
836 struct RSAKey *b = (struct RSAKey *) bv;
843 * Compare by length of moduli.
845 alen = bignum_bitcount(am);
846 blen = bignum_bitcount(bm);
849 else if (alen < blen)
852 * Now compare by moduli themselves.
854 alen = (alen + 7) / 8; /* byte count */
857 abyte = bignum_byte(am, alen);
858 bbyte = bignum_byte(bm, alen);
861 else if (abyte < bbyte)
871 * Key comparison function for the 2-3-4 tree of SSH2 keys.
873 static int cmpkeys_ssh2(void *av, void *bv)
875 struct ssh2_userkey *a = (struct ssh2_userkey *) av;
876 struct ssh2_userkey *b = (struct ssh2_userkey *) bv;
879 unsigned char *ablob, *bblob;
883 * Compare purely by public blob.
885 ablob = a->alg->public_blob(a->data, &alen);
886 bblob = b->alg->public_blob(b->data, &blen);
889 for (i = 0; i < alen && i < blen; i++) {
890 if (ablob[i] < bblob[i]) {
893 } else if (ablob[i] > bblob[i]) {
898 if (c == 0 && i < alen)
899 c = +1; /* a is longer */
900 if (c == 0 && i < blen)
901 c = -1; /* a is longer */
910 * Key comparison function for looking up a blob in the 2-3-4 tree
913 static int cmpkeys_ssh2_asymm(void *av, void *bv)
915 struct blob *a = (struct blob *) av;
916 struct ssh2_userkey *b = (struct ssh2_userkey *) bv;
919 unsigned char *ablob, *bblob;
923 * Compare purely by public blob.
927 bblob = b->alg->public_blob(b->data, &blen);
930 for (i = 0; i < alen && i < blen; i++) {
931 if (ablob[i] < bblob[i]) {
934 } else if (ablob[i] > bblob[i]) {
939 if (c == 0 && i < alen)
940 c = +1; /* a is longer */
941 if (c == 0 && i < blen)
942 c = -1; /* a is longer */
950 * Prompt for a key file to add, and add it.
952 static void prompt_add_keyfile(void)
955 char filename[FILENAME_MAX];
956 memset(&of, 0, sizeof(of));
957 #ifdef OPENFILENAME_SIZE_VERSION_400
958 of.lStructSize = OPENFILENAME_SIZE_VERSION_400;
960 of.lStructSize = sizeof(of);
963 of.lpstrFilter = "All Files\0*\0\0\0";
964 of.lpstrCustomFilter = NULL;
966 of.lpstrFile = filename;
968 of.nMaxFile = sizeof(filename);
969 of.lpstrFileTitle = NULL;
970 of.lpstrInitialDir = NULL;
971 of.lpstrTitle = "Select Private Key File";
973 if (GetOpenFileName(&of)) {
974 add_keyfile(filename);
980 * Dialog-box function for the key list box.
982 static int CALLBACK KeyListProc(HWND hwnd, UINT msg,
983 WPARAM wParam, LPARAM lParam)
986 struct ssh2_userkey *skey;
993 { /* centre the window */
997 hw = GetDesktopWindow();
998 if (GetWindowRect(hw, &rs) && GetWindowRect(hwnd, &rd))
1000 (rs.right + rs.left + rd.left - rd.right) / 2,
1001 (rs.bottom + rs.top + rd.top - rd.bottom) / 2,
1002 rd.right - rd.left, rd.bottom - rd.top, TRUE);
1007 static int tabs[] = { 35, 60, 210 };
1008 SendDlgItemMessage(hwnd, 100, LB_SETTABSTOPS,
1009 sizeof(tabs) / sizeof(*tabs),
1015 switch (LOWORD(wParam)) {
1019 DestroyWindow(hwnd);
1021 case 101: /* add key */
1022 if (HIWORD(wParam) == BN_CLICKED ||
1023 HIWORD(wParam) == BN_DOUBLECLICKED) {
1024 if (passphrase_box) {
1025 MessageBeep(MB_ICONERROR);
1026 SetForegroundWindow(passphrase_box);
1029 prompt_add_keyfile();
1032 case 102: /* remove key */
1033 if (HIWORD(wParam) == BN_CLICKED ||
1034 HIWORD(wParam) == BN_DOUBLECLICKED) {
1035 int n = SendDlgItemMessage(hwnd, 100, LB_GETCURSEL, 0, 0);
1041 for (i = 0; NULL != (rkey = index234(rsakeys, i)); i++)
1045 del234(rsakeys, rkey);
1049 for (i = 0; NULL != (skey = index234(ssh2keys, i));
1053 del234(ssh2keys, skey);
1054 skey->alg->freekey(skey->data);
1065 DestroyWindow(hwnd);
1071 static LRESULT CALLBACK WndProc(HWND hwnd, UINT message,
1072 WPARAM wParam, LPARAM lParam)
1075 static int menuinprogress;
1079 if (lParam == WM_RBUTTONUP) {
1081 GetCursorPos(&cursorpos);
1082 PostMessage(hwnd, WM_SYSTRAY2, cursorpos.x, cursorpos.y);
1083 } else if (lParam == WM_LBUTTONDBLCLK) {
1084 /* Equivalent to IDM_VIEWKEYS. */
1085 PostMessage(hwnd, WM_COMMAND, IDM_VIEWKEYS, 0);
1089 if (!menuinprogress) {
1091 SetForegroundWindow(hwnd);
1092 ret = TrackPopupMenu(systray_menu,
1093 TPM_RIGHTALIGN | TPM_BOTTOMALIGN |
1095 wParam, lParam, 0, hwnd, NULL);
1101 switch (wParam & ~0xF) { /* low 4 bits reserved to Windows */
1104 SendMessage(passphrase_box, WM_CLOSE, 0, 0);
1105 SendMessage(hwnd, WM_CLOSE, 0, 0);
1109 keylist = CreateDialog(instance, MAKEINTRESOURCE(211),
1111 ShowWindow(keylist, SW_SHOWNORMAL);
1113 * Sometimes the window comes up minimised / hidden
1114 * for no obvious reason. Prevent this.
1116 SetForegroundWindow(keylist);
1117 SetWindowPos(keylist, HWND_TOP, 0, 0, 0, 0,
1118 SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
1122 if (passphrase_box) {
1123 MessageBeep(MB_ICONERROR);
1124 SetForegroundWindow(passphrase_box);
1127 prompt_add_keyfile();
1131 aboutbox = CreateDialog(instance, MAKEINTRESOURCE(213),
1133 ShowWindow(aboutbox, SW_SHOWNORMAL);
1135 * Sometimes the window comes up minimised / hidden
1136 * for no obvious reason. Prevent this.
1138 SetForegroundWindow(aboutbox);
1139 SetWindowPos(aboutbox, HWND_TOP, 0, 0, 0, 0,
1140 SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
1150 COPYDATASTRUCT *cds;
1156 PSID mapowner, procowner;
1157 PSECURITY_DESCRIPTOR psd1 = NULL, psd2 = NULL;
1161 cds = (COPYDATASTRUCT *) lParam;
1162 if (cds->dwData != AGENT_COPYDATA_ID)
1163 return 0; /* not our message, mate */
1164 mapname = (char *) cds->lpData;
1165 if (mapname[cds->cbData - 1] != '\0')
1166 return 0; /* failure to be ASCIZ! */
1168 debug(("mapname is :%s:\n", mapname));
1170 filemap = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, mapname);
1172 debug(("filemap is %p\n", filemap));
1174 if (filemap != NULL && filemap != INVALID_HANDLE_VALUE) {
1178 if ((proc = OpenProcess(MAXIMUM_ALLOWED, FALSE,
1179 GetCurrentProcessId())) ==
1182 debug(("couldn't get handle for process\n"));
1186 if (getsecurityinfo(proc, SE_KERNEL_OBJECT,
1187 OWNER_SECURITY_INFORMATION,
1188 &procowner, NULL, NULL, NULL,
1189 &psd2) != ERROR_SUCCESS) {
1191 debug(("couldn't get owner info for process\n"));
1194 return 0; /* unable to get security info */
1197 if ((rc = getsecurityinfo(filemap, SE_KERNEL_OBJECT,
1198 OWNER_SECURITY_INFORMATION,
1199 &mapowner, NULL, NULL, NULL,
1200 &psd1) != ERROR_SUCCESS)) {
1203 ("couldn't get owner info for filemap: %d\n",
1209 debug(("got security stuff\n"));
1211 if (!EqualSid(mapowner, procowner))
1212 return 0; /* security ID mismatch! */
1214 debug(("security stuff matched\n"));
1220 debug(("security APIs not present\n"));
1224 p = MapViewOfFile(filemap, FILE_MAP_WRITE, 0, 0, 0);
1226 debug(("p is %p\n", p));
1229 for (i = 0; i < 5; i++)
1232 ((unsigned char *) p)[i]));}
1238 CloseHandle(filemap);
1243 return DefWindowProc(hwnd, message, wParam, lParam);
1247 * Fork and Exec the command in cmdline. [DBW]
1249 void spawn_cmd(char *cmdline, int show)
1251 if (ShellExecute(NULL, _T("open"), cmdline,
1252 NULL, NULL, show) <= (HINSTANCE) 32) {
1254 sprintf(sMsg, _T("Failed to run \"%.100s\", Error: %d"), cmdline,
1255 (int)GetLastError());
1256 MessageBox(NULL, sMsg, APPNAME, MB_OK | MB_ICONEXCLAMATION);
1260 int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
1266 char *command = NULL;
1270 * Determine whether we're an NT system (should have security
1271 * APIs) or a non-NT system (don't do security).
1273 memset(&osi, 0, sizeof(OSVERSIONINFO));
1274 osi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
1275 if (GetVersionEx(&osi) && osi.dwPlatformId == VER_PLATFORM_WIN32_NT) {
1276 has_security = TRUE;
1278 has_security = FALSE;
1283 * Attempt to get the security API we need.
1285 advapi = LoadLibrary("ADVAPI32.DLL");
1287 (gsi_fn_t) GetProcAddress(advapi, "GetSecurityInfo");
1288 if (!getsecurityinfo) {
1290 "Unable to access security APIs. Pageant will\n"
1291 "not run, in case it causes a security breach.",
1292 "Pageant Fatal Error", MB_ICONERROR | MB_OK);
1297 "This program has been compiled for Win9X and will\n"
1298 "not run on NT, in case it causes a security breach.",
1299 "Pageant Fatal Error", MB_ICONERROR | MB_OK);
1308 * Find out if Pageant is already running.
1310 already_running = FALSE;
1312 already_running = TRUE;
1317 wndclass.lpfnWndProc = WndProc;
1318 wndclass.cbClsExtra = 0;
1319 wndclass.cbWndExtra = 0;
1320 wndclass.hInstance = inst;
1321 wndclass.hIcon = LoadIcon(inst, MAKEINTRESOURCE(IDI_MAINICON));
1322 wndclass.hCursor = LoadCursor(NULL, IDC_IBEAM);
1323 wndclass.hbrBackground = GetStockObject(BLACK_BRUSH);
1324 wndclass.lpszMenuName = NULL;
1325 wndclass.lpszClassName = APPNAME;
1327 RegisterClass(&wndclass);
1330 hwnd = keylist = NULL;
1332 hwnd = CreateWindow(APPNAME, APPNAME,
1333 WS_OVERLAPPEDWINDOW | WS_VSCROLL,
1334 CW_USEDEFAULT, CW_USEDEFAULT,
1335 100, 100, NULL, NULL, inst, NULL);
1337 /* Set up a system tray icon */
1340 NOTIFYICONDATA tnid;
1343 #ifdef NIM_SETVERSION
1345 res = Shell_NotifyIcon(NIM_SETVERSION, &tnid);
1348 tnid.cbSize = sizeof(NOTIFYICONDATA);
1350 tnid.uID = 1; /* unique within this systray use */
1351 tnid.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
1352 tnid.uCallbackMessage = WM_SYSTRAY;
1353 tnid.hIcon = hicon = LoadIcon(instance, MAKEINTRESOURCE(201));
1354 strcpy(tnid.szTip, "Pageant (PuTTY authentication agent)");
1356 res = Shell_NotifyIcon(NIM_ADD, &tnid);
1361 systray_menu = CreatePopupMenu();
1362 /* accelerators used: vkxa */
1363 AppendMenu(systray_menu, MF_ENABLED, IDM_VIEWKEYS,
1365 AppendMenu(systray_menu, MF_ENABLED, IDM_ADDKEY, "Add &Key");
1366 AppendMenu(systray_menu, MF_ENABLED, IDM_ABOUT, "&About");
1367 AppendMenu(systray_menu, MF_ENABLED, IDM_CLOSE, "E&xit");
1370 ShowWindow(hwnd, SW_HIDE);
1373 * Initialise storage for RSA keys.
1375 rsakeys = newtree234(cmpkeys_rsa);
1376 ssh2keys = newtree234(cmpkeys_ssh2);
1381 * Process the command line and add keys as listed on it.
1382 * If we already determined that we need to spawn a program from above we
1383 * need to ignore the first two arguments. [DBW]
1390 while (*p && isspace(*p))
1392 if (*p && !isspace(*p)) {
1393 char *q = p, *pp = p;
1394 while (*p && (inquotes || !isspace(*p))) {
1396 inquotes = !inquotes;
1407 if (!strcmp(q, "-c")) {
1409 * If we see `-c', then the rest of the
1410 * command line should be treated as a
1411 * command to be spawned.
1413 while (*p && isspace(*p))
1426 spawn_cmd(command, show);
1429 * If Pageant was already running, we leave now. If we haven't
1430 * even taken any auxiliary action (spawned a command or added
1433 if (already_running) {
1434 if (!command && !added_keys) {
1435 MessageBox(NULL, "Pageant is already running", "Pageant Error",
1436 MB_ICONERROR | MB_OK);
1439 FreeLibrary(advapi);
1444 * Main message loop.
1446 while (GetMessage(&msg, NULL, 0, 0) == 1) {
1447 TranslateMessage(&msg);
1448 DispatchMessage(&msg);
1451 /* Clean up the system tray icon */
1453 NOTIFYICONDATA tnid;
1455 tnid.cbSize = sizeof(NOTIFYICONDATA);
1459 Shell_NotifyIcon(NIM_DELETE, &tnid);
1461 DestroyMenu(systray_menu);
1465 FreeLibrary(advapi);