1 |
twoaday |
1 |
/* OEPassphraseDLG.cpp - GPGME Passphrase Callback |
2 |
|
|
* Copyright (C) 2001, 2002, 2006 Timo Schulz |
3 |
|
|
* |
4 |
|
|
* This file is part of GPGOE. |
5 |
|
|
* |
6 |
|
|
* GPGOE is free software; you can redistribute it and/or modify |
7 |
|
|
* it under the terms of the GNU Lesser General Public License as published by |
8 |
|
|
* the Free Software Foundation; either version 2.1 of the License, or |
9 |
|
|
* (at your option) any later version. |
10 |
|
|
* |
11 |
|
|
* GPGOE is distributed in the hope that it will be useful, |
12 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
|
|
* GNU General Public License for more details. |
15 |
|
|
* |
16 |
|
|
* You should have received a copy of the GNU Lesser General Public License |
17 |
|
|
* along with GPGOE; if not, write to the Free Software Foundation, |
18 |
|
|
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA7 USA |
19 |
|
|
*/ |
20 |
|
|
|
21 |
|
|
#ifdef HAVE_CONFIG_H |
22 |
|
|
#include <config.h> |
23 |
|
|
#endif |
24 |
|
|
#include <windows.h> |
25 |
|
|
#include <assert.h> |
26 |
|
|
#include "resource.h" |
27 |
|
|
#include "gpgme.h" |
28 |
|
|
#include "GPGOE.h" |
29 |
|
|
|
30 |
twoaday |
11 |
|
31 |
twoaday |
1 |
/* Structure for the passphrase callback. */ |
32 |
|
|
struct pass_cb_s { |
33 |
|
|
const char *uid_hint; |
34 |
|
|
const char *passphrase_info; |
35 |
twoaday |
16 |
char keyid[8+2]; |
36 |
twoaday |
1 |
char *pass; |
37 |
twoaday |
16 |
HWND main_wnd; |
38 |
twoaday |
1 |
int cancel; |
39 |
|
|
int prev_was_bad; |
40 |
|
|
}; |
41 |
|
|
|
42 |
|
|
|
43 |
twoaday |
19 |
extern char gpgoe_pass_cache[HASH_BUCKETS][256]; |
44 |
twoaday |
16 |
|
45 |
|
|
|
46 |
twoaday |
19 |
DWORD hash_string (const char *str_param); |
47 |
twoaday |
16 |
|
48 |
twoaday |
19 |
/* Store the passphrase @pass in the hash table using the keyid @keyid |
49 |
|
|
as the index. */ |
50 |
twoaday |
16 |
static void |
51 |
twoaday |
19 |
passphrase_put (char ctx[HASH_BUCKETS][256], const char *keyid, const char *pass) |
52 |
twoaday |
16 |
{ |
53 |
twoaday |
19 |
int pos = hash_string (keyid) % HASH_BUCKETS; |
54 |
|
|
int n = 0; |
55 |
twoaday |
16 |
|
56 |
twoaday |
19 |
while (n < HASH_BUCKETS) { |
57 |
|
|
if (strlen (ctx[(pos+n) % HASH_BUCKETS]) == 0) { |
58 |
|
|
memcpy (ctx[(pos+n) % HASH_BUCKETS], keyid, strlen (keyid)); |
59 |
|
|
strncpy (ctx[(pos+n) % HASH_BUCKETS]+strlen (keyid), pass, 240); |
60 |
|
|
break; |
61 |
|
|
} |
62 |
|
|
n++; |
63 |
twoaday |
16 |
} |
64 |
|
|
} |
65 |
|
|
|
66 |
|
|
|
67 |
twoaday |
19 |
/* Return the requested passphrase from the hash table @ctx |
68 |
|
|
using the keyid @keyid as the index. Or NULL if there is |
69 |
|
|
no stored passphrase. */ |
70 |
twoaday |
16 |
static const char* |
71 |
twoaday |
19 |
passphrase_get (char ctx[HASH_BUCKETS][256], const char *keyid) |
72 |
twoaday |
16 |
{ |
73 |
twoaday |
19 |
const char *item; |
74 |
|
|
int pos = hash_string (keyid) % HASH_BUCKETS; |
75 |
|
|
int n=0; |
76 |
twoaday |
16 |
|
77 |
twoaday |
19 |
item = gpgoe_pass_cache[pos]; |
78 |
|
|
while (strncmp (item, keyid, strlen (keyid)) && |
79 |
|
|
n < HASH_BUCKETS) { |
80 |
|
|
item = ctx[(pos+n) % HASH_BUCKETS]; |
81 |
|
|
n++; |
82 |
twoaday |
16 |
} |
83 |
twoaday |
19 |
|
84 |
|
|
if (strlen (item) > 0 && !strncmp (item, keyid, strlen (keyid))) |
85 |
|
|
return item+strlen (keyid); |
86 |
twoaday |
16 |
return NULL; |
87 |
|
|
} |
88 |
|
|
|
89 |
|
|
|
90 |
twoaday |
1 |
/* Extract public key algorithm from passwd info. */ |
91 |
|
|
const char* |
92 |
|
|
get_pubkey_algo (const char *passphrase_info) |
93 |
|
|
{ |
94 |
|
|
size_t pos = 16+1+16+1; |
95 |
|
|
|
96 |
|
|
/* AA6F7AB7DD3B4A21 E71D9BC8C93A8529 1 0 */ |
97 |
|
|
assert (strlen (passphrase_info) > pos); |
98 |
|
|
switch (atol (passphrase_info+pos)) { |
99 |
twoaday |
16 |
case 1: return "RSA"; |
100 |
twoaday |
1 |
case 17: return "DSA"; |
101 |
|
|
case 16: return "ELG"; |
102 |
|
|
} |
103 |
|
|
return "???"; |
104 |
|
|
} |
105 |
|
|
|
106 |
|
|
|
107 |
|
|
/* Passphrase callback dialog box procedure. */ |
108 |
|
|
BOOL CALLBACK |
109 |
|
|
pass_cb_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
110 |
|
|
{ |
111 |
|
|
static pass_cb_t ctx; |
112 |
twoaday |
16 |
char *info; |
113 |
twoaday |
1 |
int n; |
114 |
|
|
|
115 |
|
|
switch (msg) { |
116 |
|
|
case WM_INITDIALOG: |
117 |
|
|
ctx = (pass_cb_t)lparam; |
118 |
|
|
assert (ctx); |
119 |
|
|
if (ctx->uid_hint && ctx->passphrase_info) { |
120 |
|
|
char *utf8_uid = utf8_to_native (ctx->uid_hint+17); |
121 |
twoaday |
16 |
info = xcalloc (1, strlen (utf8_uid) + strlen (ctx->keyid) + 32); |
122 |
twoaday |
1 |
sprintf (info, _("%s\n%s key, ID %s"), utf8_uid, |
123 |
twoaday |
16 |
get_pubkey_algo (ctx->passphrase_info), ctx->keyid); |
124 |
twoaday |
1 |
SetDlgItemText (dlg, IDC_DECRYPT_MSG, info); |
125 |
|
|
free_if_alloc (utf8_uid); |
126 |
|
|
free_if_alloc (info); |
127 |
|
|
} |
128 |
twoaday |
12 |
SetDlgItemText (dlg, IDOK, _("&OK")); |
129 |
|
|
SetDlgItemText (dlg, IDCANCEL, _("&Cancel")); |
130 |
|
|
SetDlgItemText (dlg, IDC_DECRYPT_PWDINFO, _("Please enter your passphrase")); |
131 |
twoaday |
1 |
SendDlgItemMessage (dlg, IDC_DECRYPT_PWD, EM_SETPASSWORDCHAR, |
132 |
|
|
(WPARAM)(UINT)'*', 0); |
133 |
twoaday |
16 |
CheckDlgButton (dlg, IDC_DECRYPT_HIDE, BST_CHECKED); |
134 |
twoaday |
18 |
SetDlgItemText (dlg, IDC_DECRYPT_HIDE, _("&Hide typing")); |
135 |
twoaday |
12 |
SetWindowText (dlg, _("Decryption")); |
136 |
twoaday |
1 |
SetForegroundWindow (dlg); |
137 |
twoaday |
16 |
center_window (dlg, ctx->main_wnd); |
138 |
twoaday |
1 |
break; |
139 |
|
|
|
140 |
|
|
case WM_COMMAND: |
141 |
twoaday |
16 |
if (HIWORD (wparam) == BN_CLICKED && |
142 |
|
|
LOWORD (wparam) == IDC_DECRYPT_HIDE) { |
143 |
|
|
HWND hwnd; |
144 |
|
|
hwnd = GetDlgItem (dlg, IDC_DECRYPT_PWD); |
145 |
|
|
SendMessage (hwnd, EM_SETPASSWORDCHAR, |
146 |
|
|
IsDlgButtonChecked (dlg, IDC_DECRYPT_HIDE)? '*' : 0, 0); |
147 |
|
|
SetFocus (hwnd); |
148 |
|
|
break; |
149 |
|
|
} |
150 |
|
|
|
151 |
twoaday |
1 |
switch (LOWORD (wparam)) { |
152 |
|
|
case IDCANCEL: |
153 |
|
|
ctx->cancel = 1; |
154 |
|
|
free_if_alloc (ctx->pass); |
155 |
|
|
EndDialog (dlg, FALSE); |
156 |
|
|
break; |
157 |
|
|
|
158 |
|
|
case IDOK: |
159 |
|
|
if (ctx->prev_was_bad) |
160 |
|
|
SetDlgItemText (dlg, IDC_DECRYPT_PWDINFO, |
161 |
|
|
_("Invalid passphrase; please enter your passphrase again")); |
162 |
|
|
else |
163 |
|
|
SetDlgItemText (dlg, IDC_DECRYPT_PWDINFO, |
164 |
|
|
_("Please enter your passphrase")); |
165 |
twoaday |
16 |
n = SendDlgItemMessage (dlg, IDC_DECRYPT_PWD, |
166 |
twoaday |
1 |
WM_GETTEXTLENGTH, 0, 0); |
167 |
|
|
if (n < 1) { |
168 |
|
|
MessageBox (dlg, _("Please enter your passphrase"), |
169 |
|
|
_("GPG Plug-in Info"), MB_ICONINFORMATION|MB_OK); |
170 |
|
|
return FALSE; |
171 |
|
|
} |
172 |
|
|
ctx->cancel = 0; |
173 |
|
|
ctx->pass = xcalloc (1, n+2); |
174 |
|
|
GetDlgItemText (dlg, IDC_DECRYPT_PWD, ctx->pass, n+1); |
175 |
twoaday |
16 |
|
176 |
twoaday |
19 |
if (gpgoe_get_active_modes () & GPGOE_MODE_CACHEPASS) |
177 |
|
|
passphrase_put (gpgoe_pass_cache, ctx->keyid, ctx->pass); |
178 |
twoaday |
1 |
EndDialog (dlg, TRUE); |
179 |
|
|
break; |
180 |
|
|
} |
181 |
|
|
break; |
182 |
|
|
} |
183 |
|
|
return FALSE; |
184 |
|
|
} |
185 |
|
|
|
186 |
|
|
|
187 |
|
|
/* GPGME compatible passphrase callback. */ |
188 |
|
|
gpgme_error_t |
189 |
|
|
passphrase_cb (void *hook, |
190 |
|
|
const char *uid_hint, |
191 |
|
|
const char *passphrase_info, |
192 |
|
|
int prev_was_bad, int fd) |
193 |
|
|
{ |
194 |
|
|
pass_cb_t cb = (pass_cb_t)hook; |
195 |
|
|
HANDLE fp = (HANDLE)fd; |
196 |
twoaday |
16 |
const char *pass; |
197 |
twoaday |
1 |
DWORD nwritten = 0; |
198 |
|
|
|
199 |
twoaday |
19 |
assert (cb); |
200 |
twoaday |
1 |
cb->prev_was_bad = prev_was_bad; |
201 |
|
|
if (prev_was_bad && !cb->cancel) { |
202 |
|
|
wipememory (cb->pass, strlen (cb->pass)); |
203 |
|
|
free_if_alloc (cb->pass); |
204 |
|
|
} |
205 |
|
|
if (!cb->pass && !cb->cancel) { |
206 |
|
|
cb->uid_hint = uid_hint; |
207 |
|
|
cb->passphrase_info = passphrase_info; |
208 |
twoaday |
16 |
memset (cb->keyid, 0, sizeof (cb->keyid)); |
209 |
|
|
memcpy (cb->keyid, cb->passphrase_info+8, 8); |
210 |
|
|
|
211 |
twoaday |
19 |
pass = passphrase_get (gpgoe_pass_cache, cb->keyid); |
212 |
|
|
if (pass) |
213 |
twoaday |
16 |
cb->pass = xstrdup (pass); |
214 |
|
|
else |
215 |
|
|
DialogBoxParam (mod_hinst_dll, (LPCTSTR)IDD_DECRYPT, |
216 |
|
|
cb->main_wnd, pass_cb_dlg_proc, (LPARAM)cb); |
217 |
twoaday |
1 |
} |
218 |
|
|
if (cb->cancel) |
219 |
|
|
WriteFile (fp, "\n", 1, &nwritten, NULL); |
220 |
|
|
else if (cb->pass != NULL) { |
221 |
|
|
WriteFile (fp, cb->pass, strlen (cb->pass), &nwritten, NULL); |
222 |
|
|
WriteFile (fp, "\n", 1, &nwritten, NULL); |
223 |
|
|
} |
224 |
|
|
return 0; |
225 |
|
|
} |
226 |
|
|
|
227 |
|
|
|
228 |
|
|
/* Allocate new passphrase callback context. */ |
229 |
|
|
pass_cb_t |
230 |
|
|
new_pass_cb (HWND main) |
231 |
|
|
{ |
232 |
|
|
pass_cb_t cb_val; |
233 |
|
|
|
234 |
|
|
cb_val = xcalloc (1, sizeof *cb_val); |
235 |
twoaday |
16 |
cb_val->main_wnd = main; |
236 |
twoaday |
1 |
return cb_val; |
237 |
|
|
} |
238 |
|
|
|
239 |
|
|
|
240 |
|
|
|
241 |
|
|
/* Free passphrase callback context. */ |
242 |
|
|
void |
243 |
|
|
free_pass_cb (pass_cb_t cb) |
244 |
|
|
{ |
245 |
|
|
if (!cb) |
246 |
|
|
return; |
247 |
|
|
if (cb->pass) { |
248 |
|
|
wipememory (cb->pass, strlen (cb->pass)); |
249 |
|
|
free_if_alloc (cb->pass); |
250 |
|
|
} |
251 |
|
|
free_if_alloc (cb); |
252 |
|
|
} |
253 |
twoaday |
16 |
|
254 |
|
|
|
255 |
twoaday |
18 |
int |
256 |
|
|
pass_cb_cancelled (pass_cb_t cb) |
257 |
|
|
{ |
258 |
|
|
return cb->cancel; |
259 |
|
|
} |
260 |
|
|
|
261 |
|
|
|
262 |
twoaday |
19 |
/* Reset the passphrase cache. */ |
263 |
twoaday |
16 |
void |
264 |
twoaday |
19 |
reset_pass_cache (void) |
265 |
twoaday |
16 |
{ |
266 |
twoaday |
19 |
int i; |
267 |
|
|
|
268 |
|
|
for (i=0; i < HASH_BUCKETS; i++) |
269 |
|
|
wipememory (gpgoe_pass_cache[i], 256); |
270 |
twoaday |
16 |
} |