5c58ad2d |
1 | /* |
2 | * Pageant: the PuTTY Authentication Agent. |
3 | */ |
4 | |
5 | #include <windows.h> |
123bc6ea |
6 | #ifndef NO_SECURITY |
d70f60ae |
7 | #include <aclapi.h> |
123bc6ea |
8 | #endif |
9b581c37 |
9 | #include <stdio.h> |
49bad831 |
10 | #include <stdlib.h> |
11 | #include <ctype.h> |
ddecd643 |
12 | #include <tchar.h> |
13 | |
5c58ad2d |
14 | #include "ssh.h" |
15 | #include "tree234.h" |
16 | |
17 | #define IDI_MAINICON 200 |
18 | #define IDI_TRAYICON 201 |
19 | |
20 | #define WM_XUSER (WM_USER + 0x2000) |
21 | #define WM_SYSTRAY (WM_XUSER + 6) |
22 | #define WM_SYSTRAY2 (WM_XUSER + 7) |
d70f60ae |
23 | |
24 | #define AGENT_COPYDATA_ID 0x804e50ba /* random goop */ |
25 | |
26 | /* |
27 | * FIXME: maybe some day we can sort this out ... |
28 | */ |
29 | #define AGENT_MAX_MSGLEN 8192 |
5c58ad2d |
30 | |
31 | #define IDM_CLOSE 0x0010 |
32 | #define IDM_VIEWKEYS 0x0020 |
ab162329 |
33 | #define IDM_ADDKEY 0x0030 |
34 | #define IDM_ABOUT 0x0040 |
5c58ad2d |
35 | |
36 | #define APPNAME "Pageant" |
37 | |
5af3a3b6 |
38 | extern char ver[]; |
39 | |
75cab814 |
40 | static HINSTANCE instance; |
41 | static HWND hwnd; |
42 | static HWND keylist; |
43 | static HWND aboutbox; |
44 | static HMENU systray_menu; |
ddecd643 |
45 | static int already_running; |
5c58ad2d |
46 | |
45cebe79 |
47 | static tree234 *rsakeys, *ssh2keys; |
5c58ad2d |
48 | |
75cab814 |
49 | static int has_security; |
123bc6ea |
50 | #ifndef NO_SECURITY |
016ef8ab |
51 | typedef DWORD (WINAPI *gsi_fn_t) |
52 | (HANDLE, SE_OBJECT_TYPE, SECURITY_INFORMATION, |
53 | PSID *, PSID *, PACL *, PACL *, |
54 | PSECURITY_DESCRIPTOR *); |
75cab814 |
55 | static gsi_fn_t getsecurityinfo; |
123bc6ea |
56 | #endif |
016ef8ab |
57 | |
5c58ad2d |
58 | /* |
260f3dec |
59 | * Exports from pageantc.c |
60 | */ |
61 | void agent_query(void *in, int inlen, void **out, int *outlen); |
62 | int agent_exists(void); |
63 | |
64 | /* |
5c58ad2d |
65 | * We need this to link with the RSA code, because rsaencrypt() |
45cebe79 |
66 | * pads its data with random bytes. Since we only use rsadecrypt() |
67 | * and the signing functions, which are deterministic, this should |
68 | * never be called. |
69 | * |
5c58ad2d |
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. |
73 | */ |
74 | int random_byte(void) { |
75 | MessageBox(hwnd, "Internal Error", APPNAME, MB_OK | MB_ICONERROR); |
76 | exit(0); |
77 | } |
78 | |
79 | /* |
45cebe79 |
80 | * Blob structure for passing to the asymmetric SSH2 key compare |
81 | * function, prototyped here. |
82 | */ |
83 | struct blob { |
84 | unsigned char *blob; |
85 | int len; |
86 | }; |
87 | static int cmpkeys_ssh2_asymm(void *av, void *bv); |
88 | |
89 | /* |
5c58ad2d |
90 | * This function is needed to link with the DES code. We need not |
91 | * have it do anything at all. |
92 | */ |
93 | void logevent(char *msg) { |
94 | } |
95 | |
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])) |
101 | |
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); } |
107 | |
108 | #define PASSPHRASE_MAXLEN 512 |
109 | |
d4de2d2a |
110 | struct PassphraseProcStruct { |
111 | char *passphrase; |
112 | char *comment; |
113 | }; |
114 | |
5c58ad2d |
115 | /* |
5af3a3b6 |
116 | * Dialog-box function for the Licence box. |
117 | */ |
118 | static int CALLBACK LicenceProc (HWND hwnd, UINT msg, |
119 | WPARAM wParam, LPARAM lParam) { |
120 | switch (msg) { |
121 | case WM_INITDIALOG: |
122 | return 1; |
123 | case WM_COMMAND: |
124 | switch (LOWORD(wParam)) { |
125 | case IDOK: |
126 | EndDialog(hwnd, 1); |
127 | return 0; |
128 | } |
129 | return 0; |
130 | case WM_CLOSE: |
131 | EndDialog(hwnd, 1); |
132 | return 0; |
133 | } |
134 | return 0; |
135 | } |
136 | |
137 | /* |
138 | * Dialog-box function for the About box. |
139 | */ |
140 | static int CALLBACK AboutProc (HWND hwnd, UINT msg, |
141 | WPARAM wParam, LPARAM lParam) { |
142 | switch (msg) { |
143 | case WM_INITDIALOG: |
144 | SetDlgItemText (hwnd, 100, ver); |
145 | return 1; |
146 | case WM_COMMAND: |
147 | switch (LOWORD(wParam)) { |
148 | case IDOK: |
149 | aboutbox = NULL; |
150 | DestroyWindow (hwnd); |
151 | return 0; |
152 | case 101: |
153 | EnableWindow(hwnd, 0); |
154 | DialogBox (instance, MAKEINTRESOURCE(214), NULL, LicenceProc); |
155 | EnableWindow(hwnd, 1); |
156 | SetActiveWindow(hwnd); |
157 | return 0; |
158 | } |
159 | return 0; |
160 | case WM_CLOSE: |
161 | aboutbox = NULL; |
162 | DestroyWindow (hwnd); |
163 | return 0; |
164 | } |
165 | return 0; |
166 | } |
167 | |
168 | /* |
5c58ad2d |
169 | * Dialog-box function for the passphrase box. |
170 | */ |
171 | static int CALLBACK PassphraseProc(HWND hwnd, UINT msg, |
172 | WPARAM wParam, LPARAM lParam) { |
8e1feb27 |
173 | static char *passphrase = NULL; |
d4de2d2a |
174 | struct PassphraseProcStruct *p; |
5c58ad2d |
175 | |
176 | switch (msg) { |
177 | case WM_INITDIALOG: |
45cebe79 |
178 | /* |
179 | * Centre the window. |
180 | */ |
181 | { /* centre the window */ |
182 | RECT rs, rd; |
183 | HWND hw; |
184 | |
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); |
190 | } |
191 | |
ab162329 |
192 | SetForegroundWindow(hwnd); |
193 | SetWindowPos (hwnd, HWND_TOP, 0, 0, 0, 0, |
194 | SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW); |
d4de2d2a |
195 | p = (struct PassphraseProcStruct *)lParam; |
196 | passphrase = p->passphrase; |
197 | if (p->comment) |
198 | SetDlgItemText(hwnd, 101, p->comment); |
5c58ad2d |
199 | *passphrase = 0; |
8e1feb27 |
200 | SetDlgItemText (hwnd, 102, passphrase); |
5c58ad2d |
201 | return 0; |
202 | case WM_COMMAND: |
203 | switch (LOWORD(wParam)) { |
204 | case IDOK: |
205 | if (*passphrase) |
206 | EndDialog (hwnd, 1); |
207 | else |
208 | MessageBeep (0); |
209 | return 0; |
210 | case IDCANCEL: |
211 | EndDialog (hwnd, 0); |
212 | return 0; |
213 | case 102: /* edit box */ |
8e1feb27 |
214 | if ((HIWORD(wParam) == EN_CHANGE) && passphrase) { |
5c58ad2d |
215 | GetDlgItemText (hwnd, 102, passphrase, PASSPHRASE_MAXLEN-1); |
216 | passphrase[PASSPHRASE_MAXLEN-1] = '\0'; |
217 | } |
218 | return 0; |
219 | } |
220 | return 0; |
221 | case WM_CLOSE: |
222 | EndDialog (hwnd, 0); |
223 | return 0; |
224 | } |
225 | return 0; |
226 | } |
227 | |
228 | /* |
3c0b3d06 |
229 | * Update the visible key list. |
230 | */ |
75cab814 |
231 | static void keylist_update(void) { |
45cebe79 |
232 | struct RSAKey *rkey; |
233 | struct ssh2_userkey *skey; |
d2371c81 |
234 | int i; |
3c0b3d06 |
235 | |
236 | if (keylist) { |
237 | SendDlgItemMessage(keylist, 100, LB_RESETCONTENT, 0, 0); |
d2371c81 |
238 | for (i = 0; NULL != (rkey = index234(rsakeys, i)); i++) { |
45cebe79 |
239 | char listentry[512], *p; |
240 | /* |
241 | * Replace two spaces in the fingerprint with tabs, for |
242 | * nice alignment in the box. |
243 | */ |
244 | strcpy(listentry, "ssh1\t"); |
245 | p = listentry+strlen(listentry); |
246 | rsa_fingerprint(p, sizeof(listentry)-(p-listentry), rkey); |
247 | p = strchr(listentry, ' '); if (p) *p = '\t'; |
248 | p = strchr(listentry, ' '); if (p) *p = '\t'; |
249 | SendDlgItemMessage (keylist, 100, LB_ADDSTRING, |
250 | 0, (LPARAM)listentry); |
251 | } |
d2371c81 |
252 | for (i = 0; NULL != (skey = index234(ssh2keys, i)); i++) { |
1c2a93c4 |
253 | char listentry[512], *p; |
45cebe79 |
254 | int len; |
1c2a93c4 |
255 | /* |
256 | * Replace two spaces in the fingerprint with tabs, for |
257 | * nice alignment in the box. |
258 | */ |
45cebe79 |
259 | p = skey->alg->fingerprint(skey->data); |
260 | strncpy(listentry, p, sizeof(listentry)); |
1c2a93c4 |
261 | p = strchr(listentry, ' '); if (p) *p = '\t'; |
262 | p = strchr(listentry, ' '); if (p) *p = '\t'; |
45cebe79 |
263 | len = strlen(listentry); |
264 | if (len < sizeof(listentry)-2) { |
265 | listentry[len] = '\t'; |
266 | strncpy(listentry+len+1, skey->comment, sizeof(listentry)-len-1); |
267 | } |
3c0b3d06 |
268 | SendDlgItemMessage (keylist, 100, LB_ADDSTRING, |
1c2a93c4 |
269 | 0, (LPARAM)listentry); |
3c0b3d06 |
270 | } |
271 | SendDlgItemMessage (keylist, 100, LB_SETCURSEL, (WPARAM) -1, 0); |
272 | } |
273 | } |
274 | |
275 | /* |
5c58ad2d |
276 | * This function loads a key from a file and adds it. |
277 | */ |
75cab814 |
278 | static void add_keyfile(char *filename) { |
5c58ad2d |
279 | char passphrase[PASSPHRASE_MAXLEN]; |
45cebe79 |
280 | struct RSAKey *rkey; |
281 | struct ssh2_userkey *skey; |
5c58ad2d |
282 | int needs_pass; |
283 | int ret; |
284 | int attempts; |
d4de2d2a |
285 | char *comment; |
286 | struct PassphraseProcStruct pps; |
45cebe79 |
287 | int ver; |
288 | |
289 | ver = keyfile_version(filename); |
290 | if (ver == 0) { |
291 | MessageBox(NULL, "Couldn't load private key.", APPNAME, |
292 | MB_OK | MB_ICONERROR); |
293 | return; |
294 | } |
5c58ad2d |
295 | |
45cebe79 |
296 | if (ver == 1) |
297 | needs_pass = rsakey_encrypted(filename, &comment); |
298 | else |
299 | needs_pass = ssh2_userkey_encrypted(filename, &comment); |
5c58ad2d |
300 | attempts = 0; |
45cebe79 |
301 | if (ver == 1) |
302 | rkey = smalloc(sizeof(*rkey)); |
d4de2d2a |
303 | pps.passphrase = passphrase; |
304 | pps.comment = comment; |
5c58ad2d |
305 | do { |
306 | if (needs_pass) { |
307 | int dlgret; |
308 | dlgret = DialogBoxParam(instance, MAKEINTRESOURCE(210), |
309 | NULL, PassphraseProc, |
d4de2d2a |
310 | (LPARAM)&pps); |
5c58ad2d |
311 | if (!dlgret) { |
dcbde236 |
312 | if (comment) sfree(comment); |
45cebe79 |
313 | if (ver == 1) |
314 | sfree(rkey); |
5c58ad2d |
315 | return; /* operation cancelled */ |
316 | } |
317 | } else |
318 | *passphrase = '\0'; |
45cebe79 |
319 | if (ver == 1) |
320 | ret = loadrsakey(filename, rkey, passphrase); |
321 | else { |
322 | skey = ssh2_load_userkey(filename, passphrase); |
323 | if (skey == SSH2_WRONG_PASSPHRASE) |
324 | ret = -1; |
325 | else if (!skey) |
326 | ret = 0; |
327 | else |
328 | ret = 1; |
329 | } |
5c58ad2d |
330 | attempts++; |
331 | } while (ret == -1); |
dcbde236 |
332 | if (comment) sfree(comment); |
5c58ad2d |
333 | if (ret == 0) { |
5af3a3b6 |
334 | MessageBox(NULL, "Couldn't load private key.", APPNAME, |
5c58ad2d |
335 | MB_OK | MB_ICONERROR); |
45cebe79 |
336 | if (ver == 1) |
337 | sfree(rkey); |
5c58ad2d |
338 | return; |
339 | } |
45cebe79 |
340 | if (ver == 1) { |
ddecd643 |
341 | if (already_running) { |
342 | unsigned char *request, *response; |
343 | int reqlen, clen, resplen; |
344 | |
ddecd643 |
345 | clen = strlen(rkey->comment); |
ddecd643 |
346 | |
347 | reqlen = 4 + 1 + /* length, message type */ |
348 | 4 + /* bit count */ |
349 | ssh1_bignum_length(rkey->modulus) + |
350 | ssh1_bignum_length(rkey->exponent) + |
351 | ssh1_bignum_length(rkey->private_exponent) + |
352 | ssh1_bignum_length(rkey->iqmp) + |
353 | ssh1_bignum_length(rkey->p) + |
354 | ssh1_bignum_length(rkey->q) + |
355 | 4 + clen /* comment */ |
356 | ; |
ddecd643 |
357 | |
358 | request = smalloc(reqlen); |
ddecd643 |
359 | |
ddecd643 |
360 | request[4] = SSH1_AGENTC_ADD_RSA_IDENTITY; |
ddecd643 |
361 | reqlen = 5; |
362 | PUT_32BIT(request+reqlen, bignum_bitcount(rkey->modulus)); |
363 | reqlen += 4; |
364 | reqlen += ssh1_write_bignum(request+reqlen, rkey->modulus); |
365 | reqlen += ssh1_write_bignum(request+reqlen, rkey->exponent); |
366 | reqlen += ssh1_write_bignum(request+reqlen, rkey->private_exponent); |
367 | reqlen += ssh1_write_bignum(request+reqlen, rkey->iqmp); |
368 | reqlen += ssh1_write_bignum(request+reqlen, rkey->p); |
369 | reqlen += ssh1_write_bignum(request+reqlen, rkey->q); |
370 | PUT_32BIT(request+reqlen, clen); |
371 | memcpy(request+reqlen+4, rkey->comment, clen); |
372 | reqlen += 4+clen; |
373 | PUT_32BIT(request, reqlen-4); |
374 | |
ddecd643 |
375 | agent_query(request, reqlen, &response, &resplen); |
ddecd643 |
376 | if (resplen < 5 || response[4] != SSH_AGENT_SUCCESS) |
377 | MessageBox(NULL, "The already running Pageant " |
378 | "refused to add the key.", APPNAME, |
379 | MB_OK | MB_ICONERROR); |
380 | } else { |
381 | if (add234(rsakeys, rkey) != rkey) |
382 | sfree(rkey); /* already present, don't waste RAM */ |
383 | } |
45cebe79 |
384 | } else { |
ddecd643 |
385 | if (already_running) { |
386 | unsigned char *request, *response; |
387 | int reqlen, alglen, clen, keybloblen, resplen; |
ddecd643 |
388 | alglen = strlen(skey->alg->name); |
ddecd643 |
389 | clen = strlen(skey->comment); |
ddecd643 |
390 | |
ddecd643 |
391 | keybloblen = skey->alg->openssh_fmtkey(skey->data, NULL, 0); |
ddecd643 |
392 | |
ddecd643 |
393 | reqlen = 4 + 1 + /* length, message type */ |
394 | 4 + alglen + /* algorithm name */ |
395 | keybloblen + /* key data */ |
396 | 4 + clen /* comment */ |
397 | ; |
ddecd643 |
398 | |
ddecd643 |
399 | request = smalloc(reqlen); |
ddecd643 |
400 | |
401 | request[4] = SSH2_AGENTC_ADD_IDENTITY; |
ddecd643 |
402 | reqlen = 5; |
ddecd643 |
403 | PUT_32BIT(request+reqlen, alglen); |
ddecd643 |
404 | reqlen += 4; |
ddecd643 |
405 | memcpy(request+reqlen, skey->alg->name, alglen); |
ddecd643 |
406 | reqlen += alglen; |
ddecd643 |
407 | reqlen += skey->alg->openssh_fmtkey(skey->data, |
408 | request+reqlen, keybloblen); |
ddecd643 |
409 | PUT_32BIT(request+reqlen, clen); |
ddecd643 |
410 | memcpy(request+reqlen+4, skey->comment, clen); |
ddecd643 |
411 | PUT_32BIT(request, reqlen-4); |
ddecd643 |
412 | reqlen += clen+4; |
413 | |
414 | agent_query(request, reqlen, &response, &resplen); |
ddecd643 |
415 | if (resplen < 5 || response[4] != SSH_AGENT_SUCCESS) |
416 | MessageBox(NULL, "The already running Pageant" |
417 | "refused to add the key.", APPNAME, |
418 | MB_OK | MB_ICONERROR); |
ddecd643 |
419 | } else { |
420 | if (add234(ssh2keys, skey) != skey) { |
421 | skey->alg->freekey(skey->data); |
422 | sfree(skey); /* already present, don't waste RAM */ |
423 | } |
45cebe79 |
424 | } |
425 | } |
5c58ad2d |
426 | } |
427 | |
428 | /* |
429 | * This is the main agent function that answers messages. |
430 | */ |
75cab814 |
431 | static void answer_msg(void *msg) { |
d70f60ae |
432 | unsigned char *p = msg; |
433 | unsigned char *ret = msg; |
5c58ad2d |
434 | int type; |
435 | |
5c58ad2d |
436 | /* |
437 | * Get the message type. |
438 | */ |
439 | type = p[4]; |
440 | |
441 | p += 5; |
5c58ad2d |
442 | switch (type) { |
45cebe79 |
443 | case SSH1_AGENTC_REQUEST_RSA_IDENTITIES: |
5c58ad2d |
444 | /* |
45cebe79 |
445 | * Reply with SSH1_AGENT_RSA_IDENTITIES_ANSWER. |
5c58ad2d |
446 | */ |
447 | { |
5c58ad2d |
448 | struct RSAKey *key; |
449 | int len, nkeys; |
d2371c81 |
450 | int i; |
5c58ad2d |
451 | |
452 | /* |
453 | * Count up the number and length of keys we hold. |
454 | */ |
455 | len = nkeys = 0; |
d2371c81 |
456 | for (i = 0; NULL != (key = index234(rsakeys, i)); i++) { |
5c58ad2d |
457 | nkeys++; |
458 | len += 4; /* length field */ |
459 | len += ssh1_bignum_length(key->exponent); |
460 | len += ssh1_bignum_length(key->modulus); |
461 | len += 4 + strlen(key->comment); |
462 | } |
463 | |
464 | /* |
465 | * Packet header is the obvious five bytes, plus four |
466 | * bytes for the key count. |
467 | */ |
468 | len += 5 + 4; |
d70f60ae |
469 | if (len > AGENT_MAX_MSGLEN) |
470 | goto failure; /* aaargh! too much stuff! */ |
471 | PUT_32BIT(ret, len-4); |
45cebe79 |
472 | ret[4] = SSH1_AGENT_RSA_IDENTITIES_ANSWER; |
d70f60ae |
473 | PUT_32BIT(ret+5, nkeys); |
474 | p = ret + 5 + 4; |
d2371c81 |
475 | for (i = 0; NULL != (key = index234(rsakeys, i)); i++) { |
ddecd643 |
476 | PUT_32BIT(p, bignum_bitcount(key->modulus)); |
d70f60ae |
477 | p += 4; |
478 | p += ssh1_write_bignum(p, key->exponent); |
479 | p += ssh1_write_bignum(p, key->modulus); |
480 | PUT_32BIT(p, strlen(key->comment)); |
481 | memcpy(p+4, key->comment, strlen(key->comment)); |
482 | p += 4 + strlen(key->comment); |
5c58ad2d |
483 | } |
484 | } |
485 | break; |
45cebe79 |
486 | case SSH2_AGENTC_REQUEST_IDENTITIES: |
5c58ad2d |
487 | /* |
45cebe79 |
488 | * Reply with SSH2_AGENT_IDENTITIES_ANSWER. |
489 | */ |
490 | { |
45cebe79 |
491 | struct ssh2_userkey *key; |
492 | int len, nkeys; |
493 | unsigned char *blob; |
494 | int bloblen; |
d2371c81 |
495 | int i; |
45cebe79 |
496 | |
497 | /* |
498 | * Count up the number and length of keys we hold. |
499 | */ |
500 | len = nkeys = 0; |
d2371c81 |
501 | for (i = 0; NULL != (key = index234(ssh2keys, i)); i++) { |
45cebe79 |
502 | nkeys++; |
503 | len += 4; /* length field */ |
504 | blob = key->alg->public_blob(key->data, &bloblen); |
505 | len += bloblen; |
506 | sfree(blob); |
507 | len += 4 + strlen(key->comment); |
508 | } |
509 | |
510 | /* |
511 | * Packet header is the obvious five bytes, plus four |
512 | * bytes for the key count. |
513 | */ |
514 | len += 5 + 4; |
515 | if (len > AGENT_MAX_MSGLEN) |
516 | goto failure; /* aaargh! too much stuff! */ |
517 | PUT_32BIT(ret, len-4); |
518 | ret[4] = SSH2_AGENT_IDENTITIES_ANSWER; |
519 | PUT_32BIT(ret+5, nkeys); |
520 | p = ret + 5 + 4; |
d2371c81 |
521 | for (i = 0; NULL != (key = index234(ssh2keys, i)); i++) { |
45cebe79 |
522 | blob = key->alg->public_blob(key->data, &bloblen); |
523 | PUT_32BIT(p, bloblen); |
524 | p += 4; |
525 | memcpy(p, blob, bloblen); |
526 | p += bloblen; |
527 | sfree(blob); |
528 | PUT_32BIT(p, strlen(key->comment)); |
529 | memcpy(p+4, key->comment, strlen(key->comment)); |
530 | p += 4 + strlen(key->comment); |
531 | } |
532 | } |
533 | break; |
534 | case SSH1_AGENTC_RSA_CHALLENGE: |
535 | /* |
536 | * Reply with either SSH1_AGENT_RSA_RESPONSE or |
5c58ad2d |
537 | * SSH_AGENT_FAILURE, depending on whether we have that key |
538 | * or not. |
539 | */ |
540 | { |
541 | struct RSAKey reqkey, *key; |
542 | Bignum challenge, response; |
543 | unsigned char response_source[48], response_md5[16]; |
544 | struct MD5Context md5c; |
545 | int i, len; |
546 | |
547 | p += 4; |
548 | p += ssh1_read_bignum(p, &reqkey.exponent); |
549 | p += ssh1_read_bignum(p, &reqkey.modulus); |
550 | p += ssh1_read_bignum(p, &challenge); |
551 | memcpy(response_source+32, p, 16); p += 16; |
552 | if (GET_32BIT(p) != 1 || |
553 | (key = find234(rsakeys, &reqkey, NULL)) == NULL) { |
554 | freebn(reqkey.exponent); |
555 | freebn(reqkey.modulus); |
556 | freebn(challenge); |
557 | goto failure; |
558 | } |
559 | response = rsadecrypt(challenge, key); |
560 | for (i = 0; i < 32; i++) |
561 | response_source[i] = bignum_byte(response, 31-i); |
562 | |
563 | MD5Init(&md5c); |
564 | MD5Update(&md5c, response_source, 48); |
565 | MD5Final(response_md5, &md5c); |
566 | memset(response_source, 0, 48); /* burn the evidence */ |
567 | freebn(response); /* and that evidence */ |
568 | freebn(challenge); /* yes, and that evidence */ |
569 | freebn(reqkey.exponent); /* and free some memory ... */ |
570 | freebn(reqkey.modulus); /* ... while we're at it. */ |
571 | |
572 | /* |
573 | * Packet is the obvious five byte header, plus sixteen |
574 | * bytes of MD5. |
575 | */ |
576 | len = 5 + 16; |
d70f60ae |
577 | PUT_32BIT(ret, len-4); |
45cebe79 |
578 | ret[4] = SSH1_AGENT_RSA_RESPONSE; |
d70f60ae |
579 | memcpy(ret+5, response_md5, 16); |
5c58ad2d |
580 | } |
581 | break; |
45cebe79 |
582 | case SSH2_AGENTC_SIGN_REQUEST: |
583 | /* |
584 | * Reply with either SSH2_AGENT_RSA_RESPONSE or |
585 | * SSH_AGENT_FAILURE, depending on whether we have that key |
586 | * or not. |
587 | */ |
588 | { |
589 | struct ssh2_userkey *key; |
590 | struct blob b; |
591 | unsigned char *data, *signature; |
592 | int datalen, siglen, len; |
593 | |
594 | b.len = GET_32BIT(p); |
595 | p += 4; |
596 | b.blob = p; |
597 | p += b.len; |
598 | datalen = GET_32BIT(p); |
599 | p += 4; |
600 | data = p; |
601 | key = find234(ssh2keys, &b, cmpkeys_ssh2_asymm); |
602 | if (!key) |
603 | goto failure; |
604 | signature = key->alg->sign(key->data, data, datalen, &siglen); |
605 | len = 5+4+siglen; |
606 | PUT_32BIT(ret, len-4); |
607 | ret[4] = SSH2_AGENT_SIGN_RESPONSE; |
608 | PUT_32BIT(ret+5, siglen); |
609 | memcpy(ret+5+4, signature, siglen); |
610 | sfree(signature); |
611 | } |
612 | break; |
613 | case SSH1_AGENTC_ADD_RSA_IDENTITY: |
5c58ad2d |
614 | /* |
615 | * Add to the list and return SSH_AGENT_SUCCESS, or |
616 | * SSH_AGENT_FAILURE if the key was malformed. |
617 | */ |
3c0b3d06 |
618 | { |
619 | struct RSAKey *key; |
620 | char *comment; |
dcbde236 |
621 | key = smalloc(sizeof(struct RSAKey)); |
3c0b3d06 |
622 | memset(key, 0, sizeof(key)); |
623 | p += makekey(p, key, NULL, 1); |
624 | p += makeprivate(p, key); |
45cebe79 |
625 | p += ssh1_read_bignum(p, key->iqmp); /* p^-1 mod q */ |
626 | p += ssh1_read_bignum(p, key->p); /* p */ |
627 | p += ssh1_read_bignum(p, key->q); /* q */ |
dcbde236 |
628 | comment = smalloc(GET_32BIT(p)); |
3c0b3d06 |
629 | if (comment) { |
630 | memcpy(comment, p+4, GET_32BIT(p)); |
631 | key->comment = comment; |
632 | } |
633 | PUT_32BIT(ret, 1); |
634 | ret[4] = SSH_AGENT_FAILURE; |
635 | if (add234(rsakeys, key) == key) { |
636 | keylist_update(); |
637 | ret[4] = SSH_AGENT_SUCCESS; |
638 | } else { |
639 | freersakey(key); |
dcbde236 |
640 | sfree(key); |
3c0b3d06 |
641 | } |
642 | } |
5c58ad2d |
643 | break; |
45cebe79 |
644 | case SSH2_AGENTC_ADD_IDENTITY: |
645 | /* |
646 | * Add to the list and return SSH_AGENT_SUCCESS, or |
647 | * SSH_AGENT_FAILURE if the key was malformed. |
648 | */ |
649 | { |
650 | struct ssh2_userkey *key; |
651 | char *comment, *alg; |
652 | int alglen, commlen; |
653 | int bloblen; |
654 | |
655 | key = smalloc(sizeof(struct ssh2_userkey)); |
656 | |
657 | alglen = GET_32BIT(p); p += 4; |
658 | alg = p; p += alglen; |
659 | /* Add further algorithm names here. */ |
660 | if (alglen == 7 && !memcmp(alg, "ssh-rsa", 7)) |
661 | key->alg = &ssh_rsa; |
662 | else { |
663 | sfree(key); |
664 | goto failure; |
665 | } |
666 | |
667 | bloblen = GET_32BIT((unsigned char *)msg) - (p-(unsigned char *)msg-4); |
668 | key->data = key->alg->openssh_createkey(&p, &bloblen); |
669 | if (!key->data) { |
670 | sfree(key); |
671 | goto failure; |
672 | } |
673 | commlen = GET_32BIT(p); p += 4; |
674 | |
675 | comment = smalloc(commlen+1); |
676 | if (comment) { |
677 | memcpy(comment, p, commlen); |
678 | comment[commlen] = '\0'; |
679 | } |
680 | key->comment = comment; |
681 | |
682 | PUT_32BIT(ret, 1); |
683 | ret[4] = SSH_AGENT_FAILURE; |
684 | if (add234(ssh2keys, key) == key) { |
685 | keylist_update(); |
686 | ret[4] = SSH_AGENT_SUCCESS; |
687 | } else { |
688 | key->alg->freekey(key->data); |
689 | sfree(key->comment); |
690 | sfree(key); |
691 | } |
692 | } |
693 | break; |
694 | case SSH1_AGENTC_REMOVE_RSA_IDENTITY: |
5c58ad2d |
695 | /* |
696 | * Remove from the list and return SSH_AGENT_SUCCESS, or |
697 | * perhaps SSH_AGENT_FAILURE if it wasn't in the list to |
698 | * start with. |
699 | */ |
3c0b3d06 |
700 | { |
701 | struct RSAKey reqkey, *key; |
702 | |
703 | p += makekey(p, &reqkey, NULL, 0); |
704 | key = find234(rsakeys, &reqkey, NULL); |
705 | freebn(reqkey.exponent); |
706 | freebn(reqkey.modulus); |
707 | PUT_32BIT(ret, 1); |
708 | ret[4] = SSH_AGENT_FAILURE; |
709 | if (key) { |
710 | del234(rsakeys, key); |
711 | keylist_update(); |
d4de2d2a |
712 | freersakey(key); |
45cebe79 |
713 | sfree(key); |
3c0b3d06 |
714 | ret[4] = SSH_AGENT_SUCCESS; |
715 | } |
716 | } |
5c58ad2d |
717 | break; |
45cebe79 |
718 | case SSH2_AGENTC_REMOVE_IDENTITY: |
719 | /* |
720 | * Remove from the list and return SSH_AGENT_SUCCESS, or |
721 | * perhaps SSH_AGENT_FAILURE if it wasn't in the list to |
722 | * start with. |
723 | */ |
724 | { |
725 | struct ssh2_userkey *key; |
726 | struct blob b; |
727 | |
728 | b.len = GET_32BIT(p); |
729 | p += 4; |
730 | b.blob = p; |
731 | p += b.len; |
732 | key = find234(ssh2keys, &b, cmpkeys_ssh2_asymm); |
733 | if (!key) |
734 | goto failure; |
735 | |
736 | PUT_32BIT(ret, 1); |
737 | ret[4] = SSH_AGENT_FAILURE; |
738 | if (key) { |
739 | del234(ssh2keys, key); |
740 | keylist_update(); |
741 | key->alg->freekey(key->data); |
742 | sfree(key); |
743 | ret[4] = SSH_AGENT_SUCCESS; |
744 | } |
745 | } |
746 | break; |
747 | case SSH1_AGENTC_REMOVE_ALL_RSA_IDENTITIES: |
748 | /* |
749 | * Remove all SSH1 keys. Always returns success. |
750 | */ |
751 | { |
752 | struct RSAKey *rkey; |
45cebe79 |
753 | |
d2371c81 |
754 | while ( (rkey = index234(rsakeys, 0)) != NULL ) { |
45cebe79 |
755 | del234(rsakeys, rkey); |
756 | freersakey(rkey); |
757 | sfree(rkey); |
758 | } |
759 | keylist_update(); |
760 | |
761 | PUT_32BIT(ret, 1); |
762 | ret[4] = SSH_AGENT_SUCCESS; |
763 | } |
764 | break; |
765 | case SSH2_AGENTC_REMOVE_ALL_IDENTITIES: |
766 | /* |
767 | * Remove all SSH2 keys. Always returns success. |
768 | */ |
769 | { |
770 | struct ssh2_userkey *skey; |
45cebe79 |
771 | |
d2371c81 |
772 | while ( (skey = index234(ssh2keys, 0)) != NULL ) { |
45cebe79 |
773 | del234(ssh2keys, skey); |
774 | skey->alg->freekey(skey->data); |
775 | sfree(skey); |
776 | } |
777 | keylist_update(); |
778 | |
779 | PUT_32BIT(ret, 1); |
780 | ret[4] = SSH_AGENT_SUCCESS; |
781 | } |
782 | break; |
5c58ad2d |
783 | default: |
784 | failure: |
785 | /* |
786 | * Unrecognised message. Return SSH_AGENT_FAILURE. |
787 | */ |
d70f60ae |
788 | PUT_32BIT(ret, 1); |
789 | ret[4] = SSH_AGENT_FAILURE; |
5c58ad2d |
790 | break; |
791 | } |
5c58ad2d |
792 | } |
793 | |
794 | /* |
795 | * Key comparison function for the 2-3-4 tree of RSA keys. |
796 | */ |
45cebe79 |
797 | static int cmpkeys_rsa(void *av, void *bv) { |
5c58ad2d |
798 | struct RSAKey *a = (struct RSAKey *)av; |
799 | struct RSAKey *b = (struct RSAKey *)bv; |
800 | Bignum am, bm; |
801 | int alen, blen; |
802 | |
803 | am = a->modulus; |
804 | bm = b->modulus; |
805 | /* |
806 | * Compare by length of moduli. |
807 | */ |
ddecd643 |
808 | alen = bignum_bitcount(am); |
809 | blen = bignum_bitcount(bm); |
5c58ad2d |
810 | if (alen > blen) return +1; else if (alen < blen) return -1; |
811 | /* |
812 | * Now compare by moduli themselves. |
813 | */ |
814 | alen = (alen + 7) / 8; /* byte count */ |
815 | while (alen-- > 0) { |
816 | int abyte, bbyte; |
817 | abyte = bignum_byte(am, alen); |
818 | bbyte = bignum_byte(bm, alen); |
819 | if (abyte > bbyte) return +1; else if (abyte < bbyte) return -1; |
820 | } |
821 | /* |
822 | * Give up. |
823 | */ |
824 | return 0; |
825 | } |
826 | |
45cebe79 |
827 | /* |
828 | * Key comparison function for the 2-3-4 tree of SSH2 keys. |
829 | */ |
830 | static int cmpkeys_ssh2(void *av, void *bv) { |
831 | struct ssh2_userkey *a = (struct ssh2_userkey *)av; |
832 | struct ssh2_userkey *b = (struct ssh2_userkey *)bv; |
833 | int i; |
834 | int alen, blen; |
835 | unsigned char *ablob, *bblob; |
836 | int c; |
837 | |
838 | /* |
839 | * Compare purely by public blob. |
840 | */ |
841 | ablob = a->alg->public_blob(a->data, &alen); |
842 | bblob = b->alg->public_blob(b->data, &blen); |
843 | |
844 | c = 0; |
845 | for (i = 0; i < alen && i < blen; i++) { |
846 | if (ablob[i] < bblob[i]) { |
847 | c = -1; break; |
848 | } else if (ablob[i] > bblob[i]) { |
849 | c = +1; break; |
850 | } |
851 | } |
852 | if (c == 0 && i < alen) c = +1; /* a is longer */ |
853 | if (c == 0 && i < blen) c = -1; /* a is longer */ |
854 | |
855 | sfree(ablob); |
856 | sfree(bblob); |
857 | |
858 | return c; |
859 | } |
860 | |
861 | /* |
862 | * Key comparison function for looking up a blob in the 2-3-4 tree |
863 | * of SSH2 keys. |
864 | */ |
865 | static int cmpkeys_ssh2_asymm(void *av, void *bv) { |
866 | struct blob *a = (struct blob *)av; |
867 | struct ssh2_userkey *b = (struct ssh2_userkey *)bv; |
868 | int i; |
869 | int alen, blen; |
870 | unsigned char *ablob, *bblob; |
871 | int c; |
872 | |
873 | /* |
874 | * Compare purely by public blob. |
875 | */ |
876 | ablob = a->blob; |
877 | alen = a->len; |
878 | bblob = b->alg->public_blob(b->data, &blen); |
879 | |
880 | c = 0; |
881 | for (i = 0; i < alen && i < blen; i++) { |
882 | if (ablob[i] < bblob[i]) { |
883 | c = -1; break; |
884 | } else if (ablob[i] > bblob[i]) { |
885 | c = +1; break; |
886 | } |
887 | } |
888 | if (c == 0 && i < alen) c = +1; /* a is longer */ |
889 | if (c == 0 && i < blen) c = -1; /* a is longer */ |
890 | |
891 | sfree(bblob); |
892 | |
893 | return c; |
894 | } |
895 | |
5c58ad2d |
896 | static void error(char *s) { |
897 | MessageBox(hwnd, s, APPNAME, MB_OK | MB_ICONERROR); |
898 | } |
899 | |
900 | /* |
ab162329 |
901 | * Prompt for a key file to add, and add it. |
902 | */ |
903 | static void prompt_add_keyfile(void) { |
904 | OPENFILENAME of; |
905 | char filename[FILENAME_MAX]; |
906 | memset(&of, 0, sizeof(of)); |
907 | #ifdef OPENFILENAME_SIZE_VERSION_400 |
908 | of.lStructSize = OPENFILENAME_SIZE_VERSION_400; |
909 | #else |
910 | of.lStructSize = sizeof(of); |
911 | #endif |
912 | of.hwndOwner = hwnd; |
913 | of.lpstrFilter = "All Files\0*\0\0\0"; |
914 | of.lpstrCustomFilter = NULL; |
915 | of.nFilterIndex = 1; |
916 | of.lpstrFile = filename; *filename = '\0'; |
917 | of.nMaxFile = sizeof(filename); |
918 | of.lpstrFileTitle = NULL; |
919 | of.lpstrInitialDir = NULL; |
920 | of.lpstrTitle = "Select Private Key File"; |
921 | of.Flags = 0; |
922 | if (GetOpenFileName(&of)) { |
923 | add_keyfile(filename); |
924 | keylist_update(); |
925 | } |
926 | } |
927 | |
928 | /* |
5c58ad2d |
929 | * Dialog-box function for the key list box. |
930 | */ |
931 | static int CALLBACK KeyListProc(HWND hwnd, UINT msg, |
932 | WPARAM wParam, LPARAM lParam) { |
45cebe79 |
933 | struct RSAKey *rkey; |
934 | struct ssh2_userkey *skey; |
5c58ad2d |
935 | |
936 | switch (msg) { |
937 | case WM_INITDIALOG: |
45cebe79 |
938 | /* |
939 | * Centre the window. |
940 | */ |
941 | { /* centre the window */ |
942 | RECT rs, rd; |
943 | HWND hw; |
944 | |
945 | hw = GetDesktopWindow(); |
946 | if (GetWindowRect (hw, &rs) && GetWindowRect (hwnd, &rd)) |
947 | MoveWindow (hwnd, (rs.right + rs.left + rd.left - rd.right)/2, |
948 | (rs.bottom + rs.top + rd.top - rd.bottom)/2, |
949 | rd.right-rd.left, rd.bottom-rd.top, TRUE); |
950 | } |
951 | |
1c2a93c4 |
952 | keylist = hwnd; |
953 | { |
45cebe79 |
954 | static int tabs[] = {35, 60, 210}; |
955 | SendDlgItemMessage (hwnd, 100, LB_SETTABSTOPS, |
956 | sizeof(tabs)/sizeof(*tabs), (LPARAM) tabs); |
1c2a93c4 |
957 | } |
958 | keylist_update(); |
5c58ad2d |
959 | return 0; |
960 | case WM_COMMAND: |
961 | switch (LOWORD(wParam)) { |
962 | case IDOK: |
963 | case IDCANCEL: |
964 | keylist = NULL; |
965 | DestroyWindow(hwnd); |
966 | return 0; |
967 | case 101: /* add key */ |
968 | if (HIWORD(wParam) == BN_CLICKED || |
969 | HIWORD(wParam) == BN_DOUBLECLICKED) { |
ab162329 |
970 | prompt_add_keyfile(); |
5c58ad2d |
971 | } |
972 | return 0; |
973 | case 102: /* remove key */ |
974 | if (HIWORD(wParam) == BN_CLICKED || |
975 | HIWORD(wParam) == BN_DOUBLECLICKED) { |
976 | int n = SendDlgItemMessage (hwnd, 100, LB_GETCURSEL, 0, 0); |
d2371c81 |
977 | int i; |
f70a606e |
978 | if (n == LB_ERR) { |
5c58ad2d |
979 | MessageBeep(0); |
980 | break; |
981 | } |
d2371c81 |
982 | for (i = 0; NULL != (rkey = index234(rsakeys, i)); i++) |
5c58ad2d |
983 | if (n-- == 0) |
984 | break; |
45cebe79 |
985 | if (rkey) { |
986 | del234(rsakeys, rkey); |
987 | freersakey(rkey); |
988 | sfree(rkey); |
989 | } else { |
d2371c81 |
990 | for (i = 0; NULL != (skey = index234(ssh2keys, i)); i++) |
45cebe79 |
991 | if (n-- == 0) |
992 | break; |
993 | if (skey) { |
994 | del234(ssh2keys, skey); |
995 | skey->alg->freekey(skey->data); |
996 | sfree(skey); |
997 | } |
998 | } |
1c2a93c4 |
999 | keylist_update(); |
5c58ad2d |
1000 | } |
1001 | return 0; |
1002 | } |
1003 | return 0; |
1004 | case WM_CLOSE: |
1005 | keylist = NULL; |
1006 | DestroyWindow(hwnd); |
1007 | return 0; |
1008 | } |
1009 | return 0; |
1010 | } |
1011 | |
1012 | static LRESULT CALLBACK WndProc (HWND hwnd, UINT message, |
1013 | WPARAM wParam, LPARAM lParam) { |
1014 | int ret; |
1015 | static int menuinprogress; |
1016 | |
1017 | switch (message) { |
1018 | case WM_SYSTRAY: |
1019 | if (lParam == WM_RBUTTONUP) { |
1020 | POINT cursorpos; |
1021 | GetCursorPos(&cursorpos); |
1022 | PostMessage(hwnd, WM_SYSTRAY2, cursorpos.x, cursorpos.y); |
905beba5 |
1023 | } else if (lParam == WM_LBUTTONDBLCLK) { |
1024 | /* Equivalent to IDM_VIEWKEYS. */ |
1025 | PostMessage(hwnd, WM_COMMAND, IDM_VIEWKEYS, 0); |
5c58ad2d |
1026 | } |
1027 | break; |
1028 | case WM_SYSTRAY2: |
1029 | if (!menuinprogress) { |
1030 | menuinprogress = 1; |
1031 | SetForegroundWindow(hwnd); |
1032 | ret = TrackPopupMenu(systray_menu, |
1033 | TPM_RIGHTALIGN | TPM_BOTTOMALIGN | |
1034 | TPM_RIGHTBUTTON, |
1035 | wParam, lParam, 0, hwnd, NULL); |
1036 | menuinprogress = 0; |
1037 | } |
1038 | break; |
1039 | case WM_COMMAND: |
1040 | case WM_SYSCOMMAND: |
1041 | switch (wParam & ~0xF) { /* low 4 bits reserved to Windows */ |
1042 | case IDM_CLOSE: |
1043 | SendMessage(hwnd, WM_CLOSE, 0, 0); |
1044 | break; |
1045 | case IDM_VIEWKEYS: |
1046 | if (!keylist) { |
1047 | keylist = CreateDialog (instance, MAKEINTRESOURCE(211), |
1048 | NULL, KeyListProc); |
1049 | ShowWindow (keylist, SW_SHOWNORMAL); |
905beba5 |
1050 | /* |
1051 | * Sometimes the window comes up minimised / hidden |
1052 | * for no obvious reason. Prevent this. |
1053 | */ |
1054 | SetForegroundWindow(keylist); |
1055 | SetWindowPos (keylist, HWND_TOP, 0, 0, 0, 0, |
1056 | SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW); |
5c58ad2d |
1057 | } |
1058 | break; |
ab162329 |
1059 | case IDM_ADDKEY: |
1060 | prompt_add_keyfile(); |
1061 | break; |
5af3a3b6 |
1062 | case IDM_ABOUT: |
1063 | if (!aboutbox) { |
1064 | aboutbox = CreateDialog (instance, MAKEINTRESOURCE(213), |
1065 | NULL, AboutProc); |
1066 | ShowWindow (aboutbox, SW_SHOWNORMAL); |
1067 | /* |
1068 | * Sometimes the window comes up minimised / hidden |
1069 | * for no obvious reason. Prevent this. |
1070 | */ |
1071 | SetForegroundWindow(aboutbox); |
1072 | SetWindowPos (aboutbox, HWND_TOP, 0, 0, 0, 0, |
1073 | SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW); |
1074 | } |
1075 | break; |
5c58ad2d |
1076 | } |
1077 | break; |
1078 | case WM_DESTROY: |
1079 | PostQuitMessage (0); |
1080 | return 0; |
1081 | case WM_COPYDATA: |
1082 | { |
1083 | COPYDATASTRUCT *cds; |
d70f60ae |
1084 | char *mapname; |
1085 | void *p; |
1086 | HANDLE filemap, proc; |
1087 | PSID mapowner, procowner; |
1088 | PSECURITY_DESCRIPTOR psd1 = NULL, psd2 = NULL; |
1089 | int ret = 0; |
5c58ad2d |
1090 | |
1091 | cds = (COPYDATASTRUCT *)lParam; |
d70f60ae |
1092 | if (cds->dwData != AGENT_COPYDATA_ID) |
1093 | return 0; /* not our message, mate */ |
1094 | mapname = (char *)cds->lpData; |
1095 | if (mapname[cds->cbData - 1] != '\0') |
1096 | return 0; /* failure to be ASCIZ! */ |
1097 | #ifdef DEBUG_IPC |
765c4200 |
1098 | debug(("mapname is :%s:\n", mapname)); |
d70f60ae |
1099 | #endif |
1100 | filemap = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, mapname); |
1101 | #ifdef DEBUG_IPC |
765c4200 |
1102 | debug(("filemap is %p\n", filemap)); |
d70f60ae |
1103 | #endif |
1104 | if (filemap != NULL && filemap != INVALID_HANDLE_VALUE) { |
1105 | int rc; |
123bc6ea |
1106 | #ifndef NO_SECURITY |
016ef8ab |
1107 | if (has_security) { |
1108 | if ((proc = OpenProcess(MAXIMUM_ALLOWED, FALSE, |
1109 | GetCurrentProcessId())) == NULL) { |
d70f60ae |
1110 | #ifdef DEBUG_IPC |
765c4200 |
1111 | debug(("couldn't get handle for process\n")); |
d70f60ae |
1112 | #endif |
016ef8ab |
1113 | return 0; |
1114 | } |
1115 | if (getsecurityinfo(proc, SE_KERNEL_OBJECT, |
1116 | OWNER_SECURITY_INFORMATION, |
1117 | &procowner, NULL, NULL, NULL, |
1118 | &psd2) != ERROR_SUCCESS) { |
d70f60ae |
1119 | #ifdef DEBUG_IPC |
765c4200 |
1120 | debug(("couldn't get owner info for process\n")); |
d70f60ae |
1121 | #endif |
016ef8ab |
1122 | CloseHandle(proc); |
1123 | return 0; /* unable to get security info */ |
1124 | } |
d70f60ae |
1125 | CloseHandle(proc); |
016ef8ab |
1126 | if ((rc = getsecurityinfo(filemap, SE_KERNEL_OBJECT, |
1127 | OWNER_SECURITY_INFORMATION, |
1128 | &mapowner, NULL, NULL, NULL, |
1129 | &psd1) != ERROR_SUCCESS)) { |
d70f60ae |
1130 | #ifdef DEBUG_IPC |
765c4200 |
1131 | debug(("couldn't get owner info for filemap: %d\n", rc)); |
d70f60ae |
1132 | #endif |
016ef8ab |
1133 | return 0; |
1134 | } |
1135 | #ifdef DEBUG_IPC |
765c4200 |
1136 | debug(("got security stuff\n")); |
016ef8ab |
1137 | #endif |
1138 | if (!EqualSid(mapowner, procowner)) |
1139 | return 0; /* security ID mismatch! */ |
d70f60ae |
1140 | #ifdef DEBUG_IPC |
765c4200 |
1141 | debug(("security stuff matched\n")); |
d70f60ae |
1142 | #endif |
016ef8ab |
1143 | LocalFree(psd1); |
1144 | LocalFree(psd2); |
1145 | } else { |
d70f60ae |
1146 | #ifdef DEBUG_IPC |
765c4200 |
1147 | debug(("security APIs not present\n")); |
d70f60ae |
1148 | #endif |
016ef8ab |
1149 | } |
123bc6ea |
1150 | #endif |
d70f60ae |
1151 | p = MapViewOfFile(filemap, FILE_MAP_WRITE, 0, 0, 0); |
1152 | #ifdef DEBUG_IPC |
765c4200 |
1153 | debug(("p is %p\n", p)); |
1154 | {int i; for(i=0;i<5;i++)debug(("p[%d]=%02x\n", i, ((unsigned char *)p)[i]));} |
d70f60ae |
1155 | #endif |
1156 | answer_msg(p); |
1157 | ret = 1; |
1158 | UnmapViewOfFile(p); |
1159 | } |
1160 | CloseHandle(filemap); |
1161 | return ret; |
5c58ad2d |
1162 | } |
5c58ad2d |
1163 | } |
1164 | |
1165 | return DefWindowProc (hwnd, message, wParam, lParam); |
1166 | } |
1167 | |
ddecd643 |
1168 | /* |
1169 | * Fork and Exec the command in cmdline. [DBW] |
1170 | */ |
1171 | void spawn_cmd(char *cmdline, int show) { |
1172 | if (ShellExecute(NULL, _T("open"), cmdline, |
1173 | NULL, NULL, show) <= (HINSTANCE) 32) { |
1174 | TCHAR sMsg[140]; |
1175 | sprintf(sMsg, _T("Failed to run \"%.100s\", Error: %d"), cmdline, |
1176 | GetLastError()); |
1177 | MessageBox(NULL, sMsg, APPNAME, MB_OK | MB_ICONEXCLAMATION); |
1178 | } |
1179 | } |
1180 | |
5c58ad2d |
1181 | int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show) { |
1182 | WNDCLASS wndclass; |
5c58ad2d |
1183 | MSG msg; |
016ef8ab |
1184 | OSVERSIONINFO osi; |
1185 | HMODULE advapi; |
ddecd643 |
1186 | char *command = NULL; |
1187 | int added_keys = 0; |
016ef8ab |
1188 | |
1189 | /* |
1190 | * Determine whether we're an NT system (should have security |
1191 | * APIs) or a non-NT system (don't do security). |
1192 | */ |
1193 | memset(&osi, 0, sizeof(OSVERSIONINFO)); |
1194 | osi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); |
1195 | if (GetVersionEx(&osi) && osi.dwPlatformId==VER_PLATFORM_WIN32_NT) { |
1196 | has_security = TRUE; |
1197 | } else |
1198 | has_security = FALSE; |
1199 | |
1200 | if (has_security) { |
123bc6ea |
1201 | #ifndef NO_SECURITY |
016ef8ab |
1202 | /* |
ddecd643 |
1203 | * Attempt to get the security API we need. |
016ef8ab |
1204 | */ |
1205 | advapi = LoadLibrary("ADVAPI32.DLL"); |
1206 | getsecurityinfo = (gsi_fn_t)GetProcAddress(advapi, "GetSecurityInfo"); |
1207 | if (!getsecurityinfo) { |
1208 | MessageBox(NULL, |
1209 | "Unable to access security APIs. Pageant will\n" |
1210 | "not run, in case it causes a security breach.", |
1211 | "Pageant Fatal Error", MB_ICONERROR | MB_OK); |
1212 | return 1; |
1213 | } |
123bc6ea |
1214 | #else |
1215 | MessageBox(NULL, |
1216 | "This program has been compiled for Win9X and will\n" |
1217 | "not run on NT, in case it causes a security breach.", |
1218 | "Pageant Fatal Error", MB_ICONERROR | MB_OK); |
1219 | return 1; |
1220 | #endif |
016ef8ab |
1221 | } else |
1222 | advapi = NULL; |
5c58ad2d |
1223 | |
1224 | instance = inst; |
1225 | |
ddecd643 |
1226 | /* |
1227 | * Find out if Pageant is already running. |
1228 | */ |
1229 | already_running = FALSE; |
260f3dec |
1230 | if (agent_exists()) |
ddecd643 |
1231 | already_running = TRUE; |
1232 | else { |
1233 | |
1234 | if (!prev) { |
1235 | wndclass.style = 0; |
1236 | wndclass.lpfnWndProc = WndProc; |
1237 | wndclass.cbClsExtra = 0; |
1238 | wndclass.cbWndExtra = 0; |
1239 | wndclass.hInstance = inst; |
1240 | wndclass.hIcon = LoadIcon (inst, |
1241 | MAKEINTRESOURCE(IDI_MAINICON)); |
1242 | wndclass.hCursor = LoadCursor (NULL, IDC_IBEAM); |
1243 | wndclass.hbrBackground = GetStockObject (BLACK_BRUSH); |
1244 | wndclass.lpszMenuName = NULL; |
1245 | wndclass.lpszClassName = APPNAME; |
1246 | |
1247 | RegisterClass (&wndclass); |
1248 | } |
5c58ad2d |
1249 | |
ddecd643 |
1250 | hwnd = keylist = NULL; |
5c58ad2d |
1251 | |
ddecd643 |
1252 | hwnd = CreateWindow (APPNAME, APPNAME, |
1253 | WS_OVERLAPPEDWINDOW | WS_VSCROLL, |
1254 | CW_USEDEFAULT, CW_USEDEFAULT, |
1255 | 100, 100, NULL, NULL, inst, NULL); |
5c58ad2d |
1256 | |
ddecd643 |
1257 | /* Set up a system tray icon */ |
1258 | { |
1259 | BOOL res; |
1260 | NOTIFYICONDATA tnid; |
1261 | HICON hicon; |
5c58ad2d |
1262 | |
1263 | #ifdef NIM_SETVERSION |
ddecd643 |
1264 | tnid.uVersion = 0; |
1265 | res = Shell_NotifyIcon(NIM_SETVERSION, &tnid); |
5c58ad2d |
1266 | #endif |
1267 | |
ddecd643 |
1268 | tnid.cbSize = sizeof(NOTIFYICONDATA); |
1269 | tnid.hWnd = hwnd; |
1270 | tnid.uID = 1; /* unique within this systray use */ |
1271 | tnid.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP; |
1272 | tnid.uCallbackMessage = WM_SYSTRAY; |
1273 | tnid.hIcon = hicon = LoadIcon (instance, MAKEINTRESOURCE(201)); |
1274 | strcpy(tnid.szTip, "Pageant (PuTTY authentication agent)"); |
1275 | |
1276 | res = Shell_NotifyIcon(NIM_ADD, &tnid); |
1277 | |
1278 | if (hicon) |
1279 | DestroyIcon(hicon); |
1280 | |
1281 | systray_menu = CreatePopupMenu(); |
1282 | /* accelerators used: vkxa */ |
1283 | AppendMenu (systray_menu, MF_ENABLED, IDM_VIEWKEYS, "&View Keys"); |
1284 | AppendMenu (systray_menu, MF_ENABLED, IDM_ADDKEY, "Add &Key"); |
1285 | AppendMenu (systray_menu, MF_ENABLED, IDM_ABOUT, "&About"); |
1286 | AppendMenu (systray_menu, MF_ENABLED, IDM_CLOSE, "E&xit"); |
1287 | } |
5c58ad2d |
1288 | |
ddecd643 |
1289 | ShowWindow (hwnd, SW_HIDE); |
5c58ad2d |
1290 | |
ddecd643 |
1291 | /* |
1292 | * Initialise storage for RSA keys. |
1293 | */ |
1294 | rsakeys = newtree234(cmpkeys_rsa); |
1295 | ssh2keys = newtree234(cmpkeys_ssh2); |
1296 | |
1297 | } |
dacbd0e8 |
1298 | |
1299 | /* |
45cebe79 |
1300 | * Process the command line and add keys as listed on it. |
ddecd643 |
1301 | * If we already determined that we need to spawn a program from above we |
1302 | * need to ignore the first two arguments. [DBW] |
5c58ad2d |
1303 | */ |
1304 | { |
8c42f28c |
1305 | char *p; |
1306 | int inquotes = 0; |
ddecd643 |
1307 | int ignorearg = 0; |
8c42f28c |
1308 | p = cmdline; |
dacbd0e8 |
1309 | while (*p) { |
1310 | while (*p && isspace(*p)) p++; |
1311 | if (*p && !isspace(*p)) { |
8c42f28c |
1312 | char *q = p, *pp = p; |
1313 | while (*p && (inquotes || !isspace(*p))) |
1314 | { |
1315 | if (*p == '"') { |
1316 | inquotes = !inquotes; |
1317 | p++; |
1318 | continue; |
1319 | } |
1320 | *pp++ = *p++; |
1321 | } |
1322 | if (*pp) { |
1323 | if (*p) p++; |
1324 | *pp++ = '\0'; |
1325 | } |
ddecd643 |
1326 | if (!strcmp(q, "-c")) { |
1327 | /* |
1328 | * If we see `-c', then the rest of the |
1329 | * command line should be treated as a |
1330 | * command to be spawned. |
1331 | */ |
1332 | while (*p && isspace(*p)) p++; |
1333 | command = p; |
1334 | break; |
1335 | } else { |
1336 | add_keyfile(q); |
1337 | added_keys = TRUE; |
1338 | } |
dacbd0e8 |
1339 | } |
1340 | } |
5c58ad2d |
1341 | } |
1342 | |
ddecd643 |
1343 | if (command) spawn_cmd (command, show); |
1344 | |
1345 | /* |
1346 | * If Pageant was already running, we leave now. If we haven't |
1347 | * even taken any auxiliary action (spawned a command or added |
1348 | * keys), complain. |
1349 | */ |
1350 | if (already_running) { |
1351 | if (!command && !added_keys) { |
1352 | MessageBox(NULL, "Pageant is already running", "Pageant Error", |
1353 | MB_ICONERROR | MB_OK); |
1354 | } |
1355 | if (advapi) FreeLibrary(advapi); |
1356 | return 0; |
1357 | } |
1358 | |
5c58ad2d |
1359 | /* |
dacbd0e8 |
1360 | * Main message loop. |
5c58ad2d |
1361 | */ |
5c58ad2d |
1362 | while (GetMessage(&msg, NULL, 0, 0) == 1) { |
1363 | TranslateMessage(&msg); |
1364 | DispatchMessage(&msg); |
1365 | } |
1366 | |
1367 | /* Clean up the system tray icon */ |
1368 | { |
1369 | NOTIFYICONDATA tnid; |
1370 | |
1371 | tnid.cbSize = sizeof(NOTIFYICONDATA); |
1372 | tnid.hWnd = hwnd; |
1373 | tnid.uID = 1; |
1374 | |
1375 | Shell_NotifyIcon(NIM_DELETE, &tnid); |
1376 | |
1377 | DestroyMenu(systray_menu); |
1378 | } |
1379 | |
016ef8ab |
1380 | if (advapi) FreeLibrary(advapi); |
5c58ad2d |
1381 | exit(msg.wParam); |
1382 | } |