2 * Pageant: the PuTTY Authentication Agent.
17 #define IDI_MAINICON 200
18 #define IDI_TRAYICON 201
20 #define WM_XUSER (WM_USER + 0x2000)
21 #define WM_SYSTRAY (WM_XUSER + 6)
22 #define WM_SYSTRAY2 (WM_XUSER + 7)
24 #define AGENT_COPYDATA_ID 0x804e50ba /* random goop */
27 * FIXME: maybe some day we can sort this out ...
29 #define AGENT_MAX_MSGLEN 8192
31 #define IDM_CLOSE 0x0010
32 #define IDM_VIEWKEYS 0x0020
33 #define IDM_ADDKEY 0x0030
34 #define IDM_ABOUT 0x0040
36 #define APPNAME "Pageant"
40 static HINSTANCE instance;
44 static HMENU systray_menu;
45 static int already_running;
47 static tree234 *rsakeys, *ssh2keys;
49 static int has_security;
51 typedef DWORD (WINAPI *gsi_fn_t)
52 (HANDLE, SE_OBJECT_TYPE, SECURITY_INFORMATION,
53 PSID *, PSID *, PACL *, PACL *,
54 PSECURITY_DESCRIPTOR *);
55 static gsi_fn_t getsecurityinfo;
59 * We need this to link with the RSA code, because rsaencrypt()
60 * pads its data with random bytes. Since we only use rsadecrypt()
61 * and the signing functions, which are deterministic, this should
64 * If it _is_ called, there is a _serious_ problem, because it
65 * won't generate true random numbers. So we must scream, panic,
66 * and exit immediately if that should happen.
68 int random_byte(void) {
69 MessageBox(hwnd, "Internal Error", APPNAME, MB_OK | MB_ICONERROR);
74 * Blob structure for passing to the asymmetric SSH2 key compare
75 * function, prototyped here.
81 static int cmpkeys_ssh2_asymm(void *av, void *bv);
84 * This function is needed to link with the DES code. We need not
85 * have it do anything at all.
87 void logevent(char *msg) {
90 #define GET_32BIT(cp) \
91 (((unsigned long)(unsigned char)(cp)[0] << 24) | \
92 ((unsigned long)(unsigned char)(cp)[1] << 16) | \
93 ((unsigned long)(unsigned char)(cp)[2] << 8) | \
94 ((unsigned long)(unsigned char)(cp)[3]))
96 #define PUT_32BIT(cp, value) { \
97 (cp)[0] = (unsigned char)((value) >> 24); \
98 (cp)[1] = (unsigned char)((value) >> 16); \
99 (cp)[2] = (unsigned char)((value) >> 8); \
100 (cp)[3] = (unsigned char)(value); }
102 #define PASSPHRASE_MAXLEN 512
104 struct PassphraseProcStruct {
110 * Dialog-box function for the Licence box.
112 static int CALLBACK LicenceProc (HWND hwnd, UINT msg,
113 WPARAM wParam, LPARAM lParam) {
118 switch (LOWORD(wParam)) {
132 * Dialog-box function for the About box.
134 static int CALLBACK AboutProc (HWND hwnd, UINT msg,
135 WPARAM wParam, LPARAM lParam) {
138 SetDlgItemText (hwnd, 100, ver);
141 switch (LOWORD(wParam)) {
144 DestroyWindow (hwnd);
147 EnableWindow(hwnd, 0);
148 DialogBox (instance, MAKEINTRESOURCE(214), NULL, LicenceProc);
149 EnableWindow(hwnd, 1);
150 SetActiveWindow(hwnd);
156 DestroyWindow (hwnd);
163 * Dialog-box function for the passphrase box.
165 static int CALLBACK PassphraseProc(HWND hwnd, UINT msg,
166 WPARAM wParam, LPARAM lParam) {
167 static char *passphrase;
168 struct PassphraseProcStruct *p;
175 { /* centre the window */
179 hw = GetDesktopWindow();
180 if (GetWindowRect (hw, &rs) && GetWindowRect (hwnd, &rd))
181 MoveWindow (hwnd, (rs.right + rs.left + rd.left - rd.right)/2,
182 (rs.bottom + rs.top + rd.top - rd.bottom)/2,
183 rd.right-rd.left, rd.bottom-rd.top, TRUE);
186 SetForegroundWindow(hwnd);
187 SetWindowPos (hwnd, HWND_TOP, 0, 0, 0, 0,
188 SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
189 p = (struct PassphraseProcStruct *)lParam;
190 passphrase = p->passphrase;
192 SetDlgItemText(hwnd, 101, p->comment);
196 switch (LOWORD(wParam)) {
206 case 102: /* edit box */
207 if (HIWORD(wParam) == EN_CHANGE) {
208 GetDlgItemText (hwnd, 102, passphrase, PASSPHRASE_MAXLEN-1);
209 passphrase[PASSPHRASE_MAXLEN-1] = '\0';
222 * Update the visible key list.
224 static void keylist_update(void) {
226 struct ssh2_userkey *skey;
230 SendDlgItemMessage(keylist, 100, LB_RESETCONTENT, 0, 0);
231 for (rkey = first234(rsakeys, &e); rkey; rkey = next234(&e)) {
232 char listentry[512], *p;
234 * Replace two spaces in the fingerprint with tabs, for
235 * nice alignment in the box.
237 strcpy(listentry, "ssh1\t");
238 p = listentry+strlen(listentry);
239 rsa_fingerprint(p, sizeof(listentry)-(p-listentry), rkey);
240 p = strchr(listentry, ' '); if (p) *p = '\t';
241 p = strchr(listentry, ' '); if (p) *p = '\t';
242 SendDlgItemMessage (keylist, 100, LB_ADDSTRING,
243 0, (LPARAM)listentry);
245 for (skey = first234(ssh2keys, &e); skey; skey = next234(&e)) {
246 char listentry[512], *p;
249 * Replace two spaces in the fingerprint with tabs, for
250 * nice alignment in the box.
252 p = skey->alg->fingerprint(skey->data);
253 strncpy(listentry, p, sizeof(listentry));
254 p = strchr(listentry, ' '); if (p) *p = '\t';
255 p = strchr(listentry, ' '); if (p) *p = '\t';
256 len = strlen(listentry);
257 if (len < sizeof(listentry)-2) {
258 listentry[len] = '\t';
259 strncpy(listentry+len+1, skey->comment, sizeof(listentry)-len-1);
261 SendDlgItemMessage (keylist, 100, LB_ADDSTRING,
262 0, (LPARAM)listentry);
264 SendDlgItemMessage (keylist, 100, LB_SETCURSEL, (WPARAM) -1, 0);
269 * This function loads a key from a file and adds it.
271 static void add_keyfile(char *filename) {
272 char passphrase[PASSPHRASE_MAXLEN];
274 struct ssh2_userkey *skey;
279 struct PassphraseProcStruct pps;
282 ver = keyfile_version(filename);
284 MessageBox(NULL, "Couldn't load private key.", APPNAME,
285 MB_OK | MB_ICONERROR);
290 needs_pass = rsakey_encrypted(filename, &comment);
292 needs_pass = ssh2_userkey_encrypted(filename, &comment);
295 rkey = smalloc(sizeof(*rkey));
296 pps.passphrase = passphrase;
297 pps.comment = comment;
301 dlgret = DialogBoxParam(instance, MAKEINTRESOURCE(210),
302 NULL, PassphraseProc,
305 if (comment) sfree(comment);
308 return; /* operation cancelled */
313 ret = loadrsakey(filename, rkey, passphrase);
315 skey = ssh2_load_userkey(filename, passphrase);
316 if (skey == SSH2_WRONG_PASSPHRASE)
325 if (comment) sfree(comment);
327 MessageBox(NULL, "Couldn't load private key.", APPNAME,
328 MB_OK | MB_ICONERROR);
334 if (already_running) {
335 unsigned char *request, *response;
336 int reqlen, clen, resplen;
338 clen = strlen(rkey->comment);
340 reqlen = 4 + 1 + /* length, message type */
342 ssh1_bignum_length(rkey->modulus) +
343 ssh1_bignum_length(rkey->exponent) +
344 ssh1_bignum_length(rkey->private_exponent) +
345 ssh1_bignum_length(rkey->iqmp) +
346 ssh1_bignum_length(rkey->p) +
347 ssh1_bignum_length(rkey->q) +
348 4 + clen /* comment */
351 request = smalloc(reqlen);
353 request[4] = SSH1_AGENTC_ADD_RSA_IDENTITY;
355 PUT_32BIT(request+reqlen, bignum_bitcount(rkey->modulus));
357 reqlen += ssh1_write_bignum(request+reqlen, rkey->modulus);
358 reqlen += ssh1_write_bignum(request+reqlen, rkey->exponent);
359 reqlen += ssh1_write_bignum(request+reqlen, rkey->private_exponent);
360 reqlen += ssh1_write_bignum(request+reqlen, rkey->iqmp);
361 reqlen += ssh1_write_bignum(request+reqlen, rkey->p);
362 reqlen += ssh1_write_bignum(request+reqlen, rkey->q);
363 PUT_32BIT(request+reqlen, clen);
364 memcpy(request+reqlen+4, rkey->comment, clen);
366 PUT_32BIT(request, reqlen-4);
368 agent_query(request, reqlen, &response, &resplen);
369 if (resplen < 5 || response[4] != SSH_AGENT_SUCCESS)
370 MessageBox(NULL, "The already running Pageant "
371 "refused to add the key.", APPNAME,
372 MB_OK | MB_ICONERROR);
374 if (add234(rsakeys, rkey) != rkey)
375 sfree(rkey); /* already present, don't waste RAM */
378 if (already_running) {
379 unsigned char *request, *response;
380 int reqlen, alglen, clen, keybloblen, resplen;
381 alglen = strlen(skey->alg->name);
382 clen = strlen(skey->comment);
384 keybloblen = skey->alg->openssh_fmtkey(skey->data, NULL, 0);
386 reqlen = 4 + 1 + /* length, message type */
387 4 + alglen + /* algorithm name */
388 keybloblen + /* key data */
389 4 + clen /* comment */
392 request = smalloc(reqlen);
394 request[4] = SSH2_AGENTC_ADD_IDENTITY;
396 PUT_32BIT(request+reqlen, alglen);
398 memcpy(request+reqlen, skey->alg->name, alglen);
400 reqlen += skey->alg->openssh_fmtkey(skey->data,
401 request+reqlen, keybloblen);
402 PUT_32BIT(request+reqlen, clen);
403 memcpy(request+reqlen+4, skey->comment, clen);
404 PUT_32BIT(request, reqlen-4);
407 agent_query(request, reqlen, &response, &resplen);
408 if (resplen < 5 || response[4] != SSH_AGENT_SUCCESS)
409 MessageBox(NULL, "The already running Pageant"
410 "refused to add the key.", APPNAME,
411 MB_OK | MB_ICONERROR);
413 if (add234(ssh2keys, skey) != skey) {
414 skey->alg->freekey(skey->data);
415 sfree(skey); /* already present, don't waste RAM */
422 * This is the main agent function that answers messages.
424 static void answer_msg(void *msg) {
425 unsigned char *p = msg;
426 unsigned char *ret = msg;
430 * Get the message type.
436 case SSH1_AGENTC_REQUEST_RSA_IDENTITIES:
438 * Reply with SSH1_AGENT_RSA_IDENTITIES_ANSWER.
446 * Count up the number and length of keys we hold.
449 for (key = first234(rsakeys, &e); key; key = next234(&e)) {
451 len += 4; /* length field */
452 len += ssh1_bignum_length(key->exponent);
453 len += ssh1_bignum_length(key->modulus);
454 len += 4 + strlen(key->comment);
458 * Packet header is the obvious five bytes, plus four
459 * bytes for the key count.
462 if (len > AGENT_MAX_MSGLEN)
463 goto failure; /* aaargh! too much stuff! */
464 PUT_32BIT(ret, len-4);
465 ret[4] = SSH1_AGENT_RSA_IDENTITIES_ANSWER;
466 PUT_32BIT(ret+5, nkeys);
468 for (key = first234(rsakeys, &e); key; key = next234(&e)) {
469 PUT_32BIT(p, bignum_bitcount(key->modulus));
471 p += ssh1_write_bignum(p, key->exponent);
472 p += ssh1_write_bignum(p, key->modulus);
473 PUT_32BIT(p, strlen(key->comment));
474 memcpy(p+4, key->comment, strlen(key->comment));
475 p += 4 + strlen(key->comment);
479 case SSH2_AGENTC_REQUEST_IDENTITIES:
481 * Reply with SSH2_AGENT_IDENTITIES_ANSWER.
485 struct ssh2_userkey *key;
491 * Count up the number and length of keys we hold.
494 for (key = first234(ssh2keys, &e); key; key = next234(&e)) {
496 len += 4; /* length field */
497 blob = key->alg->public_blob(key->data, &bloblen);
500 len += 4 + strlen(key->comment);
504 * Packet header is the obvious five bytes, plus four
505 * bytes for the key count.
508 if (len > AGENT_MAX_MSGLEN)
509 goto failure; /* aaargh! too much stuff! */
510 PUT_32BIT(ret, len-4);
511 ret[4] = SSH2_AGENT_IDENTITIES_ANSWER;
512 PUT_32BIT(ret+5, nkeys);
514 for (key = first234(ssh2keys, &e); key; key = next234(&e)) {
515 blob = key->alg->public_blob(key->data, &bloblen);
516 PUT_32BIT(p, bloblen);
518 memcpy(p, blob, bloblen);
521 PUT_32BIT(p, strlen(key->comment));
522 memcpy(p+4, key->comment, strlen(key->comment));
523 p += 4 + strlen(key->comment);
527 case SSH1_AGENTC_RSA_CHALLENGE:
529 * Reply with either SSH1_AGENT_RSA_RESPONSE or
530 * SSH_AGENT_FAILURE, depending on whether we have that key
534 struct RSAKey reqkey, *key;
535 Bignum challenge, response;
536 unsigned char response_source[48], response_md5[16];
537 struct MD5Context md5c;
541 p += ssh1_read_bignum(p, &reqkey.exponent);
542 p += ssh1_read_bignum(p, &reqkey.modulus);
543 p += ssh1_read_bignum(p, &challenge);
544 memcpy(response_source+32, p, 16); p += 16;
545 if (GET_32BIT(p) != 1 ||
546 (key = find234(rsakeys, &reqkey, NULL)) == NULL) {
547 freebn(reqkey.exponent);
548 freebn(reqkey.modulus);
552 response = rsadecrypt(challenge, key);
553 for (i = 0; i < 32; i++)
554 response_source[i] = bignum_byte(response, 31-i);
557 MD5Update(&md5c, response_source, 48);
558 MD5Final(response_md5, &md5c);
559 memset(response_source, 0, 48); /* burn the evidence */
560 freebn(response); /* and that evidence */
561 freebn(challenge); /* yes, and that evidence */
562 freebn(reqkey.exponent); /* and free some memory ... */
563 freebn(reqkey.modulus); /* ... while we're at it. */
566 * Packet is the obvious five byte header, plus sixteen
570 PUT_32BIT(ret, len-4);
571 ret[4] = SSH1_AGENT_RSA_RESPONSE;
572 memcpy(ret+5, response_md5, 16);
575 case SSH2_AGENTC_SIGN_REQUEST:
577 * Reply with either SSH2_AGENT_RSA_RESPONSE or
578 * SSH_AGENT_FAILURE, depending on whether we have that key
582 struct ssh2_userkey *key;
584 unsigned char *data, *signature;
585 int datalen, siglen, len;
587 b.len = GET_32BIT(p);
591 datalen = GET_32BIT(p);
594 key = find234(ssh2keys, &b, cmpkeys_ssh2_asymm);
597 signature = key->alg->sign(key->data, data, datalen, &siglen);
599 PUT_32BIT(ret, len-4);
600 ret[4] = SSH2_AGENT_SIGN_RESPONSE;
601 PUT_32BIT(ret+5, siglen);
602 memcpy(ret+5+4, signature, siglen);
606 case SSH1_AGENTC_ADD_RSA_IDENTITY:
608 * Add to the list and return SSH_AGENT_SUCCESS, or
609 * SSH_AGENT_FAILURE if the key was malformed.
614 key = smalloc(sizeof(struct RSAKey));
615 memset(key, 0, sizeof(key));
616 p += makekey(p, key, NULL, 1);
617 p += makeprivate(p, key);
618 p += ssh1_read_bignum(p, key->iqmp); /* p^-1 mod q */
619 p += ssh1_read_bignum(p, key->p); /* p */
620 p += ssh1_read_bignum(p, key->q); /* q */
621 comment = smalloc(GET_32BIT(p));
623 memcpy(comment, p+4, GET_32BIT(p));
624 key->comment = comment;
627 ret[4] = SSH_AGENT_FAILURE;
628 if (add234(rsakeys, key) == key) {
630 ret[4] = SSH_AGENT_SUCCESS;
637 case SSH2_AGENTC_ADD_IDENTITY:
639 * Add to the list and return SSH_AGENT_SUCCESS, or
640 * SSH_AGENT_FAILURE if the key was malformed.
643 struct ssh2_userkey *key;
648 key = smalloc(sizeof(struct ssh2_userkey));
650 alglen = GET_32BIT(p); p += 4;
651 alg = p; p += alglen;
652 /* Add further algorithm names here. */
653 if (alglen == 7 && !memcmp(alg, "ssh-rsa", 7))
660 bloblen = GET_32BIT((unsigned char *)msg) - (p-(unsigned char *)msg-4);
661 key->data = key->alg->openssh_createkey(&p, &bloblen);
666 commlen = GET_32BIT(p); p += 4;
668 comment = smalloc(commlen+1);
670 memcpy(comment, p, commlen);
671 comment[commlen] = '\0';
673 key->comment = comment;
676 ret[4] = SSH_AGENT_FAILURE;
677 if (add234(ssh2keys, key) == key) {
679 ret[4] = SSH_AGENT_SUCCESS;
681 key->alg->freekey(key->data);
687 case SSH1_AGENTC_REMOVE_RSA_IDENTITY:
689 * Remove from the list and return SSH_AGENT_SUCCESS, or
690 * perhaps SSH_AGENT_FAILURE if it wasn't in the list to
694 struct RSAKey reqkey, *key;
696 p += makekey(p, &reqkey, NULL, 0);
697 key = find234(rsakeys, &reqkey, NULL);
698 freebn(reqkey.exponent);
699 freebn(reqkey.modulus);
701 ret[4] = SSH_AGENT_FAILURE;
703 del234(rsakeys, key);
707 ret[4] = SSH_AGENT_SUCCESS;
711 case SSH2_AGENTC_REMOVE_IDENTITY:
713 * Remove from the list and return SSH_AGENT_SUCCESS, or
714 * perhaps SSH_AGENT_FAILURE if it wasn't in the list to
718 struct ssh2_userkey *key;
721 b.len = GET_32BIT(p);
725 key = find234(ssh2keys, &b, cmpkeys_ssh2_asymm);
730 ret[4] = SSH_AGENT_FAILURE;
732 del234(ssh2keys, key);
734 key->alg->freekey(key->data);
736 ret[4] = SSH_AGENT_SUCCESS;
740 case SSH1_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
742 * Remove all SSH1 keys. Always returns success.
748 while ( (rkey = first234(rsakeys, &e)) != NULL ) {
749 del234(rsakeys, rkey);
756 ret[4] = SSH_AGENT_SUCCESS;
759 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
761 * Remove all SSH2 keys. Always returns success.
764 struct ssh2_userkey *skey;
767 while ( (skey = first234(ssh2keys, &e)) != NULL ) {
768 del234(ssh2keys, skey);
769 skey->alg->freekey(skey->data);
775 ret[4] = SSH_AGENT_SUCCESS;
781 * Unrecognised message. Return SSH_AGENT_FAILURE.
784 ret[4] = SSH_AGENT_FAILURE;
790 * Key comparison function for the 2-3-4 tree of RSA keys.
792 static int cmpkeys_rsa(void *av, void *bv) {
793 struct RSAKey *a = (struct RSAKey *)av;
794 struct RSAKey *b = (struct RSAKey *)bv;
801 * Compare by length of moduli.
803 alen = bignum_bitcount(am);
804 blen = bignum_bitcount(bm);
805 if (alen > blen) return +1; else if (alen < blen) return -1;
807 * Now compare by moduli themselves.
809 alen = (alen + 7) / 8; /* byte count */
812 abyte = bignum_byte(am, alen);
813 bbyte = bignum_byte(bm, alen);
814 if (abyte > bbyte) return +1; else if (abyte < bbyte) return -1;
823 * Key comparison function for the 2-3-4 tree of SSH2 keys.
825 static int cmpkeys_ssh2(void *av, void *bv) {
826 struct ssh2_userkey *a = (struct ssh2_userkey *)av;
827 struct ssh2_userkey *b = (struct ssh2_userkey *)bv;
830 unsigned char *ablob, *bblob;
834 * Compare purely by public blob.
836 ablob = a->alg->public_blob(a->data, &alen);
837 bblob = b->alg->public_blob(b->data, &blen);
840 for (i = 0; i < alen && i < blen; i++) {
841 if (ablob[i] < bblob[i]) {
843 } else if (ablob[i] > bblob[i]) {
847 if (c == 0 && i < alen) c = +1; /* a is longer */
848 if (c == 0 && i < blen) c = -1; /* a is longer */
857 * Key comparison function for looking up a blob in the 2-3-4 tree
860 static int cmpkeys_ssh2_asymm(void *av, void *bv) {
861 struct blob *a = (struct blob *)av;
862 struct ssh2_userkey *b = (struct ssh2_userkey *)bv;
865 unsigned char *ablob, *bblob;
869 * Compare purely by public blob.
873 bblob = b->alg->public_blob(b->data, &blen);
876 for (i = 0; i < alen && i < blen; i++) {
877 if (ablob[i] < bblob[i]) {
879 } else if (ablob[i] > bblob[i]) {
883 if (c == 0 && i < alen) c = +1; /* a is longer */
884 if (c == 0 && i < blen) c = -1; /* a is longer */
891 static void error(char *s) {
892 MessageBox(hwnd, s, APPNAME, MB_OK | MB_ICONERROR);
896 * Prompt for a key file to add, and add it.
898 static void prompt_add_keyfile(void) {
900 char filename[FILENAME_MAX];
901 memset(&of, 0, sizeof(of));
902 #ifdef OPENFILENAME_SIZE_VERSION_400
903 of.lStructSize = OPENFILENAME_SIZE_VERSION_400;
905 of.lStructSize = sizeof(of);
908 of.lpstrFilter = "All Files\0*\0\0\0";
909 of.lpstrCustomFilter = NULL;
911 of.lpstrFile = filename; *filename = '\0';
912 of.nMaxFile = sizeof(filename);
913 of.lpstrFileTitle = NULL;
914 of.lpstrInitialDir = NULL;
915 of.lpstrTitle = "Select Private Key File";
917 if (GetOpenFileName(&of)) {
918 add_keyfile(filename);
924 * Dialog-box function for the key list box.
926 static int CALLBACK KeyListProc(HWND hwnd, UINT msg,
927 WPARAM wParam, LPARAM lParam) {
930 struct ssh2_userkey *skey;
937 { /* centre the window */
941 hw = GetDesktopWindow();
942 if (GetWindowRect (hw, &rs) && GetWindowRect (hwnd, &rd))
943 MoveWindow (hwnd, (rs.right + rs.left + rd.left - rd.right)/2,
944 (rs.bottom + rs.top + rd.top - rd.bottom)/2,
945 rd.right-rd.left, rd.bottom-rd.top, TRUE);
950 static int tabs[] = {35, 60, 210};
951 SendDlgItemMessage (hwnd, 100, LB_SETTABSTOPS,
952 sizeof(tabs)/sizeof(*tabs), (LPARAM) tabs);
957 switch (LOWORD(wParam)) {
963 case 101: /* add key */
964 if (HIWORD(wParam) == BN_CLICKED ||
965 HIWORD(wParam) == BN_DOUBLECLICKED) {
966 prompt_add_keyfile();
969 case 102: /* remove key */
970 if (HIWORD(wParam) == BN_CLICKED ||
971 HIWORD(wParam) == BN_DOUBLECLICKED) {
972 int n = SendDlgItemMessage (hwnd, 100, LB_GETCURSEL, 0, 0);
977 for (rkey = first234(rsakeys, &e); rkey; rkey = next234(&e))
981 del234(rsakeys, rkey);
985 for (skey = first234(ssh2keys, &e); skey; skey = next234(&e))
989 del234(ssh2keys, skey);
990 skey->alg->freekey(skey->data);
1001 DestroyWindow(hwnd);
1007 static LRESULT CALLBACK WndProc (HWND hwnd, UINT message,
1008 WPARAM wParam, LPARAM lParam) {
1010 static int menuinprogress;
1014 if (lParam == WM_RBUTTONUP) {
1016 GetCursorPos(&cursorpos);
1017 PostMessage(hwnd, WM_SYSTRAY2, cursorpos.x, cursorpos.y);
1018 } else if (lParam == WM_LBUTTONDBLCLK) {
1019 /* Equivalent to IDM_VIEWKEYS. */
1020 PostMessage(hwnd, WM_COMMAND, IDM_VIEWKEYS, 0);
1024 if (!menuinprogress) {
1026 SetForegroundWindow(hwnd);
1027 ret = TrackPopupMenu(systray_menu,
1028 TPM_RIGHTALIGN | TPM_BOTTOMALIGN |
1030 wParam, lParam, 0, hwnd, NULL);
1036 switch (wParam & ~0xF) { /* low 4 bits reserved to Windows */
1038 SendMessage(hwnd, WM_CLOSE, 0, 0);
1042 keylist = CreateDialog (instance, MAKEINTRESOURCE(211),
1044 ShowWindow (keylist, SW_SHOWNORMAL);
1046 * Sometimes the window comes up minimised / hidden
1047 * for no obvious reason. Prevent this.
1049 SetForegroundWindow(keylist);
1050 SetWindowPos (keylist, HWND_TOP, 0, 0, 0, 0,
1051 SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
1055 prompt_add_keyfile();
1059 aboutbox = CreateDialog (instance, MAKEINTRESOURCE(213),
1061 ShowWindow (aboutbox, SW_SHOWNORMAL);
1063 * Sometimes the window comes up minimised / hidden
1064 * for no obvious reason. Prevent this.
1066 SetForegroundWindow(aboutbox);
1067 SetWindowPos (aboutbox, HWND_TOP, 0, 0, 0, 0,
1068 SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
1074 PostQuitMessage (0);
1078 COPYDATASTRUCT *cds;
1081 HANDLE filemap, proc;
1082 PSID mapowner, procowner;
1083 PSECURITY_DESCRIPTOR psd1 = NULL, psd2 = NULL;
1086 cds = (COPYDATASTRUCT *)lParam;
1087 if (cds->dwData != AGENT_COPYDATA_ID)
1088 return 0; /* not our message, mate */
1089 mapname = (char *)cds->lpData;
1090 if (mapname[cds->cbData - 1] != '\0')
1091 return 0; /* failure to be ASCIZ! */
1093 debug(("mapname is :%s:\r\n", mapname));
1095 filemap = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, mapname);
1097 debug(("filemap is %p\r\n", filemap));
1099 if (filemap != NULL && filemap != INVALID_HANDLE_VALUE) {
1103 if ((proc = OpenProcess(MAXIMUM_ALLOWED, FALSE,
1104 GetCurrentProcessId())) == NULL) {
1106 debug(("couldn't get handle for process\r\n"));
1110 if (getsecurityinfo(proc, SE_KERNEL_OBJECT,
1111 OWNER_SECURITY_INFORMATION,
1112 &procowner, NULL, NULL, NULL,
1113 &psd2) != ERROR_SUCCESS) {
1115 debug(("couldn't get owner info for process\r\n"));
1118 return 0; /* unable to get security info */
1121 if ((rc = getsecurityinfo(filemap, SE_KERNEL_OBJECT,
1122 OWNER_SECURITY_INFORMATION,
1123 &mapowner, NULL, NULL, NULL,
1124 &psd1) != ERROR_SUCCESS)) {
1126 debug(("couldn't get owner info for filemap: %d\r\n", rc));
1131 debug(("got security stuff\r\n"));
1133 if (!EqualSid(mapowner, procowner))
1134 return 0; /* security ID mismatch! */
1136 debug(("security stuff matched\r\n"));
1142 debug(("security APIs not present\r\n"));
1146 p = MapViewOfFile(filemap, FILE_MAP_WRITE, 0, 0, 0);
1148 debug(("p is %p\r\n", p));
1149 {int i; for(i=0;i<5;i++)debug(("p[%d]=%02x\r\n", i, ((unsigned char *)p)[i]));}
1155 CloseHandle(filemap);
1160 return DefWindowProc (hwnd, message, wParam, lParam);
1164 * Fork and Exec the command in cmdline. [DBW]
1166 void spawn_cmd(char *cmdline, int show) {
1167 if (ShellExecute(NULL, _T("open"), cmdline,
1168 NULL, NULL, show) <= (HINSTANCE) 32) {
1170 sprintf(sMsg, _T("Failed to run \"%.100s\", Error: %d"), cmdline,
1172 MessageBox(NULL, sMsg, APPNAME, MB_OK | MB_ICONEXCLAMATION);
1176 int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show) {
1181 char *command = NULL;
1185 * Determine whether we're an NT system (should have security
1186 * APIs) or a non-NT system (don't do security).
1188 memset(&osi, 0, sizeof(OSVERSIONINFO));
1189 osi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
1190 if (GetVersionEx(&osi) && osi.dwPlatformId==VER_PLATFORM_WIN32_NT) {
1191 has_security = TRUE;
1193 has_security = FALSE;
1198 * Attempt to get the security API we need.
1200 advapi = LoadLibrary("ADVAPI32.DLL");
1201 getsecurityinfo = (gsi_fn_t)GetProcAddress(advapi, "GetSecurityInfo");
1202 if (!getsecurityinfo) {
1204 "Unable to access security APIs. Pageant will\n"
1205 "not run, in case it causes a security breach.",
1206 "Pageant Fatal Error", MB_ICONERROR | MB_OK);
1211 "This program has been compiled for Win9X and will\n"
1212 "not run on NT, in case it causes a security breach.",
1213 "Pageant Fatal Error", MB_ICONERROR | MB_OK);
1222 * Find out if Pageant is already running.
1224 already_running = FALSE;
1225 if (FindWindow("Pageant", "Pageant"))
1226 already_running = TRUE;
1231 wndclass.lpfnWndProc = WndProc;
1232 wndclass.cbClsExtra = 0;
1233 wndclass.cbWndExtra = 0;
1234 wndclass.hInstance = inst;
1235 wndclass.hIcon = LoadIcon (inst,
1236 MAKEINTRESOURCE(IDI_MAINICON));
1237 wndclass.hCursor = LoadCursor (NULL, IDC_IBEAM);
1238 wndclass.hbrBackground = GetStockObject (BLACK_BRUSH);
1239 wndclass.lpszMenuName = NULL;
1240 wndclass.lpszClassName = APPNAME;
1242 RegisterClass (&wndclass);
1245 hwnd = keylist = NULL;
1247 hwnd = CreateWindow (APPNAME, APPNAME,
1248 WS_OVERLAPPEDWINDOW | WS_VSCROLL,
1249 CW_USEDEFAULT, CW_USEDEFAULT,
1250 100, 100, NULL, NULL, inst, NULL);
1252 /* Set up a system tray icon */
1255 NOTIFYICONDATA tnid;
1258 #ifdef NIM_SETVERSION
1260 res = Shell_NotifyIcon(NIM_SETVERSION, &tnid);
1263 tnid.cbSize = sizeof(NOTIFYICONDATA);
1265 tnid.uID = 1; /* unique within this systray use */
1266 tnid.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
1267 tnid.uCallbackMessage = WM_SYSTRAY;
1268 tnid.hIcon = hicon = LoadIcon (instance, MAKEINTRESOURCE(201));
1269 strcpy(tnid.szTip, "Pageant (PuTTY authentication agent)");
1271 res = Shell_NotifyIcon(NIM_ADD, &tnid);
1276 systray_menu = CreatePopupMenu();
1277 /* accelerators used: vkxa */
1278 AppendMenu (systray_menu, MF_ENABLED, IDM_VIEWKEYS, "&View Keys");
1279 AppendMenu (systray_menu, MF_ENABLED, IDM_ADDKEY, "Add &Key");
1280 AppendMenu (systray_menu, MF_ENABLED, IDM_ABOUT, "&About");
1281 AppendMenu (systray_menu, MF_ENABLED, IDM_CLOSE, "E&xit");
1284 ShowWindow (hwnd, SW_HIDE);
1287 * Initialise storage for RSA keys.
1289 rsakeys = newtree234(cmpkeys_rsa);
1290 ssh2keys = newtree234(cmpkeys_ssh2);
1295 * Process the command line and add keys as listed on it.
1296 * If we already determined that we need to spawn a program from above we
1297 * need to ignore the first two arguments. [DBW]
1305 while (*p && isspace(*p)) p++;
1306 if (*p && !isspace(*p)) {
1307 char *q = p, *pp = p;
1308 while (*p && (inquotes || !isspace(*p)))
1311 inquotes = !inquotes;
1321 if (!strcmp(q, "-c")) {
1323 * If we see `-c', then the rest of the
1324 * command line should be treated as a
1325 * command to be spawned.
1327 while (*p && isspace(*p)) p++;
1338 if (command) spawn_cmd (command, show);
1341 * If Pageant was already running, we leave now. If we haven't
1342 * even taken any auxiliary action (spawned a command or added
1345 if (already_running) {
1346 if (!command && !added_keys) {
1347 MessageBox(NULL, "Pageant is already running", "Pageant Error",
1348 MB_ICONERROR | MB_OK);
1350 if (advapi) FreeLibrary(advapi);
1355 * Main message loop.
1357 while (GetMessage(&msg, NULL, 0, 0) == 1) {
1358 TranslateMessage(&msg);
1359 DispatchMessage(&msg);
1362 /* Clean up the system tray icon */
1364 NOTIFYICONDATA tnid;
1366 tnid.cbSize = sizeof(NOTIFYICONDATA);
1370 Shell_NotifyIcon(NIM_DELETE, &tnid);
1372 DestroyMenu(systray_menu);
1375 if (advapi) FreeLibrary(advapi);