24 |
#endif |
#endif |
25 |
|
|
26 |
#include <windows.h> |
#include <windows.h> |
|
#include <windows.h> |
|
27 |
#include <ctype.h> |
#include <ctype.h> |
28 |
|
|
29 |
#include "resource.h" |
#include "resource.h" |
37 |
#include "wptUTF8.h" |
#include "wptUTF8.h" |
38 |
#include "wptErrors.h" |
#include "wptErrors.h" |
39 |
#include "wptTypes.h" |
#include "wptTypes.h" |
40 |
#include "wptKeyList.h" |
#include "wptKeylist.h" |
41 |
#include "wptAgent.h" |
#include "wptAgent.h" |
42 |
#include "wptRegistry.h" |
#include "wptRegistry.h" |
43 |
|
|
44 |
const char* get_symkey_algo (int algo); |
const char* get_symkey_algo (int algo); |
45 |
|
|
46 |
#define item_ctrl_id( cmd ) \ |
#define item_ctrl_id(cmd) \ |
47 |
((cmd) == GPG_CMD_DECRYPT? IDC_DECRYPT_PWD : IDC_DECRYPT_SIGN_PWD) |
((cmd) == GPG_CMD_DECRYPT? IDC_DECRYPT_PWD : IDC_DECRYPT_SIGN_PWD) |
48 |
|
|
49 |
#define item_ctrl_id2(cmd) \ |
#define item_ctrl_id2(cmd) \ |
65 |
static BOOL CALLBACK |
static BOOL CALLBACK |
66 |
passphrase_callback_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
passphrase_callback_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
67 |
{ |
{ |
68 |
static passphrase_cb_s * c; |
static passphrase_cb_s *c; |
69 |
gpgme_decrypt_result_t res; |
gpgme_decrypt_result_t res=NULL; |
70 |
gpgme_sign_result_t res_sig; |
gpgme_sign_result_t res_sig=NULL; |
71 |
gpgme_key_t key; |
gpgme_key_t key; |
72 |
gpgme_recipient_t recip, r; |
gpgme_recipient_t recip=NULL, r; |
73 |
void *ctx = NULL, *item; |
void *item; |
74 |
const char *id; |
const char *id; |
75 |
char *info; |
char *info; |
76 |
int n; |
int n; |
77 |
|
|
78 |
switch (msg) { |
switch (msg) { |
79 |
|
case WM_ACTIVATE: |
80 |
|
safe_edit_control_init (dlg, item_ctrl_id (c->gpg_cmd)); |
81 |
|
break; |
82 |
|
|
83 |
|
case WM_DESTROY: |
84 |
|
safe_edit_control_free (dlg, item_ctrl_id (c->gpg_cmd)); |
85 |
|
break; |
86 |
|
|
87 |
case WM_INITDIALOG: |
case WM_INITDIALOG: |
88 |
c = (passphrase_cb_s *)lparam; |
c = (passphrase_cb_s *)lparam; |
89 |
if (!c) |
if (!c) |
90 |
BUG (0); |
BUG (0); |
91 |
|
SetDlgItemText (dlg, IDCANCEL, _("&Cancel")); |
92 |
|
SetDlgItemText (dlg, IDC_DECRYPT_HIDE, _("&Hide Typing")); |
93 |
SetWindowText (dlg, c->title); |
SetWindowText (dlg, c->title); |
94 |
if (c->gpg_cmd == GPG_CMD_DECRYPT) { |
if (c->gpg_cmd == GPG_CMD_DECRYPT) { |
95 |
SetDlgItemText (dlg, IDC_DECRYPT_LISTINF, |
SetDlgItemText (dlg, IDC_DECRYPT_LISTINF, |
96 |
_("Encrypted with the following public key(s)")); |
_("Encrypted with the following public key(s)")); |
97 |
CheckDlgButton (dlg, IDC_DECRYPT_HIDE, BST_CHECKED); |
CheckDlgButton (dlg, IDC_DECRYPT_HIDE, BST_CHECKED); |
98 |
} |
} |
99 |
else if (c->gpg_cmd == GPG_CMD_SIGN) |
else if (c->gpg_cmd == GPG_CMD_SIGN) { |
100 |
|
SetDlgItemText (dlg, IDC_DECRYPT_SIGN_HIDE, _("&Hide Typing")); |
101 |
CheckDlgButton (dlg, IDC_DECRYPT_SIGN_HIDE, BST_CHECKED); |
CheckDlgButton (dlg, IDC_DECRYPT_SIGN_HIDE, BST_CHECKED); |
102 |
|
} |
103 |
|
/* Because it depends on the order the keys are stored in the |
104 |
|
keyring whether res->recipients is complete or not, we also |
105 |
|
support that the recipients were externally extracted and then |
106 |
|
we use this list. */ |
107 |
if (c->recipients) |
if (c->recipients) |
108 |
recip = c->recipients; /* recipients were already extracted. */ |
recip = c->recipients; /* recipients were already extracted. */ |
109 |
else { |
else { |
|
/* XXX: not all ENCRYPT_TO entries are listed here. */ |
|
110 |
res = gpgme_op_decrypt_result (c->gpg); |
res = gpgme_op_decrypt_result (c->gpg); |
111 |
if (res && res->recipients) |
if (res && res->recipients) |
112 |
recip = res->recipients; |
recip = res->recipients; |
113 |
} |
} |
114 |
if (recip != NULL && c->gpg_cmd == GPG_CMD_DECRYPT) { |
if (recip != NULL && c->gpg_cmd == GPG_CMD_DECRYPT) { |
115 |
for (r = res->recipients; r; r = r->next) { |
for (r = recip; r; r = r->next) { |
116 |
get_pubkey (r->keyid, &key); |
get_pubkey (r->keyid, &key); |
117 |
if (key) { |
if (key) { |
118 |
|
gpgme_user_id_t u = key->uids; |
119 |
char *uid; |
char *uid; |
120 |
id = key->uids->name; |
|
121 |
|
id = u->name; |
122 |
if (!id) |
if (!id) |
123 |
id = _("Invalid User ID"); |
id = _("Invalid User ID"); |
124 |
uid = utf8_to_wincp (id, strlen (id)); |
uid = utf8_to_native (id); |
125 |
info = new char [32+strlen (uid)+1 + 4 + strlen (r->keyid)+1 |
n = 32+strlen (uid)+1+4+strlen (r->keyid)+1; |
126 |
+ strlen (key->uids->email)+1]; |
if (u->email) |
127 |
|
n += strlen (u->email)+1; |
128 |
|
info = new char [n+1]; |
129 |
if (!info) |
if (!info) |
130 |
BUG (NULL); |
BUG (NULL); |
131 |
sprintf (info, "%s <%s> (%s, 0x%s)", uid, key->uids->email, |
if (!u->email || strlen (u->email) < 1) |
132 |
get_key_pubalgo (r->pubkey_algo), r->keyid+8); |
sprintf (info, "%s (%s, 0x%s)", uid, |
133 |
|
get_key_pubalgo (r->pubkey_algo), r->keyid+8); |
134 |
|
else |
135 |
|
sprintf (info, "%s <%s> (%s, 0x%s)", uid, u->email, |
136 |
|
get_key_pubalgo (r->pubkey_algo), r->keyid+8); |
137 |
free (uid); |
free (uid); |
|
|
|
138 |
} |
} |
139 |
else { |
else { |
140 |
info = new char [32 + strlen (r->keyid)+1 + 4]; |
info = new char [32 + strlen (r->keyid)+1 + 4]; |
175 |
} |
} |
176 |
center_window (dlg, NULL); |
center_window (dlg, NULL); |
177 |
SetForegroundWindow (dlg); |
SetForegroundWindow (dlg); |
|
set_active_window (dlg); |
|
178 |
return FALSE; |
return FALSE; |
179 |
|
|
180 |
case WM_SYSCOMMAND: |
case WM_SYSCOMMAND: |
216 |
c->pwd = new char[n+2]; |
c->pwd = new char[n+2]; |
217 |
if (!c->pwd) |
if (!c->pwd) |
218 |
BUG (NULL); |
BUG (NULL); |
219 |
GetDlgItemText (dlg, item_ctrl_id (c->gpg_cmd), c->pwd, n+1); |
SafeGetDlgItemText (dlg, item_ctrl_id (c->gpg_cmd), |
220 |
|
c->pwd, n+1); |
221 |
} |
} |
222 |
res = gpgme_op_decrypt_result (c->gpg); |
res = gpgme_op_decrypt_result (c->gpg); |
223 |
if (!res) |
if (!res) |
224 |
res_sig = gpgme_op_sign_result (c->gpg); |
res_sig = gpgme_op_sign_result (c->gpg); |
225 |
if (reg_prefs.cache_time > 0 && !c->is_card && |
if (reg_prefs.cache_time > 0 && !c->is_card && |
226 |
((res && res->recipients) || (res_sig && res_sig->signatures))) { |
(res || res_sig)) { |
227 |
if (agent_get_cache (c->keyid, &item)) |
if (agent_get_cache (c->keyid, &item)) |
228 |
agent_unlock_cache_entry (&item); |
agent_unlock_cache_entry (&item); |
229 |
else |
else |
230 |
agent_put_cache (c->keyid, c->pwd, reg_prefs.cache_time); |
agent_put_cache (c->keyid, c->pwd, reg_prefs.cache_time); |
231 |
} |
} |
232 |
c->cancel = 0; |
c->cancel = 0; |
254 |
static char keyid[16+1]; |
static char keyid[16+1]; |
255 |
|
|
256 |
/* XXX: check for leading alpha-chars? */ |
/* XXX: check for leading alpha-chars? */ |
257 |
if (strlen (pass_info) < 16) |
if (strlen (pass_info) < 16) { |
258 |
|
log_debug ("parse_gpg_keyid: error '%s'\r\n", pass_info); |
259 |
return NULL; |
return NULL; |
260 |
|
} |
261 |
/* the format of the desc buffer looks like this: |
/* the format of the desc buffer looks like this: |
262 |
request_keyid[16] main_keyid[16] keytype[1] keylength[4] |
request_keyid[16] main_keyid[16] keytype[1] keylength[4] |
263 |
we use the main keyid to use only one cache entry. */ |
we use the main keyid to use only one cache entry. */ |
279 |
char *uid, *p; |
char *uid, *p; |
280 |
int n=0; |
int n=0; |
281 |
|
|
282 |
|
algo = (gpgme_pubkey_algo_t)0; |
283 |
/* Each uid_hint contains a long key-ID so it is at least 16 bytes. */ |
/* Each uid_hint contains a long key-ID so it is at least 16 bytes. */ |
284 |
if (strlen (uid_hint) < 17) { |
if (strlen (uid_hint) < 17) { |
285 |
*desc = 0; |
*desc = 0; |
286 |
|
log_debug ("parse_gpg_description: error '%s'\r\n", uid_hint); |
287 |
return -1; |
return -1; |
288 |
} |
} |
289 |
|
|
297 |
uid_hint += 16; /* skip keyid */ |
uid_hint += 16; /* skip keyid */ |
298 |
uid_hint += 1; /* space */ |
uid_hint += 1; /* space */ |
299 |
|
|
300 |
uid = utf8_to_wincp (uid_hint, strlen (uid_hint)); |
uid = utf8_to_native (uid_hint); |
301 |
|
|
302 |
if (strcmp (usedkey, mainkey)) |
if (strcmp (usedkey, mainkey)) |
303 |
_snprintf (desc, size-1, |
_snprintf (desc, size-1, |
324 |
char *p; |
char *p; |
325 |
|
|
326 |
p = strchr (id, '/'); |
p = strchr (id, '/'); |
327 |
if (!p) |
if (!p) { |
328 |
|
log_debug ("extract_serial_no: error '%s'\r\n", id); |
329 |
return NULL; |
return NULL; |
330 |
|
} |
331 |
strncpy (buf, id+(p-id)-6, 6); |
strncpy (buf, id+(p-id)-6, 6); |
332 |
return buf; |
return buf; |
333 |
} |
} |
342 |
passphrase_cb_s *c = (passphrase_cb_s*)hook; |
passphrase_cb_s *c = (passphrase_cb_s*)hook; |
343 |
HANDLE hd = (HANDLE)fd; |
HANDLE hd = (HANDLE)fd; |
344 |
void *item; |
void *item; |
345 |
const char *keyid, *pass; |
const char *keyid=NULL, *pass; |
346 |
DWORD n; |
DWORD n; |
347 |
int rc; |
int rc = 0; |
348 |
|
|
349 |
if (!c) |
if (!c) { |
350 |
|
log_debug ("passphrase_cb: error '!c'\r\n"); |
351 |
return gpg_error (GPG_ERR_INV_ARG); |
return gpg_error (GPG_ERR_INV_ARG); |
352 |
|
} |
353 |
c->bad_pwd = prev_was_bad? 1 : 0; |
c->bad_pwd = prev_was_bad? 1 : 0; |
354 |
if (prev_was_bad && !c->cancel) { |
if (prev_was_bad && !c->cancel) { |
355 |
if (c->pwd) |
if (c->pwd) |
362 |
if (strlen (passphrase_info) < 16 && |
if (strlen (passphrase_info) < 16 && |
363 |
!strstr (passphrase_info, "OPENPGP")) { |
!strstr (passphrase_info, "OPENPGP")) { |
364 |
/* assume symetric encryption. */ |
/* assume symetric encryption. */ |
365 |
int n=2; |
int pos=2; |
366 |
c->sym.sym_algo = atoi (passphrase_info); |
c->sym.sym_algo = atoi (passphrase_info); |
367 |
if (c->sym.sym_algo > 9) |
if (c->sym.sym_algo > 9) |
368 |
n++; |
pos++; |
369 |
/* XXX: be more strict. */ |
/* XXX: be more strict. */ |
370 |
c->sym.s2k_mode = atoi (passphrase_info+n); |
c->sym.s2k_mode = atoi (passphrase_info+pos); |
371 |
c->sym.s2k_hash = atoi (passphrase_info+n+2); |
c->sym.s2k_hash = atoi (passphrase_info+pos+2); |
372 |
} |
} |
373 |
|
|
374 |
keyid = parse_gpg_keyid (passphrase_info); |
keyid = parse_gpg_keyid (passphrase_info); |
375 |
pass = agent_get_cache (keyid+8, &item); |
pass = agent_get_cache (keyid+8, &item); |
376 |
if (pass) { |
if (pass) { |
377 |
agent_unlock_cache_entry (&item); |
agent_unlock_cache_entry (&item); |
378 |
c->pwd_init = 0; |
c->pwd_init = 0; |
401 |
} |
} |
402 |
else if (uid_hint) |
else if (uid_hint) |
403 |
parse_gpg_description (uid_hint, passphrase_info, |
parse_gpg_description (uid_hint, passphrase_info, |
404 |
c->info, sizeof c->info - 1); |
c->info, sizeof (c->info) - 1); |
405 |
if (c->gpg_cmd == GPG_CMD_DECRYPT) { |
if (c->gpg_cmd == GPG_CMD_DECRYPT) { |
406 |
rc = DialogBoxParam (glob_hinst, (LPCSTR)IDD_WINPT_DECRYPT, |
rc = DialogBoxParam (glob_hinst, (LPCSTR)IDD_WINPT_DECRYPT, |
407 |
(HWND)c->hwnd, passphrase_callback_proc, |
(HWND)c->hwnd, passphrase_callback_proc, |
425 |
return 0; |
return 0; |
426 |
} |
} |
427 |
|
|
428 |
WriteFile (hd, c->pwd, strlen (c->pwd), &n, NULL); |
if (!WriteFile (hd, c->pwd, strlen (c->pwd), &n, NULL)) |
429 |
WriteFile (hd, "\n", 1, &n, NULL); |
log_debug ("passphrase_cb: WriteFile() failed ec=%d\n", w32_errno); |
430 |
|
if (!WriteFile (hd, "\n", 1, &n, NULL)) |
431 |
|
log_debug ("passphrase_cb: WriteFile() failed ec=%d\n", w32_errno); |
432 |
return 0; |
return 0; |
433 |
} |
} |
434 |
|
|