1 |
/* wptPassphraseCB.cpp - GPGME Passphrase Callback |
/* wptPassphraseCB.cpp - GPGME Passphrase Callback |
2 |
* Copyright (C) 2001, 2002, 2003, 2005 Timo Schulz |
* Copyright (C) 2001, 2002, 2003, 2005 Timo Schulz |
3 |
|
* Copyright (C) 2005 g10 Code GmbH |
4 |
* |
* |
5 |
* This file is part of WinPT. |
* This file is part of WinPT. |
6 |
* |
* |
21 |
|
|
22 |
#include <windows.h> |
#include <windows.h> |
23 |
#include <ctype.h> |
#include <ctype.h> |
|
#include <io.h> |
|
24 |
|
|
25 |
#include "../resource.h" |
#include "../resource.h" |
26 |
#include "wptNLS.h" |
#include "wptNLS.h" |
50 |
passphrase_callback_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
passphrase_callback_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
51 |
{ |
{ |
52 |
static passphrase_cb_s * c; |
static passphrase_cb_s * c; |
53 |
|
gpgme_decrypt_result_t res; |
54 |
|
gpgme_sign_result_t res_sig; |
55 |
gpgme_key_t key; |
gpgme_key_t key; |
56 |
const char *id; |
const char *id; |
57 |
char *info; |
char *info; |
58 |
void *ctx = NULL, *item; |
void *ctx = NULL, *item; |
59 |
int n; |
int n; |
60 |
|
|
61 |
switch( msg ) { |
switch (msg) { |
62 |
case WM_INITDIALOG: |
case WM_INITDIALOG: |
63 |
c = (passphrase_cb_s *)lparam; |
c = (passphrase_cb_s *)lparam; |
64 |
if (!c) |
if (!c) |
71 |
} |
} |
72 |
else if( c->gpg_cmd == GPG_CMD_SIGN ) |
else if( c->gpg_cmd == GPG_CMD_SIGN ) |
73 |
CheckDlgButton (dlg, IDC_DECRYPT_SIGN_HIDE, BST_CHECKED); |
CheckDlgButton (dlg, IDC_DECRYPT_SIGN_HIDE, BST_CHECKED); |
74 |
if (gpgme_op_decrypt_result (c->gpg) != NULL && |
res = gpgme_op_decrypt_result (c->gpg); |
75 |
c->gpg_cmd == GPG_CMD_DECRYPT) { |
if (res != NULL && c->gpg_cmd == GPG_CMD_DECRYPT) { |
76 |
gpgme_recipient_t r; |
gpgme_recipient_t r; |
|
gpgme_decrypt_result_t res; |
|
77 |
|
|
|
res = gpgme_op_decrypt_result (c->gpg); |
|
78 |
for (r = res->recipients; r; r = r->next) { |
for (r = res->recipients; r; r = r->next) { |
79 |
get_pubkey (r->keyid, &key); |
get_pubkey (r->keyid, &key); |
80 |
if (key) { |
if (key) { |
81 |
char *uid; |
char *uid; |
82 |
id = key->uids->uid; |
id = key->uids->name; |
83 |
if (!id) |
if (!id) |
84 |
id = _("Invalid User ID"); |
id = _("Invalid User ID"); |
85 |
uid = utf8_to_wincp (id, strlen (id)); |
uid = utf8_to_wincp (id, strlen (id)); |
86 |
info = new char [8+strlen (uid) + 4 + strlen (r->keyid) + 2]; |
info = new char [16+strlen (uid) + 4 + strlen (r->keyid) + strlen (key->uids->email) + 3]; |
87 |
if (!info) |
if (!info) |
88 |
BUG (NULL); |
BUG (NULL); |
89 |
sprintf (info, "%s (%s, 0x%s)", uid, |
sprintf (info, "%s <%s> (%s, 0x%s)", uid, key->uids->email, |
90 |
get_key_pubalgo (r->pubkey_algo), r->keyid+8); |
get_key_pubalgo (r->pubkey_algo), r->keyid+8); |
91 |
free (uid); |
free (uid); |
92 |
free (info); |
|
93 |
} |
} |
94 |
else { |
else { |
95 |
info = new char [32 + strlen (r->keyid) + 2]; |
info = new char [32 + strlen (r->keyid) + 2]; |
97 |
BUG (NULL); |
BUG (NULL); |
98 |
sprintf (info, _("Unknown (key ID 0x%s)"), |
sprintf (info, _("Unknown (key ID 0x%s)"), |
99 |
r->keyid? r->keyid+8 : "????????"); |
r->keyid? r->keyid+8 : "????????"); |
|
free (info); |
|
100 |
} |
} |
101 |
listbox_add_string (GetDlgItem( dlg, IDC_DECRYPT_LIST ), info); |
listbox_add_string (GetDlgItem (dlg, IDC_DECRYPT_LIST), info); |
102 |
|
free (info); |
103 |
} |
} |
104 |
} |
} |
105 |
|
else if (c->gpg_cmd == GPG_CMD_DECRYPT) |
106 |
|
EnableWindow (GetDlgItem (dlg, IDC_DECRYPT_LIST), FALSE); |
107 |
SetDlgItemText( dlg, c->gpg_cmd == GPG_CMD_DECRYPT? |
SetDlgItemText( dlg, c->gpg_cmd == GPG_CMD_DECRYPT? |
108 |
IDC_DECRYPT_PWDINFO : IDC_DECRYPT_SIGN_PWDINFO, |
IDC_DECRYPT_PWDINFO : IDC_DECRYPT_SIGN_PWDINFO, |
109 |
_("Please enter your passphrase") ); |
_("Please enter your passphrase") ); |
110 |
if( c->gpg_cmd == GPG_CMD_DECRYPT ) { |
if (c->gpg_cmd == GPG_CMD_DECRYPT) { |
111 |
SetFocus( GetDlgItem( dlg, IDC_DECRYPT_PWD ) ); |
SetFocus (GetDlgItem (dlg, IDC_DECRYPT_PWD)); |
112 |
SetDlgItemText( dlg, IDC_DECRYPT_MSG, c->info ); |
if (res && !res->recipients) |
113 |
|
SetDlgItemText (dlg, IDC_DECRYPT_MSG, _("Symmetric encryption.\n")); |
114 |
|
else |
115 |
|
SetDlgItemText (dlg, IDC_DECRYPT_MSG, c->info); |
116 |
} |
} |
117 |
else { |
else { |
118 |
SetFocus( GetDlgItem( dlg, IDC_DECRYPT_SIGN_PWD ) ); |
SetFocus( GetDlgItem (dlg, IDC_DECRYPT_SIGN_PWD)); |
119 |
SetDlgItemText( dlg, IDC_DECRYPT_SIGN_MSG, c->info ); |
SetDlgItemText (dlg, IDC_DECRYPT_SIGN_MSG, c->info); |
120 |
} |
} |
121 |
center_window( dlg, NULL ); |
center_window (dlg, NULL); |
122 |
SetForegroundWindow( dlg ); |
SetForegroundWindow (dlg); |
123 |
set_active_window( dlg ); |
set_active_window (dlg); |
124 |
return FALSE; |
return FALSE; |
125 |
|
|
126 |
case WM_SYSCOMMAND: |
case WM_SYSCOMMAND: |
129 |
c->cancel = 1; |
c->cancel = 1; |
130 |
EndDialog( dlg, TRUE ); |
EndDialog( dlg, TRUE ); |
131 |
} |
} |
132 |
return FALSE; |
break; |
133 |
|
|
134 |
case WM_COMMAND: |
case WM_COMMAND: |
135 |
switch( HIWORD( wparam ) ) { |
switch( HIWORD( wparam ) ) { |
159 |
strcpy (c->pwd, ""); |
strcpy (c->pwd, ""); |
160 |
} |
} |
161 |
else { |
else { |
162 |
c->pwd = new char[n+1]; |
c->pwd = new char[n+2]; |
163 |
if (!c->pwd) |
if (!c->pwd) |
164 |
BUG (NULL); |
BUG (NULL); |
165 |
GetDlgItemText (dlg, item_ctrl_id (c->gpg_cmd), c->pwd, sizeof (c->pwd) -1); |
GetDlgItemText (dlg, item_ctrl_id (c->gpg_cmd), c->pwd, n+1); |
166 |
} |
} |
167 |
if (reg_prefs.cache_time > 0 && !c->is_card && !strstr (c->keyid, "missing")) { |
res = gpgme_op_decrypt_result (c->gpg); |
168 |
|
if (!res) |
169 |
|
res_sig = gpgme_op_sign_result (c->gpg); |
170 |
|
if (reg_prefs.cache_time > 0 && !c->is_card && |
171 |
|
((res && res->recipients) || (res_sig && res_sig->signatures))) { |
172 |
if (agent_get_cache (c->keyid, &item)) |
if (agent_get_cache (c->keyid, &item)) |
173 |
agent_unlock_cache_entry (&item); |
agent_unlock_cache_entry (&item); |
174 |
else |
else |
267 |
int prev_was_bad, int fd) |
int prev_was_bad, int fd) |
268 |
{ |
{ |
269 |
passphrase_cb_s *c = (passphrase_cb_s*)hook; |
passphrase_cb_s *c = (passphrase_cb_s*)hook; |
270 |
|
HANDLE hd = (HANDLE)fd; |
271 |
void *item; |
void *item; |
272 |
const char *keyid, *pass; |
const char *keyid, *pass; |
273 |
|
DWORD n; |
274 |
int rc; |
int rc; |
275 |
|
|
276 |
|
/* XXX: pubkey_enc cancel does not quit gpg.exe */ |
277 |
|
/* XXX: handle prev_was_bad case. */ |
278 |
if (!c) |
if (!c) |
279 |
return gpg_error (GPG_ERR_INV_ARG); |
return gpg_error (GPG_ERR_INV_ARG); |
280 |
|
|
281 |
if (passphrase_info) { |
if (passphrase_info) { |
282 |
keyid = parse_gpg_keyid (passphrase_info); |
keyid = parse_gpg_keyid (passphrase_info); |
283 |
pass = agent_get_cache (keyid, &item); |
pass = agent_get_cache (keyid+8, &item); |
284 |
if (pass) { |
if (pass) { |
285 |
agent_unlock_cache_entry (&item); |
agent_unlock_cache_entry (&item); |
286 |
c->pwd_init = 0; |
c->pwd_init = 0; |
287 |
write (fd, pass, strlen (pass)); |
if (!WriteFile (hd, pass, strlen (pass), &n, NULL)) |
288 |
write (fd, "\n", 1); |
log_debug ("passphrase_cb: WriteFile() failed ec=%d\n", w32_errno); |
289 |
|
if (!WriteFile (hd, "\n", 1, &n, NULL)) |
290 |
|
log_debug ("passphrase_cb: WriteFile() failed ec=%d\n", w32_errno); |
291 |
return 0; |
return 0; |
292 |
} |
} |
293 |
} |
} |
321 |
(LPARAM)c); |
(LPARAM)c); |
322 |
} |
} |
323 |
if (rc == -1) { |
if (rc == -1) { |
324 |
write (fd, "\n", 1); |
WriteFile (hd, "\n", 1, &n, NULL); |
325 |
return gpg_error (GPG_ERR_EOF); |
return gpg_error (GPG_ERR_EOF); |
326 |
} |
} |
327 |
c->pwd_init = 0; |
c->pwd_init = 0; |
328 |
} |
} |
329 |
if (c->cancel) { |
if (c->cancel) { |
330 |
write (fd, "\n", 1); |
WriteFile (hd, "\n", 1, &n, NULL); |
331 |
return gpg_error (GPG_ERR_EOF); |
return gpg_error (GPG_ERR_EOF); |
332 |
} |
} |
333 |
|
|
334 |
write (fd, c->pwd, strlen (c->pwd)); |
WriteFile (hd, c->pwd, strlen (c->pwd), &n, NULL); |
335 |
write (fd, "\n", 1); |
WriteFile (hd, "\n", 1, &n, NULL); |
336 |
return 0; |
return 0; |
337 |
} |
} |
338 |
|
|