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 * Exports from pageantc.c
61 void agent_query(void *in, int inlen, void **out, int *outlen);
62 int agent_exists(void);
65 * We need this to link with the RSA code, because rsaencrypt()
66 * pads its data with random bytes. Since we only use rsadecrypt()
67 * and the signing functions, which are deterministic, this should
70 * If it _is_ called, there is a _serious_ problem, because it
71 * won't generate true random numbers. So we must scream, panic,
72 * and exit immediately if that should happen.
74 int random_byte(void) {
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) {
96 #define GET_32BIT(cp) \
97 (((unsigned long)(unsigned char)(cp)[0] << 24) | \
98 ((unsigned long)(unsigned char)(cp)[1] << 16) | \
99 ((unsigned long)(unsigned char)(cp)[2] << 8) | \
100 ((unsigned long)(unsigned char)(cp)[3]))
102 #define PUT_32BIT(cp, value) { \
103 (cp)[0] = (unsigned char)((value) >> 24); \
104 (cp)[1] = (unsigned char)((value) >> 16); \
105 (cp)[2] = (unsigned char)((value) >> 8); \
106 (cp)[3] = (unsigned char)(value); }
108 #define PASSPHRASE_MAXLEN 512
110 struct PassphraseProcStruct {
116 * Dialog-box function for the Licence box.
118 static int CALLBACK LicenceProc (HWND hwnd, UINT msg,
119 WPARAM wParam, LPARAM lParam) {
124 switch (LOWORD(wParam)) {
138 * Dialog-box function for the About box.
140 static int CALLBACK AboutProc (HWND hwnd, UINT msg,
141 WPARAM wParam, LPARAM lParam) {
144 SetDlgItemText (hwnd, 100, ver);
147 switch (LOWORD(wParam)) {
150 DestroyWindow (hwnd);
153 EnableWindow(hwnd, 0);
154 DialogBox (instance, MAKEINTRESOURCE(214), NULL, LicenceProc);
155 EnableWindow(hwnd, 1);
156 SetActiveWindow(hwnd);
162 DestroyWindow (hwnd);
169 * Dialog-box function for the passphrase box.
171 static int CALLBACK PassphraseProc(HWND hwnd, UINT msg,
172 WPARAM wParam, LPARAM lParam) {
173 static char *passphrase;
174 struct PassphraseProcStruct *p;
181 { /* centre the window */
185 hw = GetDesktopWindow();
186 if (GetWindowRect (hw, &rs) && GetWindowRect (hwnd, &rd))
187 MoveWindow (hwnd, (rs.right + rs.left + rd.left - rd.right)/2,
188 (rs.bottom + rs.top + rd.top - rd.bottom)/2,
189 rd.right-rd.left, rd.bottom-rd.top, TRUE);
192 SetForegroundWindow(hwnd);
193 SetWindowPos (hwnd, HWND_TOP, 0, 0, 0, 0,
194 SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
195 p = (struct PassphraseProcStruct *)lParam;
196 passphrase = p->passphrase;
198 SetDlgItemText(hwnd, 101, p->comment);
202 switch (LOWORD(wParam)) {
212 case 102: /* edit box */
213 if (HIWORD(wParam) == EN_CHANGE) {
214 GetDlgItemText (hwnd, 102, passphrase, PASSPHRASE_MAXLEN-1);
215 passphrase[PASSPHRASE_MAXLEN-1] = '\0';
228 * Update the visible key list.
230 static void keylist_update(void) {
232 struct ssh2_userkey *skey;
236 SendDlgItemMessage(keylist, 100, LB_RESETCONTENT, 0, 0);
237 for (i = 0; NULL != (rkey = index234(rsakeys, i)); i++) {
238 char listentry[512], *p;
240 * Replace two spaces in the fingerprint with tabs, for
241 * nice alignment in the box.
243 strcpy(listentry, "ssh1\t");
244 p = listentry+strlen(listentry);
245 rsa_fingerprint(p, sizeof(listentry)-(p-listentry), rkey);
246 p = strchr(listentry, ' '); if (p) *p = '\t';
247 p = strchr(listentry, ' '); if (p) *p = '\t';
248 SendDlgItemMessage (keylist, 100, LB_ADDSTRING,
249 0, (LPARAM)listentry);
251 for (i = 0; NULL != (skey = index234(ssh2keys, i)); i++) {
252 char listentry[512], *p;
255 * Replace two spaces in the fingerprint with tabs, for
256 * nice alignment in the box.
258 p = skey->alg->fingerprint(skey->data);
259 strncpy(listentry, p, sizeof(listentry));
260 p = strchr(listentry, ' '); if (p) *p = '\t';
261 p = strchr(listentry, ' '); if (p) *p = '\t';
262 len = strlen(listentry);
263 if (len < sizeof(listentry)-2) {
264 listentry[len] = '\t';
265 strncpy(listentry+len+1, skey->comment, sizeof(listentry)-len-1);
267 SendDlgItemMessage (keylist, 100, LB_ADDSTRING,
268 0, (LPARAM)listentry);
270 SendDlgItemMessage (keylist, 100, LB_SETCURSEL, (WPARAM) -1, 0);
275 * This function loads a key from a file and adds it.
277 static void add_keyfile(char *filename) {
278 char passphrase[PASSPHRASE_MAXLEN];
280 struct ssh2_userkey *skey;
285 struct PassphraseProcStruct pps;
288 ver = keyfile_version(filename);
290 MessageBox(NULL, "Couldn't load private key.", APPNAME,
291 MB_OK | MB_ICONERROR);
296 needs_pass = rsakey_encrypted(filename, &comment);
298 needs_pass = ssh2_userkey_encrypted(filename, &comment);
301 rkey = smalloc(sizeof(*rkey));
302 pps.passphrase = passphrase;
303 pps.comment = comment;
307 dlgret = DialogBoxParam(instance, MAKEINTRESOURCE(210),
308 NULL, PassphraseProc,
311 if (comment) sfree(comment);
314 return; /* operation cancelled */
319 ret = loadrsakey(filename, rkey, passphrase);
321 skey = ssh2_load_userkey(filename, passphrase);
322 if (skey == SSH2_WRONG_PASSPHRASE)
331 if (comment) sfree(comment);
333 MessageBox(NULL, "Couldn't load private key.", APPNAME,
334 MB_OK | MB_ICONERROR);
340 if (already_running) {
341 unsigned char *request, *response;
342 int reqlen, clen, resplen;
344 clen = strlen(rkey->comment);
346 reqlen = 4 + 1 + /* length, message type */
348 ssh1_bignum_length(rkey->modulus) +
349 ssh1_bignum_length(rkey->exponent) +
350 ssh1_bignum_length(rkey->private_exponent) +
351 ssh1_bignum_length(rkey->iqmp) +
352 ssh1_bignum_length(rkey->p) +
353 ssh1_bignum_length(rkey->q) +
354 4 + clen /* comment */
357 request = smalloc(reqlen);
359 request[4] = SSH1_AGENTC_ADD_RSA_IDENTITY;
361 PUT_32BIT(request+reqlen, bignum_bitcount(rkey->modulus));
363 reqlen += ssh1_write_bignum(request+reqlen, rkey->modulus);
364 reqlen += ssh1_write_bignum(request+reqlen, rkey->exponent);
365 reqlen += ssh1_write_bignum(request+reqlen, rkey->private_exponent);
366 reqlen += ssh1_write_bignum(request+reqlen, rkey->iqmp);
367 reqlen += ssh1_write_bignum(request+reqlen, rkey->p);
368 reqlen += ssh1_write_bignum(request+reqlen, rkey->q);
369 PUT_32BIT(request+reqlen, clen);
370 memcpy(request+reqlen+4, rkey->comment, clen);
372 PUT_32BIT(request, reqlen-4);
374 agent_query(request, reqlen, &response, &resplen);
375 if (resplen < 5 || response[4] != SSH_AGENT_SUCCESS)
376 MessageBox(NULL, "The already running Pageant "
377 "refused to add the key.", APPNAME,
378 MB_OK | MB_ICONERROR);
380 if (add234(rsakeys, rkey) != rkey)
381 sfree(rkey); /* already present, don't waste RAM */
384 if (already_running) {
385 unsigned char *request, *response;
386 int reqlen, alglen, clen, keybloblen, resplen;
387 alglen = strlen(skey->alg->name);
388 clen = strlen(skey->comment);
390 keybloblen = skey->alg->openssh_fmtkey(skey->data, NULL, 0);
392 reqlen = 4 + 1 + /* length, message type */
393 4 + alglen + /* algorithm name */
394 keybloblen + /* key data */
395 4 + clen /* comment */
398 request = smalloc(reqlen);
400 request[4] = SSH2_AGENTC_ADD_IDENTITY;
402 PUT_32BIT(request+reqlen, alglen);
404 memcpy(request+reqlen, skey->alg->name, alglen);
406 reqlen += skey->alg->openssh_fmtkey(skey->data,
407 request+reqlen, keybloblen);
408 PUT_32BIT(request+reqlen, clen);
409 memcpy(request+reqlen+4, skey->comment, clen);
410 PUT_32BIT(request, reqlen-4);
413 agent_query(request, reqlen, &response, &resplen);
414 if (resplen < 5 || response[4] != SSH_AGENT_SUCCESS)
415 MessageBox(NULL, "The already running Pageant"
416 "refused to add the key.", APPNAME,
417 MB_OK | MB_ICONERROR);
419 if (add234(ssh2keys, skey) != skey) {
420 skey->alg->freekey(skey->data);
421 sfree(skey); /* already present, don't waste RAM */
428 * This is the main agent function that answers messages.
430 static void answer_msg(void *msg) {
431 unsigned char *p = msg;
432 unsigned char *ret = msg;
436 * Get the message type.
442 case SSH1_AGENTC_REQUEST_RSA_IDENTITIES:
444 * Reply with SSH1_AGENT_RSA_IDENTITIES_ANSWER.
452 * Count up the number and length of keys we hold.
455 for (i = 0; NULL != (key = index234(rsakeys, i)); i++) {
457 len += 4; /* length field */
458 len += ssh1_bignum_length(key->exponent);
459 len += ssh1_bignum_length(key->modulus);
460 len += 4 + strlen(key->comment);
464 * Packet header is the obvious five bytes, plus four
465 * bytes for the key count.
468 if (len > AGENT_MAX_MSGLEN)
469 goto failure; /* aaargh! too much stuff! */
470 PUT_32BIT(ret, len-4);
471 ret[4] = SSH1_AGENT_RSA_IDENTITIES_ANSWER;
472 PUT_32BIT(ret+5, nkeys);
474 for (i = 0; NULL != (key = index234(rsakeys, i)); i++) {
475 PUT_32BIT(p, bignum_bitcount(key->modulus));
477 p += ssh1_write_bignum(p, key->exponent);
478 p += ssh1_write_bignum(p, key->modulus);
479 PUT_32BIT(p, strlen(key->comment));
480 memcpy(p+4, key->comment, strlen(key->comment));
481 p += 4 + strlen(key->comment);
485 case SSH2_AGENTC_REQUEST_IDENTITIES:
487 * Reply with SSH2_AGENT_IDENTITIES_ANSWER.
490 struct ssh2_userkey *key;
497 * Count up the number and length of keys we hold.
500 for (i = 0; NULL != (key = index234(ssh2keys, i)); i++) {
502 len += 4; /* length field */
503 blob = key->alg->public_blob(key->data, &bloblen);
506 len += 4 + strlen(key->comment);
510 * Packet header is the obvious five bytes, plus four
511 * bytes for the key count.
514 if (len > AGENT_MAX_MSGLEN)
515 goto failure; /* aaargh! too much stuff! */
516 PUT_32BIT(ret, len-4);
517 ret[4] = SSH2_AGENT_IDENTITIES_ANSWER;
518 PUT_32BIT(ret+5, nkeys);
520 for (i = 0; NULL != (key = index234(ssh2keys, i)); i++) {
521 blob = key->alg->public_blob(key->data, &bloblen);
522 PUT_32BIT(p, bloblen);
524 memcpy(p, blob, bloblen);
527 PUT_32BIT(p, strlen(key->comment));
528 memcpy(p+4, key->comment, strlen(key->comment));
529 p += 4 + strlen(key->comment);
533 case SSH1_AGENTC_RSA_CHALLENGE:
535 * Reply with either SSH1_AGENT_RSA_RESPONSE or
536 * SSH_AGENT_FAILURE, depending on whether we have that key
540 struct RSAKey reqkey, *key;
541 Bignum challenge, response;
542 unsigned char response_source[48], response_md5[16];
543 struct MD5Context md5c;
547 p += ssh1_read_bignum(p, &reqkey.exponent);
548 p += ssh1_read_bignum(p, &reqkey.modulus);
549 p += ssh1_read_bignum(p, &challenge);
550 memcpy(response_source+32, p, 16); p += 16;
551 if (GET_32BIT(p) != 1 ||
552 (key = find234(rsakeys, &reqkey, NULL)) == NULL) {
553 freebn(reqkey.exponent);
554 freebn(reqkey.modulus);
558 response = rsadecrypt(challenge, key);
559 for (i = 0; i < 32; i++)
560 response_source[i] = bignum_byte(response, 31-i);
563 MD5Update(&md5c, response_source, 48);
564 MD5Final(response_md5, &md5c);
565 memset(response_source, 0, 48); /* burn the evidence */
566 freebn(response); /* and that evidence */
567 freebn(challenge); /* yes, and that evidence */
568 freebn(reqkey.exponent); /* and free some memory ... */
569 freebn(reqkey.modulus); /* ... while we're at it. */
572 * Packet is the obvious five byte header, plus sixteen
576 PUT_32BIT(ret, len-4);
577 ret[4] = SSH1_AGENT_RSA_RESPONSE;
578 memcpy(ret+5, response_md5, 16);
581 case SSH2_AGENTC_SIGN_REQUEST:
583 * Reply with either SSH2_AGENT_RSA_RESPONSE or
584 * SSH_AGENT_FAILURE, depending on whether we have that key
588 struct ssh2_userkey *key;
590 unsigned char *data, *signature;
591 int datalen, siglen, len;
593 b.len = GET_32BIT(p);
597 datalen = GET_32BIT(p);
600 key = find234(ssh2keys, &b, cmpkeys_ssh2_asymm);
603 signature = key->alg->sign(key->data, data, datalen, &siglen);
605 PUT_32BIT(ret, len-4);
606 ret[4] = SSH2_AGENT_SIGN_RESPONSE;
607 PUT_32BIT(ret+5, siglen);
608 memcpy(ret+5+4, signature, siglen);
612 case SSH1_AGENTC_ADD_RSA_IDENTITY:
614 * Add to the list and return SSH_AGENT_SUCCESS, or
615 * SSH_AGENT_FAILURE if the key was malformed.
620 key = smalloc(sizeof(struct RSAKey));
621 memset(key, 0, sizeof(key));
622 p += makekey(p, key, NULL, 1);
623 p += makeprivate(p, key);
624 p += ssh1_read_bignum(p, key->iqmp); /* p^-1 mod q */
625 p += ssh1_read_bignum(p, key->p); /* p */
626 p += ssh1_read_bignum(p, key->q); /* q */
627 comment = smalloc(GET_32BIT(p));
629 memcpy(comment, p+4, GET_32BIT(p));
630 key->comment = comment;
633 ret[4] = SSH_AGENT_FAILURE;
634 if (add234(rsakeys, key) == key) {
636 ret[4] = SSH_AGENT_SUCCESS;
643 case SSH2_AGENTC_ADD_IDENTITY:
645 * Add to the list and return SSH_AGENT_SUCCESS, or
646 * SSH_AGENT_FAILURE if the key was malformed.
649 struct ssh2_userkey *key;
654 key = smalloc(sizeof(struct ssh2_userkey));
656 alglen = GET_32BIT(p); p += 4;
657 alg = p; p += alglen;
658 /* Add further algorithm names here. */
659 if (alglen == 7 && !memcmp(alg, "ssh-rsa", 7))
666 bloblen = GET_32BIT((unsigned char *)msg) - (p-(unsigned char *)msg-4);
667 key->data = key->alg->openssh_createkey(&p, &bloblen);
672 commlen = GET_32BIT(p); p += 4;
674 comment = smalloc(commlen+1);
676 memcpy(comment, p, commlen);
677 comment[commlen] = '\0';
679 key->comment = comment;
682 ret[4] = SSH_AGENT_FAILURE;
683 if (add234(ssh2keys, key) == key) {
685 ret[4] = SSH_AGENT_SUCCESS;
687 key->alg->freekey(key->data);
693 case SSH1_AGENTC_REMOVE_RSA_IDENTITY:
695 * Remove from the list and return SSH_AGENT_SUCCESS, or
696 * perhaps SSH_AGENT_FAILURE if it wasn't in the list to
700 struct RSAKey reqkey, *key;
702 p += makekey(p, &reqkey, NULL, 0);
703 key = find234(rsakeys, &reqkey, NULL);
704 freebn(reqkey.exponent);
705 freebn(reqkey.modulus);
707 ret[4] = SSH_AGENT_FAILURE;
709 del234(rsakeys, key);
713 ret[4] = SSH_AGENT_SUCCESS;
717 case SSH2_AGENTC_REMOVE_IDENTITY:
719 * Remove from the list and return SSH_AGENT_SUCCESS, or
720 * perhaps SSH_AGENT_FAILURE if it wasn't in the list to
724 struct ssh2_userkey *key;
727 b.len = GET_32BIT(p);
731 key = find234(ssh2keys, &b, cmpkeys_ssh2_asymm);
736 ret[4] = SSH_AGENT_FAILURE;
738 del234(ssh2keys, key);
740 key->alg->freekey(key->data);
742 ret[4] = SSH_AGENT_SUCCESS;
746 case SSH1_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
748 * Remove all SSH1 keys. Always returns success.
753 while ( (rkey = index234(rsakeys, 0)) != NULL ) {
754 del234(rsakeys, rkey);
761 ret[4] = SSH_AGENT_SUCCESS;
764 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
766 * Remove all SSH2 keys. Always returns success.
769 struct ssh2_userkey *skey;
771 while ( (skey = index234(ssh2keys, 0)) != NULL ) {
772 del234(ssh2keys, skey);
773 skey->alg->freekey(skey->data);
779 ret[4] = SSH_AGENT_SUCCESS;
785 * Unrecognised message. Return SSH_AGENT_FAILURE.
788 ret[4] = SSH_AGENT_FAILURE;
794 * Key comparison function for the 2-3-4 tree of RSA keys.
796 static int cmpkeys_rsa(void *av, void *bv) {
797 struct RSAKey *a = (struct RSAKey *)av;
798 struct RSAKey *b = (struct RSAKey *)bv;
805 * Compare by length of moduli.
807 alen = bignum_bitcount(am);
808 blen = bignum_bitcount(bm);
809 if (alen > blen) return +1; else if (alen < blen) return -1;
811 * Now compare by moduli themselves.
813 alen = (alen + 7) / 8; /* byte count */
816 abyte = bignum_byte(am, alen);
817 bbyte = bignum_byte(bm, alen);
818 if (abyte > bbyte) return +1; else if (abyte < bbyte) return -1;
827 * Key comparison function for the 2-3-4 tree of SSH2 keys.
829 static int cmpkeys_ssh2(void *av, void *bv) {
830 struct ssh2_userkey *a = (struct ssh2_userkey *)av;
831 struct ssh2_userkey *b = (struct ssh2_userkey *)bv;
834 unsigned char *ablob, *bblob;
838 * Compare purely by public blob.
840 ablob = a->alg->public_blob(a->data, &alen);
841 bblob = b->alg->public_blob(b->data, &blen);
844 for (i = 0; i < alen && i < blen; i++) {
845 if (ablob[i] < bblob[i]) {
847 } else if (ablob[i] > bblob[i]) {
851 if (c == 0 && i < alen) c = +1; /* a is longer */
852 if (c == 0 && i < blen) c = -1; /* a is longer */
861 * Key comparison function for looking up a blob in the 2-3-4 tree
864 static int cmpkeys_ssh2_asymm(void *av, void *bv) {
865 struct blob *a = (struct blob *)av;
866 struct ssh2_userkey *b = (struct ssh2_userkey *)bv;
869 unsigned char *ablob, *bblob;
873 * Compare purely by public blob.
877 bblob = b->alg->public_blob(b->data, &blen);
880 for (i = 0; i < alen && i < blen; i++) {
881 if (ablob[i] < bblob[i]) {
883 } else if (ablob[i] > bblob[i]) {
887 if (c == 0 && i < alen) c = +1; /* a is longer */
888 if (c == 0 && i < blen) c = -1; /* a is longer */
895 static void error(char *s) {
896 MessageBox(hwnd, s, APPNAME, MB_OK | MB_ICONERROR);
900 * Prompt for a key file to add, and add it.
902 static void prompt_add_keyfile(void) {
904 char filename[FILENAME_MAX];
905 memset(&of, 0, sizeof(of));
906 #ifdef OPENFILENAME_SIZE_VERSION_400
907 of.lStructSize = OPENFILENAME_SIZE_VERSION_400;
909 of.lStructSize = sizeof(of);
912 of.lpstrFilter = "All Files\0*\0\0\0";
913 of.lpstrCustomFilter = NULL;
915 of.lpstrFile = filename; *filename = '\0';
916 of.nMaxFile = sizeof(filename);
917 of.lpstrFileTitle = NULL;
918 of.lpstrInitialDir = NULL;
919 of.lpstrTitle = "Select Private Key File";
921 if (GetOpenFileName(&of)) {
922 add_keyfile(filename);
928 * Dialog-box function for the key list box.
930 static int CALLBACK KeyListProc(HWND hwnd, UINT msg,
931 WPARAM wParam, LPARAM lParam) {
933 struct ssh2_userkey *skey;
940 { /* centre the window */
944 hw = GetDesktopWindow();
945 if (GetWindowRect (hw, &rs) && GetWindowRect (hwnd, &rd))
946 MoveWindow (hwnd, (rs.right + rs.left + rd.left - rd.right)/2,
947 (rs.bottom + rs.top + rd.top - rd.bottom)/2,
948 rd.right-rd.left, rd.bottom-rd.top, TRUE);
953 static int tabs[] = {35, 60, 210};
954 SendDlgItemMessage (hwnd, 100, LB_SETTABSTOPS,
955 sizeof(tabs)/sizeof(*tabs), (LPARAM) tabs);
960 switch (LOWORD(wParam)) {
966 case 101: /* add key */
967 if (HIWORD(wParam) == BN_CLICKED ||
968 HIWORD(wParam) == BN_DOUBLECLICKED) {
969 prompt_add_keyfile();
972 case 102: /* remove key */
973 if (HIWORD(wParam) == BN_CLICKED ||
974 HIWORD(wParam) == BN_DOUBLECLICKED) {
975 int n = SendDlgItemMessage (hwnd, 100, LB_GETCURSEL, 0, 0);
981 for (i = 0; NULL != (rkey = index234(rsakeys, i)); i++)
985 del234(rsakeys, rkey);
989 for (i = 0; NULL != (skey = index234(ssh2keys, i)); i++)
993 del234(ssh2keys, skey);
994 skey->alg->freekey(skey->data);
1005 DestroyWindow(hwnd);
1011 static LRESULT CALLBACK WndProc (HWND hwnd, UINT message,
1012 WPARAM wParam, LPARAM lParam) {
1014 static int menuinprogress;
1018 if (lParam == WM_RBUTTONUP) {
1020 GetCursorPos(&cursorpos);
1021 PostMessage(hwnd, WM_SYSTRAY2, cursorpos.x, cursorpos.y);
1022 } else if (lParam == WM_LBUTTONDBLCLK) {
1023 /* Equivalent to IDM_VIEWKEYS. */
1024 PostMessage(hwnd, WM_COMMAND, IDM_VIEWKEYS, 0);
1028 if (!menuinprogress) {
1030 SetForegroundWindow(hwnd);
1031 ret = TrackPopupMenu(systray_menu,
1032 TPM_RIGHTALIGN | TPM_BOTTOMALIGN |
1034 wParam, lParam, 0, hwnd, NULL);
1040 switch (wParam & ~0xF) { /* low 4 bits reserved to Windows */
1042 SendMessage(hwnd, WM_CLOSE, 0, 0);
1046 keylist = CreateDialog (instance, MAKEINTRESOURCE(211),
1048 ShowWindow (keylist, SW_SHOWNORMAL);
1050 * Sometimes the window comes up minimised / hidden
1051 * for no obvious reason. Prevent this.
1053 SetForegroundWindow(keylist);
1054 SetWindowPos (keylist, HWND_TOP, 0, 0, 0, 0,
1055 SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
1059 prompt_add_keyfile();
1063 aboutbox = CreateDialog (instance, MAKEINTRESOURCE(213),
1065 ShowWindow (aboutbox, SW_SHOWNORMAL);
1067 * Sometimes the window comes up minimised / hidden
1068 * for no obvious reason. Prevent this.
1070 SetForegroundWindow(aboutbox);
1071 SetWindowPos (aboutbox, HWND_TOP, 0, 0, 0, 0,
1072 SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
1078 PostQuitMessage (0);
1082 COPYDATASTRUCT *cds;
1085 HANDLE filemap, proc;
1086 PSID mapowner, procowner;
1087 PSECURITY_DESCRIPTOR psd1 = NULL, psd2 = NULL;
1090 cds = (COPYDATASTRUCT *)lParam;
1091 if (cds->dwData != AGENT_COPYDATA_ID)
1092 return 0; /* not our message, mate */
1093 mapname = (char *)cds->lpData;
1094 if (mapname[cds->cbData - 1] != '\0')
1095 return 0; /* failure to be ASCIZ! */
1097 debug(("mapname is :%s:\r\n", mapname));
1099 filemap = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, mapname);
1101 debug(("filemap is %p\r\n", filemap));
1103 if (filemap != NULL && filemap != INVALID_HANDLE_VALUE) {
1107 if ((proc = OpenProcess(MAXIMUM_ALLOWED, FALSE,
1108 GetCurrentProcessId())) == NULL) {
1110 debug(("couldn't get handle for process\r\n"));
1114 if (getsecurityinfo(proc, SE_KERNEL_OBJECT,
1115 OWNER_SECURITY_INFORMATION,
1116 &procowner, NULL, NULL, NULL,
1117 &psd2) != ERROR_SUCCESS) {
1119 debug(("couldn't get owner info for process\r\n"));
1122 return 0; /* unable to get security info */
1125 if ((rc = getsecurityinfo(filemap, SE_KERNEL_OBJECT,
1126 OWNER_SECURITY_INFORMATION,
1127 &mapowner, NULL, NULL, NULL,
1128 &psd1) != ERROR_SUCCESS)) {
1130 debug(("couldn't get owner info for filemap: %d\r\n", rc));
1135 debug(("got security stuff\r\n"));
1137 if (!EqualSid(mapowner, procowner))
1138 return 0; /* security ID mismatch! */
1140 debug(("security stuff matched\r\n"));
1146 debug(("security APIs not present\r\n"));
1150 p = MapViewOfFile(filemap, FILE_MAP_WRITE, 0, 0, 0);
1152 debug(("p is %p\r\n", p));
1153 {int i; for(i=0;i<5;i++)debug(("p[%d]=%02x\r\n", i, ((unsigned char *)p)[i]));}
1159 CloseHandle(filemap);
1164 return DefWindowProc (hwnd, message, wParam, lParam);
1168 * Fork and Exec the command in cmdline. [DBW]
1170 void spawn_cmd(char *cmdline, int show) {
1171 if (ShellExecute(NULL, _T("open"), cmdline,
1172 NULL, NULL, show) <= (HINSTANCE) 32) {
1174 sprintf(sMsg, _T("Failed to run \"%.100s\", Error: %d"), cmdline,
1176 MessageBox(NULL, sMsg, APPNAME, MB_OK | MB_ICONEXCLAMATION);
1180 int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show) {
1185 char *command = NULL;
1189 * Determine whether we're an NT system (should have security
1190 * APIs) or a non-NT system (don't do security).
1192 memset(&osi, 0, sizeof(OSVERSIONINFO));
1193 osi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
1194 if (GetVersionEx(&osi) && osi.dwPlatformId==VER_PLATFORM_WIN32_NT) {
1195 has_security = TRUE;
1197 has_security = FALSE;
1202 * Attempt to get the security API we need.
1204 advapi = LoadLibrary("ADVAPI32.DLL");
1205 getsecurityinfo = (gsi_fn_t)GetProcAddress(advapi, "GetSecurityInfo");
1206 if (!getsecurityinfo) {
1208 "Unable to access security APIs. Pageant will\n"
1209 "not run, in case it causes a security breach.",
1210 "Pageant Fatal Error", MB_ICONERROR | MB_OK);
1215 "This program has been compiled for Win9X and will\n"
1216 "not run on NT, in case it causes a security breach.",
1217 "Pageant Fatal Error", MB_ICONERROR | MB_OK);
1226 * Find out if Pageant is already running.
1228 already_running = FALSE;
1230 already_running = TRUE;
1235 wndclass.lpfnWndProc = WndProc;
1236 wndclass.cbClsExtra = 0;
1237 wndclass.cbWndExtra = 0;
1238 wndclass.hInstance = inst;
1239 wndclass.hIcon = LoadIcon (inst,
1240 MAKEINTRESOURCE(IDI_MAINICON));
1241 wndclass.hCursor = LoadCursor (NULL, IDC_IBEAM);
1242 wndclass.hbrBackground = GetStockObject (BLACK_BRUSH);
1243 wndclass.lpszMenuName = NULL;
1244 wndclass.lpszClassName = APPNAME;
1246 RegisterClass (&wndclass);
1249 hwnd = keylist = NULL;
1251 hwnd = CreateWindow (APPNAME, APPNAME,
1252 WS_OVERLAPPEDWINDOW | WS_VSCROLL,
1253 CW_USEDEFAULT, CW_USEDEFAULT,
1254 100, 100, NULL, NULL, inst, NULL);
1256 /* Set up a system tray icon */
1259 NOTIFYICONDATA tnid;
1262 #ifdef NIM_SETVERSION
1264 res = Shell_NotifyIcon(NIM_SETVERSION, &tnid);
1267 tnid.cbSize = sizeof(NOTIFYICONDATA);
1269 tnid.uID = 1; /* unique within this systray use */
1270 tnid.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
1271 tnid.uCallbackMessage = WM_SYSTRAY;
1272 tnid.hIcon = hicon = LoadIcon (instance, MAKEINTRESOURCE(201));
1273 strcpy(tnid.szTip, "Pageant (PuTTY authentication agent)");
1275 res = Shell_NotifyIcon(NIM_ADD, &tnid);
1280 systray_menu = CreatePopupMenu();
1281 /* accelerators used: vkxa */
1282 AppendMenu (systray_menu, MF_ENABLED, IDM_VIEWKEYS, "&View Keys");
1283 AppendMenu (systray_menu, MF_ENABLED, IDM_ADDKEY, "Add &Key");
1284 AppendMenu (systray_menu, MF_ENABLED, IDM_ABOUT, "&About");
1285 AppendMenu (systray_menu, MF_ENABLED, IDM_CLOSE, "E&xit");
1288 ShowWindow (hwnd, SW_HIDE);
1291 * Initialise storage for RSA keys.
1293 rsakeys = newtree234(cmpkeys_rsa);
1294 ssh2keys = newtree234(cmpkeys_ssh2);
1299 * Process the command line and add keys as listed on it.
1300 * If we already determined that we need to spawn a program from above we
1301 * need to ignore the first two arguments. [DBW]
1309 while (*p && isspace(*p)) p++;
1310 if (*p && !isspace(*p)) {
1311 char *q = p, *pp = p;
1312 while (*p && (inquotes || !isspace(*p)))
1315 inquotes = !inquotes;
1325 if (!strcmp(q, "-c")) {
1327 * If we see `-c', then the rest of the
1328 * command line should be treated as a
1329 * command to be spawned.
1331 while (*p && isspace(*p)) p++;
1342 if (command) spawn_cmd (command, show);
1345 * If Pageant was already running, we leave now. If we haven't
1346 * even taken any auxiliary action (spawned a command or added
1349 if (already_running) {
1350 if (!command && !added_keys) {
1351 MessageBox(NULL, "Pageant is already running", "Pageant Error",
1352 MB_ICONERROR | MB_OK);
1354 if (advapi) FreeLibrary(advapi);
1359 * Main message loop.
1361 while (GetMessage(&msg, NULL, 0, 0) == 1) {
1362 TranslateMessage(&msg);
1363 DispatchMessage(&msg);
1366 /* Clean up the system tray icon */
1368 NOTIFYICONDATA tnid;
1370 tnid.cbSize = sizeof(NOTIFYICONDATA);
1374 Shell_NotifyIcon(NIM_DELETE, &tnid);
1376 DestroyMenu(systray_menu);
1379 if (advapi) FreeLibrary(advapi);