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 |
|
17 |
#ifdef HAVE_CONFIG_H |
18 |
#include <config.h> |
19 |
#endif |
20 |
#include <windows.h> |
21 |
#include <assert.h> |
22 |
#include "resource.h" |
23 |
#include "gpgme.h" |
24 |
#include "GPGOE.h" |
25 |
|
26 |
|
27 |
/* Structure for the passphrase callback. */ |
28 |
struct pass_cb_s { |
29 |
const char *uid_hint; |
30 |
const char *passphrase_info; |
31 |
char keyid[8+2]; |
32 |
char *pass; |
33 |
HWND main_wnd; |
34 |
int cancel; |
35 |
int prev_was_bad; |
36 |
}; |
37 |
|
38 |
|
39 |
/* Reference to the passphrase cache. */ |
40 |
extern char gpgoe_pass_cache[HASH_BUCKETS][MAX_PASS_LEN]; |
41 |
|
42 |
|
43 |
DWORD hash_string (const char *str_param); |
44 |
|
45 |
/* Store the passphrase @pass in the hash table using the keyid @keyid |
46 |
as the index. */ |
47 |
static void |
48 |
passphrase_put (char ctx[HASH_BUCKETS][MAX_PASS_LEN], const char *keyid, |
49 |
const char *pass) |
50 |
{ |
51 |
int pos = hash_string (keyid) % HASH_BUCKETS; |
52 |
int n, passlen; |
53 |
|
54 |
/* We cannot store passphrase of that size. */ |
55 |
passlen = strlen (pass); |
56 |
if (passlen > MAX_PASS_LEN-1) |
57 |
return; |
58 |
|
59 |
n = 0; |
60 |
while (n < HASH_BUCKETS) { |
61 |
if (strlen (ctx[(pos+n) % HASH_BUCKETS]) != 0) { |
62 |
n++; |
63 |
continue; |
64 |
} |
65 |
memcpy (ctx[(pos+n) % HASH_BUCKETS], keyid, strlen (keyid)); |
66 |
strncpy (ctx[(pos+n) % HASH_BUCKETS]+strlen (keyid), pass, passlen); |
67 |
break; |
68 |
} |
69 |
} |
70 |
|
71 |
|
72 |
/* Return the requested passphrase from the hash table @ctx |
73 |
using the keyid @keyid as the index. Or NULL if there is |
74 |
no stored passphrase. */ |
75 |
static const char* |
76 |
passphrase_get (char ctx[HASH_BUCKETS][MAX_PASS_LEN], const char *keyid) |
77 |
{ |
78 |
const char *item; |
79 |
int pos = hash_string (keyid) % HASH_BUCKETS; |
80 |
int n; |
81 |
|
82 |
n = 0; |
83 |
item = gpgoe_pass_cache[pos]; |
84 |
while (n < HASH_BUCKETS) { |
85 |
item = ctx[(pos+n) % HASH_BUCKETS]; |
86 |
if (!strncmp (item, keyid, strlen (keyid))) |
87 |
break; |
88 |
n++; |
89 |
} |
90 |
|
91 |
if (strlen (item) > 0 && !strncmp (item, keyid, strlen (keyid))) |
92 |
return item + strlen (keyid); |
93 |
/* Return NULL to indicate no matching entry. */ |
94 |
return NULL; |
95 |
} |
96 |
|
97 |
|
98 |
/* Extract public key algorithm from passwd info. */ |
99 |
const char* |
100 |
get_pubkey_algo (const char *passphrase_info) |
101 |
{ |
102 |
size_t pos = 16+1+16+1; |
103 |
|
104 |
/* AA6F7AB7DD3B4A21 E71D9BC8C93A8529 1 0 */ |
105 |
assert (strlen (passphrase_info) > pos); |
106 |
switch (atol (passphrase_info+pos)) { |
107 |
case 1: return "RSA"; |
108 |
case 17: return "DSA"; |
109 |
case 16: return "ELG"; |
110 |
default: break; |
111 |
} |
112 |
return "???"; |
113 |
} |
114 |
|
115 |
|
116 |
/* Passphrase callback dialog box procedure. */ |
117 |
BOOL CALLBACK |
118 |
pass_cb_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
119 |
{ |
120 |
static pass_cb_t ctx; |
121 |
char *info; |
122 |
int n; |
123 |
|
124 |
switch (msg) { |
125 |
case WM_INITDIALOG: |
126 |
ctx = (pass_cb_t)lparam; |
127 |
assert (ctx != NULL); |
128 |
if (ctx->uid_hint && ctx->passphrase_info) { |
129 |
char *utf8_uid = utf8_to_native (ctx->uid_hint+17); |
130 |
info = xcalloc (1, strlen (utf8_uid) + strlen (ctx->keyid) + 32); |
131 |
sprintf (info, _("%s\n%s key, ID %s"), utf8_uid, |
132 |
get_pubkey_algo (ctx->passphrase_info), ctx->keyid); |
133 |
SetDlgItemText (dlg, IDC_DECRYPT_MSG, info); |
134 |
free_if_alloc (utf8_uid); |
135 |
free_if_alloc (info); |
136 |
} |
137 |
SetDlgItemText (dlg, IDOK, _("&OK")); |
138 |
SetDlgItemText (dlg, IDCANCEL, _("&Cancel")); |
139 |
SetDlgItemText (dlg, IDC_DECRYPT_PWDINFO, _("Please enter your passphrase")); |
140 |
SendDlgItemMessage (dlg, IDC_DECRYPT_PWD, EM_SETPASSWORDCHAR, |
141 |
(WPARAM)(UINT)'*', 0); |
142 |
CheckDlgButton (dlg, IDC_DECRYPT_HIDE, BST_CHECKED); |
143 |
SetDlgItemText (dlg, IDC_DECRYPT_HIDE, _("&Hide typing")); |
144 |
SetWindowText (dlg, _("Decryption")); |
145 |
SetForegroundWindow (dlg); |
146 |
center_window (dlg, ctx->main_wnd); |
147 |
break; |
148 |
|
149 |
case WM_COMMAND: |
150 |
if (HIWORD (wparam) == BN_CLICKED && |
151 |
LOWORD (wparam) == IDC_DECRYPT_HIDE) { |
152 |
HWND hwnd; |
153 |
hwnd = GetDlgItem (dlg, IDC_DECRYPT_PWD); |
154 |
SendMessage (hwnd, EM_SETPASSWORDCHAR, |
155 |
IsDlgButtonChecked (dlg, IDC_DECRYPT_HIDE)? '*' : 0, 0); |
156 |
SetFocus (hwnd); |
157 |
break; |
158 |
} |
159 |
|
160 |
switch (LOWORD (wparam)) { |
161 |
case IDCANCEL: |
162 |
ctx->cancel = 1; |
163 |
free_if_alloc (ctx->pass); |
164 |
EndDialog (dlg, FALSE); |
165 |
break; |
166 |
|
167 |
case IDOK: |
168 |
if (ctx->prev_was_bad) |
169 |
SetDlgItemText (dlg, IDC_DECRYPT_PWDINFO, |
170 |
_("Invalid passphrase; please enter your passphrase again")); |
171 |
else |
172 |
SetDlgItemText (dlg, IDC_DECRYPT_PWDINFO, |
173 |
_("Please enter your passphrase")); |
174 |
n = SendDlgItemMessage (dlg, IDC_DECRYPT_PWD, |
175 |
WM_GETTEXTLENGTH, 0, 0); |
176 |
if (n < 1) { |
177 |
MessageBox (dlg, _("Please enter your passphrase"), |
178 |
_("GPG Plug-in Info"), MB_ICONINFORMATION|MB_OK); |
179 |
return FALSE; |
180 |
} |
181 |
/* Make sure we reset the cancel flag. */ |
182 |
ctx->cancel = 0; |
183 |
ctx->pass = xcalloc (1, n+2); |
184 |
GetDlgItemText (dlg, IDC_DECRYPT_PWD, ctx->pass, n+1); |
185 |
|
186 |
if (gpgoe_get_active_modes () & GPGOE_MODE_CACHEPASS) |
187 |
passphrase_put (gpgoe_pass_cache, ctx->keyid, ctx->pass); |
188 |
EndDialog (dlg, TRUE); |
189 |
break; |
190 |
} |
191 |
break; |
192 |
} |
193 |
return FALSE; |
194 |
} |
195 |
|
196 |
|
197 |
/* GPGME compatible passphrase callback. */ |
198 |
gpgme_error_t |
199 |
passphrase_cb (void *hook, |
200 |
const char *uid_hint, |
201 |
const char *passphrase_info, |
202 |
int prev_was_bad, int fd) |
203 |
{ |
204 |
pass_cb_t cb = (pass_cb_t)hook; |
205 |
HANDLE fp = (HANDLE)fd; |
206 |
const char *pass; |
207 |
DWORD nwritten; |
208 |
|
209 |
assert (cb != NULL); |
210 |
cb->prev_was_bad = prev_was_bad; |
211 |
if (prev_was_bad && !cb->cancel) { |
212 |
wipememory (cb->pass, strlen (cb->pass)); |
213 |
free_if_alloc (cb->pass); |
214 |
} |
215 |
if (!cb->pass && !cb->cancel) { |
216 |
cb->uid_hint = uid_hint; |
217 |
cb->passphrase_info = passphrase_info; |
218 |
memset (cb->keyid, 0, sizeof (cb->keyid)); |
219 |
memcpy (cb->keyid, cb->passphrase_info+8, 8); |
220 |
|
221 |
pass = passphrase_get (gpgoe_pass_cache, cb->keyid); |
222 |
if (pass) |
223 |
cb->pass = xstrdup (pass); |
224 |
else |
225 |
DialogBoxParam (mod_hinst_dll, (LPCTSTR)IDD_DECRYPT, |
226 |
cb->main_wnd, pass_cb_dlg_proc, (LPARAM)cb); |
227 |
} |
228 |
if (cb->cancel) |
229 |
WriteFile (fp, "\n", 1, &nwritten, NULL); |
230 |
else if (cb->pass != NULL) { |
231 |
WriteFile (fp, cb->pass, strlen (cb->pass), &nwritten, NULL); |
232 |
WriteFile (fp, "\n", 1, &nwritten, NULL); |
233 |
} |
234 |
return 0; |
235 |
} |
236 |
|
237 |
|
238 |
/* Allocate new passphrase callback context. */ |
239 |
pass_cb_t |
240 |
new_pass_cb (HWND main) |
241 |
{ |
242 |
pass_cb_t cb_val; |
243 |
|
244 |
cb_val = xcalloc (1, sizeof *cb_val); |
245 |
cb_val->main_wnd = main; |
246 |
return cb_val; |
247 |
} |
248 |
|
249 |
|
250 |
|
251 |
/* Free passphrase callback context. */ |
252 |
void |
253 |
free_pass_cb (pass_cb_t cb) |
254 |
{ |
255 |
if (!cb) |
256 |
return; |
257 |
if (cb->pass) { |
258 |
wipememory (cb->pass, strlen (cb->pass)); |
259 |
free_if_alloc (cb->pass); |
260 |
} |
261 |
free_if_alloc (cb); |
262 |
} |
263 |
|
264 |
|
265 |
/* Return 1 if the passphrase dialog were cancelled. */ |
266 |
int |
267 |
pass_cb_cancelled (pass_cb_t cb) |
268 |
{ |
269 |
return cb->cancel; |
270 |
} |
271 |
|
272 |
|
273 |
/* Reset the passphrase cache by overwritting each items with zeroes */ |
274 |
void |
275 |
reset_pass_cache (void) |
276 |
{ |
277 |
int i; |
278 |
|
279 |
for (i=0; i < HASH_BUCKETS; i++) |
280 |
wipememory (gpgoe_pass_cache[i], DIM (gpgoe_pass_cache[i])); |
281 |
} |