12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
* GNU General Public License for more details. |
* GNU General Public License for more details. |
|
* |
|
|
* You should have received a copy of the GNU Lesser General Public License |
|
|
* along with GPGOE; if not, write to the Free Software Foundation, |
|
|
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA7 USA |
|
15 |
*/ |
*/ |
16 |
|
|
17 |
#ifdef HAVE_CONFIG_H |
#ifdef HAVE_CONFIG_H |
24 |
#include "GPGOE.h" |
#include "GPGOE.h" |
25 |
|
|
26 |
|
|
|
/* Structure for the passphrase cache. */ |
|
|
struct pass_cache_s { |
|
|
struct pass_cache_s *next; |
|
|
char *keyid; |
|
|
char *pass; |
|
|
}; |
|
|
typedef struct pass_cache_s *pass_cache_t; |
|
|
|
|
27 |
/* Structure for the passphrase callback. */ |
/* Structure for the passphrase callback. */ |
28 |
struct pass_cb_s { |
struct pass_cb_s { |
29 |
const char *uid_hint; |
const char *uid_hint; |
30 |
const char *passphrase_info; |
const char *passphrase_info; |
31 |
char keyid[8+2]; |
char keyid[8+2]; |
32 |
char *pass; |
char *pass; |
|
pass_cache_t cache; |
|
33 |
HWND main_wnd; |
HWND main_wnd; |
34 |
int cancel; |
int cancel; |
35 |
int prev_was_bad; |
int prev_was_bad; |
36 |
}; |
}; |
37 |
|
|
38 |
|
|
39 |
/* Global passphrase cache. */ |
/* Reference to the passphrase cache. */ |
40 |
static pass_cache_t the_cache = NULL; |
extern char gpgoe_pass_cache[HASH_BUCKETS][256]; |
41 |
|
|
42 |
|
|
43 |
/* Release the passphrase cache @ctx and invalidate all passphrases. */ |
DWORD hash_string (const char *str_param); |
|
static void |
|
|
invalidate_cache (pass_cache_t ctx) |
|
|
{ |
|
|
pass_cache_t c; |
|
|
|
|
|
while (ctx) { |
|
|
c = ctx->next; |
|
|
free_if_alloc (ctx->keyid); |
|
|
wipememory (ctx->pass, strlen (ctx->pass)); |
|
|
free_if_alloc (ctx->pass); |
|
|
ctx = c; |
|
|
} |
|
|
} |
|
44 |
|
|
45 |
/* Put the passphrase @pass into the passphrase cache @ctx. */ |
/* Store the passphrase @pass in the hash table using the keyid @keyid |
46 |
|
as the index. */ |
47 |
static void |
static void |
48 |
passphrase_put (pass_cache_t *ctx, const char *keyid, const char *pass) |
passphrase_put (char ctx[HASH_BUCKETS][256], const char *keyid, const char *pass) |
49 |
{ |
{ |
50 |
pass_cache_t c, n; |
int pos = hash_string (keyid) % HASH_BUCKETS; |
51 |
|
int n, passlen; |
|
/* check if the item is already present. */ |
|
|
for (n = *ctx; n; n = n->next) { |
|
|
if (!strcmp (n->keyid, keyid)) |
|
|
return; |
|
|
} |
|
52 |
|
|
53 |
c = xcalloc (1, sizeof *c); |
/* We cannot store passphrase of that size. */ |
54 |
c->keyid = xstrdup (keyid); |
passlen = strlen (pass); |
55 |
c->pass = xstrdup (pass); |
if (passlen > 255) |
56 |
|
return; |
57 |
if (!*ctx) |
|
58 |
*ctx = c; |
n = 0; |
59 |
else { |
while (n < HASH_BUCKETS) { |
60 |
for (n = *ctx; n->next; n = n->next) |
if (strlen (ctx[(pos+n) % HASH_BUCKETS]) != 0) { |
61 |
; |
n++; |
62 |
n->next = c; |
continue; |
63 |
|
} |
64 |
|
memcpy (ctx[(pos+n) % HASH_BUCKETS], keyid, strlen (keyid)); |
65 |
|
strncpy (ctx[(pos+n) % HASH_BUCKETS]+strlen (keyid), pass, passlen); |
66 |
|
break; |
67 |
} |
} |
68 |
} |
} |
69 |
|
|
70 |
|
|
71 |
/* Return the passphrase for the key with the keyid @keyid |
/* Return the requested passphrase from the hash table @ctx |
72 |
or NULL if the passphrase was not cached for this key. */ |
using the keyid @keyid as the index. Or NULL if there is |
73 |
|
no stored passphrase. */ |
74 |
static const char* |
static const char* |
75 |
passphrase_get (pass_cache_t ctx, const char *keyid) |
passphrase_get (char ctx[HASH_BUCKETS][256], const char *keyid) |
76 |
{ |
{ |
77 |
pass_cache_t c; |
const char *item; |
78 |
|
int pos = hash_string (keyid) % HASH_BUCKETS; |
79 |
|
int n; |
80 |
|
|
81 |
for (c = ctx; c; c = c->next) { |
n = 0; |
82 |
if (!strcmp (c->keyid, keyid)) |
item = gpgoe_pass_cache[pos]; |
83 |
return c->pass; |
while (n < HASH_BUCKETS) { |
84 |
|
item = ctx[(pos+n) % HASH_BUCKETS]; |
85 |
|
if (!strncmp (item, keyid, strlen (keyid))) |
86 |
|
break; |
87 |
|
n++; |
88 |
} |
} |
89 |
|
|
90 |
|
if (strlen (item) > 0 && !strncmp (item, keyid, strlen (keyid))) |
91 |
|
return item + strlen (keyid); |
92 |
|
/* Return NULL to indicate no matching entry. */ |
93 |
return NULL; |
return NULL; |
94 |
} |
} |
95 |
|
|
96 |
|
|
|
|
|
|
|
|
97 |
/* Extract public key algorithm from passwd info. */ |
/* Extract public key algorithm from passwd info. */ |
98 |
const char* |
const char* |
99 |
get_pubkey_algo (const char *passphrase_info) |
get_pubkey_algo (const char *passphrase_info) |
106 |
case 1: return "RSA"; |
case 1: return "RSA"; |
107 |
case 17: return "DSA"; |
case 17: return "DSA"; |
108 |
case 16: return "ELG"; |
case 16: return "ELG"; |
109 |
|
default: break; |
110 |
} |
} |
111 |
return "???"; |
return "???"; |
112 |
} |
} |
123 |
switch (msg) { |
switch (msg) { |
124 |
case WM_INITDIALOG: |
case WM_INITDIALOG: |
125 |
ctx = (pass_cb_t)lparam; |
ctx = (pass_cb_t)lparam; |
126 |
assert (ctx); |
assert (ctx != NULL); |
127 |
if (ctx->uid_hint && ctx->passphrase_info) { |
if (ctx->uid_hint && ctx->passphrase_info) { |
128 |
char *utf8_uid = utf8_to_native (ctx->uid_hint+17); |
char *utf8_uid = utf8_to_native (ctx->uid_hint+17); |
129 |
info = xcalloc (1, strlen (utf8_uid) + strlen (ctx->keyid) + 32); |
info = xcalloc (1, strlen (utf8_uid) + strlen (ctx->keyid) + 32); |
130 |
sprintf (info, _("%s\n%s key, ID %s"), utf8_uid, |
sprintf (info, _("%s\n%s key, ID %s"), utf8_uid, |
131 |
get_pubkey_algo (ctx->passphrase_info), ctx->keyid); |
get_pubkey_algo (ctx->passphrase_info), ctx->keyid); |
132 |
SetDlgItemText (dlg, IDC_DECRYPT_MSG, info); |
SetDlgItemText (dlg, IDC_DECRYPT_MSG, info); |
133 |
free_if_alloc (utf8_uid); |
free_if_alloc (utf8_uid); |
134 |
free_if_alloc (info); |
free_if_alloc (info); |
177 |
_("GPG Plug-in Info"), MB_ICONINFORMATION|MB_OK); |
_("GPG Plug-in Info"), MB_ICONINFORMATION|MB_OK); |
178 |
return FALSE; |
return FALSE; |
179 |
} |
} |
180 |
|
/* Make sure we reset the cancel flag. */ |
181 |
ctx->cancel = 0; |
ctx->cancel = 0; |
182 |
ctx->pass = xcalloc (1, n+2); |
ctx->pass = xcalloc (1, n+2); |
183 |
GetDlgItemText (dlg, IDC_DECRYPT_PWD, ctx->pass, n+1); |
GetDlgItemText (dlg, IDC_DECRYPT_PWD, ctx->pass, n+1); |
184 |
|
|
185 |
/*passphrase_put (&the_cache, ctx->keyid, ctx->pass);*/ |
if (gpgoe_get_active_modes () & GPGOE_MODE_CACHEPASS) |
186 |
|
passphrase_put (gpgoe_pass_cache, ctx->keyid, ctx->pass); |
187 |
EndDialog (dlg, TRUE); |
EndDialog (dlg, TRUE); |
188 |
break; |
break; |
189 |
} |
} |
203 |
pass_cb_t cb = (pass_cb_t)hook; |
pass_cb_t cb = (pass_cb_t)hook; |
204 |
HANDLE fp = (HANDLE)fd; |
HANDLE fp = (HANDLE)fd; |
205 |
const char *pass; |
const char *pass; |
206 |
DWORD nwritten = 0; |
DWORD nwritten; |
207 |
|
|
208 |
|
assert (cb != NULL); |
209 |
cb->prev_was_bad = prev_was_bad; |
cb->prev_was_bad = prev_was_bad; |
210 |
if (prev_was_bad && !cb->cancel) { |
if (prev_was_bad && !cb->cancel) { |
211 |
wipememory (cb->pass, strlen (cb->pass)); |
wipememory (cb->pass, strlen (cb->pass)); |
217 |
memset (cb->keyid, 0, sizeof (cb->keyid)); |
memset (cb->keyid, 0, sizeof (cb->keyid)); |
218 |
memcpy (cb->keyid, cb->passphrase_info+8, 8); |
memcpy (cb->keyid, cb->passphrase_info+8, 8); |
219 |
|
|
220 |
if (the_cache && (pass=passphrase_get (the_cache, cb->keyid))) |
pass = passphrase_get (gpgoe_pass_cache, cb->keyid); |
221 |
|
if (pass) |
222 |
cb->pass = xstrdup (pass); |
cb->pass = xstrdup (pass); |
223 |
else |
else |
224 |
DialogBoxParam (mod_hinst_dll, (LPCTSTR)IDD_DECRYPT, |
DialogBoxParam (mod_hinst_dll, (LPCTSTR)IDD_DECRYPT, |
261 |
} |
} |
262 |
|
|
263 |
|
|
264 |
|
/* Return 1 if the passphrase dialog were cancelled. */ |
265 |
int |
int |
266 |
pass_cb_cancelled (pass_cb_t cb) |
pass_cb_cancelled (pass_cb_t cb) |
267 |
{ |
{ |
269 |
} |
} |
270 |
|
|
271 |
|
|
272 |
/* Release the passphrase cache. */ |
/* Reset the passphrase cache by overwritting each items with zeroes */ |
273 |
void |
void |
274 |
free_pass_cache (void) |
reset_pass_cache (void) |
275 |
{ |
{ |
276 |
invalidate_cache (the_cache); |
int i; |
277 |
the_cache = NULL; |
|
278 |
|
for (i=0; i < HASH_BUCKETS; i++) |
279 |
|
wipememory (gpgoe_pass_cache[i], DIM (gpgoe_pass_cache[i])); |
280 |
} |
} |