1 |
/* wptPassphraseCB.cpp - GPGME Passphrase Callback |
/* wptPassphraseCB.cpp - GPGME Passphrase Callback |
2 |
* Copyright (C) 2001, 2002, 2003, 2005 Timo Schulz |
* Copyright (C) 2001, 2002-2006 Timo Schulz |
3 |
* Copyright (C) 2005 g10 Code GmbH |
* Copyright (C) 2005 g10 Code GmbH |
4 |
* |
* |
5 |
* This file is part of WinPT. |
* This file is part of WinPT. |
34 |
#include "wptCommonCtl.h" |
#include "wptCommonCtl.h" |
35 |
#include "wptContext.h" |
#include "wptContext.h" |
36 |
#include "wptDlgs.h" |
#include "wptDlgs.h" |
|
#include "wptUTF8.h" |
|
37 |
#include "wptErrors.h" |
#include "wptErrors.h" |
38 |
#include "wptTypes.h" |
#include "wptTypes.h" |
39 |
#include "wptKeylist.h" |
#include "wptKeylist.h" |
40 |
#include "wptAgent.h" |
#include "wptAgent.h" |
41 |
#include "wptRegistry.h" |
#include "wptRegistry.h" |
42 |
|
#include "wptUTF8.h" |
43 |
|
|
|
const char* get_symkey_algo (int algo); |
|
44 |
|
|
45 |
#define item_ctrl_id( cmd ) \ |
/* Return the control ID dependent on the mode (sign or decrypt). */ |
46 |
|
#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) \ |
50 |
((cmd) == GPG_CMD_DECRYPT? IDC_DECRYPT_HIDE : IDC_DECRYPT_SIGN_HIDE) |
((cmd) == GPG_CMD_DECRYPT? IDC_DECRYPT_HIDE : IDC_DECRYPT_SIGN_HIDE) |
51 |
|
|
52 |
|
const char* get_symkey_algo (int algo); |
53 |
|
void ListBox_AddString_utf8 (HWND lb, const char *txt); |
54 |
|
|
55 |
|
|
56 |
/* Overwrite passphrase and free memory. */ |
/* Overwrite passphrase and free memory. */ |
57 |
static void |
static void |
71 |
static passphrase_cb_s *c; |
static passphrase_cb_s *c; |
72 |
gpgme_decrypt_result_t res=NULL; |
gpgme_decrypt_result_t res=NULL; |
73 |
gpgme_sign_result_t res_sig=NULL; |
gpgme_sign_result_t res_sig=NULL; |
|
gpgme_key_t key; |
|
74 |
gpgme_recipient_t recip=NULL, r; |
gpgme_recipient_t recip=NULL, r; |
75 |
|
winpt_key_s key; |
76 |
void *item; |
void *item; |
77 |
const char *id; |
const char *id; |
78 |
char *info; |
char *info; |
79 |
int n; |
int n; |
80 |
|
|
81 |
switch (msg) { |
switch (msg) { |
82 |
|
case WM_ACTIVATE: |
83 |
|
safe_edit_control_init (dlg, item_ctrl_id (c->gpg_cmd)); |
84 |
|
break; |
85 |
|
|
86 |
|
case WM_DESTROY: |
87 |
|
safe_edit_control_free (dlg, item_ctrl_id (c->gpg_cmd)); |
88 |
|
break; |
89 |
|
|
90 |
case WM_INITDIALOG: |
case WM_INITDIALOG: |
91 |
c = (passphrase_cb_s *)lparam; |
c = (passphrase_cb_s *)lparam; |
92 |
if (!c) |
if (!c) |
93 |
BUG (0); |
BUG (0); |
94 |
SetDlgItemText (dlg, IDC_DECRYPT_HIDE, _("&Hide Typing")); |
SetDlgItemText (dlg, IDCANCEL, _("&Cancel")); |
95 |
SetWindowText (dlg, c->title); |
SetWindowText (dlg, c->title); |
96 |
if (c->gpg_cmd == GPG_CMD_DECRYPT) { |
if (c->gpg_cmd == GPG_CMD_DECRYPT) { |
97 |
|
SetDlgItemText (dlg, IDC_DECRYPT_HIDE, _("&Hide Typing")); |
98 |
SetDlgItemText (dlg, IDC_DECRYPT_LISTINF, |
SetDlgItemText (dlg, IDC_DECRYPT_LISTINF, |
99 |
_("Encrypted with the following public key(s)")); |
_("Encrypted with the following public key(s)")); |
100 |
CheckDlgButton (dlg, IDC_DECRYPT_HIDE, BST_CHECKED); |
CheckDlgButton (dlg, IDC_DECRYPT_HIDE, BST_CHECKED); |
101 |
} |
} |
102 |
else if (c->gpg_cmd == GPG_CMD_SIGN) |
else if (c->gpg_cmd == GPG_CMD_SIGN) { |
103 |
|
SetDlgItemText (dlg, IDC_DECRYPT_SIGN_HIDE, _("&Hide Typing")); |
104 |
CheckDlgButton (dlg, IDC_DECRYPT_SIGN_HIDE, BST_CHECKED); |
CheckDlgButton (dlg, IDC_DECRYPT_SIGN_HIDE, BST_CHECKED); |
105 |
|
} |
106 |
|
/* Because it depends on the order the keys are stored in the |
107 |
|
keyring whether res->recipients is complete or not, we also |
108 |
|
support that the recipients were externally extracted and then |
109 |
|
we use this list. */ |
110 |
if (c->recipients) |
if (c->recipients) |
111 |
recip = c->recipients; /* recipients were already extracted. */ |
recip = c->recipients; /* recipients were already extracted. */ |
112 |
else { |
else { |
|
/* XXX: not all ENCRYPT_TO entries are listed here. */ |
|
113 |
res = gpgme_op_decrypt_result (c->gpg); |
res = gpgme_op_decrypt_result (c->gpg); |
114 |
if (res && res->recipients) |
if (res && res->recipients) |
115 |
recip = res->recipients; |
recip = res->recipients; |
116 |
} |
} |
117 |
if (recip != NULL && c->gpg_cmd == GPG_CMD_DECRYPT) { |
if (recip != NULL && c->gpg_cmd == GPG_CMD_DECRYPT) { |
118 |
for (r = res->recipients; r; r = r->next) { |
for (r = recip; r; r = r->next) { |
119 |
get_pubkey (r->keyid, &key); |
memset (&key, 0, sizeof (key)); |
120 |
if (key) { |
if (!winpt_get_pubkey (r->keyid, &key)) { |
121 |
char *uid; |
gpgme_user_id_t u = key.ctx->uids; |
122 |
id = key->uids->name; |
|
123 |
|
id = u->name; |
124 |
if (!id) |
if (!id) |
125 |
id = _("Invalid User ID"); |
id = _("Invalid User ID"); |
126 |
uid = utf8_to_wincp (id, strlen (id)); |
n = 32+strlen (id)+1+4+strlen (r->keyid)+1; |
127 |
info = new char [32+strlen (uid)+1 + 4 + strlen (r->keyid)+1 |
if (u->email) |
128 |
+ strlen (key->uids->email)+1]; |
n += strlen (u->email)+1; |
129 |
|
info = new char [n+1]; |
130 |
if (!info) |
if (!info) |
131 |
BUG (NULL); |
BUG (NULL); |
132 |
sprintf (info, "%s <%s> (%s, 0x%s)", uid, key->uids->email, |
if (!u->email || strlen (u->email) < 1) |
133 |
get_key_pubalgo (r->pubkey_algo), r->keyid+8); |
sprintf (info, "%s (%s, 0x%s)", id, |
134 |
free (uid); |
get_key_pubalgo (r->pubkey_algo), r->keyid+8); |
135 |
|
else |
136 |
|
sprintf (info, "%s <%s> (%s, 0x%s)", id, u->email, |
137 |
|
get_key_pubalgo (r->pubkey_algo), r->keyid+8); |
138 |
} |
} |
139 |
else { |
else { |
140 |
info = new char [32 + strlen (r->keyid)+1 + 4]; |
info = new char [32 + strlen (r->keyid)+1 + 4]; |
143 |
sprintf (info, _("Unknown key ID (%s, 0x%s)"), |
sprintf (info, _("Unknown key ID (%s, 0x%s)"), |
144 |
get_key_pubalgo (r->pubkey_algo), r->keyid+8); |
get_key_pubalgo (r->pubkey_algo), r->keyid+8); |
145 |
} |
} |
146 |
listbox_add_string (GetDlgItem (dlg, IDC_DECRYPT_LIST), info); |
ListBox_AddString_utf8 (GetDlgItem (dlg, IDC_DECRYPT_LIST), info); |
147 |
free_if_alloc (info); |
free_if_alloc (info); |
148 |
|
winpt_release_pubkey (&key); |
149 |
} |
} |
150 |
} |
} |
151 |
else if (c->gpg_cmd == GPG_CMD_DECRYPT) |
else if (c->gpg_cmd == GPG_CMD_DECRYPT) |
171 |
SetDlgItemText (dlg, IDC_DECRYPT_MSG, c->info); |
SetDlgItemText (dlg, IDC_DECRYPT_MSG, c->info); |
172 |
} |
} |
173 |
else { |
else { |
174 |
SetFocus( GetDlgItem (dlg, IDC_DECRYPT_SIGN_PWD)); |
SetFocus (GetDlgItem (dlg, IDC_DECRYPT_SIGN_PWD)); |
175 |
SetDlgItemText (dlg, IDC_DECRYPT_SIGN_MSG, c->info); |
SetDlgItemText (dlg, IDC_DECRYPT_SIGN_MSG, c->info); |
176 |
} |
} |
177 |
center_window (dlg, NULL); |
center_window (dlg, NULL); |
178 |
SetForegroundWindow (dlg); |
SetForegroundWindow (dlg); |
|
set_active_window (dlg); |
|
179 |
return FALSE; |
return FALSE; |
180 |
|
|
181 |
case WM_SYSCOMMAND: |
case WM_SYSCOMMAND: |
182 |
if (LOWORD (wparam) == SC_CLOSE) { |
if (LOWORD (wparam) == SC_CLOSE) { |
|
SetDlgItemText (dlg, item_ctrl_id (c->gpg_cmd), ""); |
|
183 |
c->cancel = 1; |
c->cancel = 1; |
184 |
EndDialog (dlg, TRUE); |
EndDialog (dlg, TRUE); |
185 |
} |
} |
213 |
strcpy (c->pwd, ""); |
strcpy (c->pwd, ""); |
214 |
} |
} |
215 |
else { |
else { |
216 |
c->pwd = new char[n+2]; |
char *p = new char[n+2]; |
217 |
if (!c->pwd) |
if (!p) |
218 |
BUG (NULL); |
BUG (NULL); |
219 |
GetDlgItemText (dlg, item_ctrl_id (c->gpg_cmd), c->pwd, n+1); |
/* Get the passphrase and convert it utf8. |
220 |
|
This does not just the us-ascii charset. */ |
221 |
|
SafeGetDlgItemText (dlg, item_ctrl_id (c->gpg_cmd), p, n+1); |
222 |
|
c->pwd = native_to_utf8 (p); |
223 |
|
sfree_if_alloc (p); |
224 |
} |
} |
225 |
res = gpgme_op_decrypt_result (c->gpg); |
res = gpgme_op_decrypt_result (c->gpg); |
226 |
if (!res) |
if (!res) |
227 |
res_sig = gpgme_op_sign_result (c->gpg); |
res_sig = gpgme_op_sign_result (c->gpg); |
228 |
if (reg_prefs.cache_time > 0 && !c->is_card && |
if (!c->is_card && reg_prefs.cache_time > 0 && |
229 |
(res || res_sig)) { |
(res || res_sig)) { |
230 |
if (agent_get_cache (c->keyid, &item)) |
if (agent_get_cache (c->keyid, &item)) |
231 |
agent_unlock_cache_entry (&item); |
agent_unlock_cache_entry (&item); |
232 |
else |
else |
233 |
agent_put_cache (c->keyid, c->pwd, reg_prefs.cache_time); |
agent_put_cache (c->keyid, c->pwd, reg_prefs.cache_time); |
279 |
gpgme_pubkey_algo_t algo; |
gpgme_pubkey_algo_t algo; |
280 |
char usedkey[16+1]; |
char usedkey[16+1]; |
281 |
char mainkey[16+1]; |
char mainkey[16+1]; |
282 |
char *uid, *p; |
char *p, *uid; |
283 |
int n=0; |
int n=0; |
284 |
|
|
285 |
algo = (gpgme_pubkey_algo_t)0; |
algo = (gpgme_pubkey_algo_t)0; |
300 |
uid_hint += 16; /* skip keyid */ |
uid_hint += 16; /* skip keyid */ |
301 |
uid_hint += 1; /* space */ |
uid_hint += 1; /* space */ |
302 |
|
|
303 |
uid = utf8_to_wincp (uid_hint, strlen (uid_hint)); |
uid = utf8_to_native (uid_hint); |
|
|
|
304 |
if (strcmp (usedkey, mainkey)) |
if (strcmp (usedkey, mainkey)) |
305 |
_snprintf (desc, size-1, |
_snprintf (desc, size-1, |
306 |
_("You need a passphrase to unlock the secret key for\n" |
_("You need a passphrase to unlock the secret key for user:\n" |
307 |
"user: \"%s\"\n" |
"\"%s\"\n" |
308 |
"%s key, ID %s (main key ID %s)\n"), |
"%s key, ID 0x%s (main key ID 0x%s)\n"), |
309 |
uid, get_key_pubalgo (algo), usedkey+8, mainkey+8); |
uid, get_key_pubalgo (algo), usedkey+8, mainkey+8); |
310 |
else if (!strcmp (usedkey, mainkey)) |
else if (!strcmp (usedkey, mainkey)) |
311 |
_snprintf (desc, size-1, |
_snprintf (desc, size-1, |
312 |
_("You need a passphrase to unlock the secret key for\n" |
_("You need a passphrase to unlock the secret key for user:\n" |
313 |
"user: \"%s\"\n" |
"\"%s\"\n" |
314 |
"%s key, ID %s\n"), |
"%s key, ID 0x%s\n"), |
315 |
uid, get_key_pubalgo (algo), usedkey+8); |
uid, get_key_pubalgo (algo), usedkey+8); |
316 |
free (uid); |
safe_free (uid); |
317 |
return 0; |
return 0; |
318 |
} |
} |
319 |
|
|
403 |
} |
} |
404 |
else if (uid_hint) |
else if (uid_hint) |
405 |
parse_gpg_description (uid_hint, passphrase_info, |
parse_gpg_description (uid_hint, passphrase_info, |
406 |
c->info, sizeof c->info - 1); |
c->info, sizeof (c->info) - 1); |
407 |
if (c->gpg_cmd == GPG_CMD_DECRYPT) { |
if (c->gpg_cmd == GPG_CMD_DECRYPT) { |
408 |
rc = DialogBoxParam (glob_hinst, (LPCSTR)IDD_WINPT_DECRYPT, |
rc = DialogBoxParam (glob_hinst, (LPCSTR)IDD_WINPT_DECRYPT, |
409 |
(HWND)c->hwnd, passphrase_callback_proc, |
(HWND)c->hwnd, passphrase_callback_proc, |
421 |
} |
} |
422 |
c->pwd_init = 0; |
c->pwd_init = 0; |
423 |
} |
} |
424 |
if (c->cancel) { |
if (c->cancel || !c->pwd) { |
425 |
if (!WriteFile (hd, "\n", 1, &n, NULL)) |
if (!WriteFile (hd, "\n", 1, &n, NULL)) |
426 |
log_debug ("passphrase_cb: WriteFile() failed ec=%d\n", w32_errno); |
log_debug ("passphrase_cb: WriteFile() failed ec=%d\n", w32_errno); |
427 |
return 0; |
return 0; |
451 |
cb->pwd_init = 1; |
cb->pwd_init = 1; |
452 |
free_if_alloc (cb->title); |
free_if_alloc (cb->title); |
453 |
cb->title = m_strdup (title); |
cb->title = m_strdup (title); |
|
if (!cb->title) |
|
|
BUG (NULL); |
|
454 |
gpgme_set_passphrase_cb (ctx, passphrase_cb, cb); |
gpgme_set_passphrase_cb (ctx, passphrase_cb, cb); |
455 |
cb->gpg = ctx; |
cb->gpg = ctx; |
456 |
} |
} |
457 |
|
|
458 |
|
|
459 |
/* Release a passphrase callback @ctx. */ |
/* Release the gpg recipient list. */ |
460 |
void |
void |
461 |
release_gpg_passphrase_cb (passphrase_cb_s *ctx) |
release_gpg_recipients (gpgme_recipient_t *recipients) |
462 |
{ |
{ |
463 |
gpgme_recipient_t r, n; |
gpgme_recipient_t r, n; |
464 |
|
|
465 |
if (!ctx) |
r = *recipients; |
466 |
return; |
while (r != NULL) { |
|
sfree_if_alloc (ctx->pwd); |
|
|
free_if_alloc (ctx->title); |
|
|
r = ctx->recipients; |
|
|
while (r) { |
|
467 |
n = r->next; |
n = r->next; |
468 |
safe_free (r->keyid); |
safe_free (r->keyid); |
469 |
safe_free (r); |
safe_free (r); |
470 |
r = n; |
r = n; |
471 |
} |
} |
472 |
|
*recipients = NULL; |
473 |
|
} |
474 |
|
|
475 |
|
|
476 |
|
|
477 |
|
/* Release a passphrase callback @ctx. */ |
478 |
|
void |
479 |
|
release_gpg_passphrase_cb (passphrase_cb_s *ctx) |
480 |
|
{ |
481 |
|
if (!ctx) |
482 |
|
return; |
483 |
|
sfree_if_alloc (ctx->pwd); |
484 |
|
free_if_alloc (ctx->title); |
485 |
|
release_gpg_recipients (&ctx->recipients); |
486 |
} |
} |
487 |
|
|
488 |
|
|