28 |
#include "GPGOE.h" |
#include "GPGOE.h" |
29 |
|
|
30 |
|
|
|
/* 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; |
|
|
|
|
31 |
/* Structure for the passphrase callback. */ |
/* Structure for the passphrase callback. */ |
32 |
struct pass_cb_s { |
struct pass_cb_s { |
33 |
const char *uid_hint; |
const char *uid_hint; |
34 |
const char *passphrase_info; |
const char *passphrase_info; |
35 |
char keyid[8+2]; |
char keyid[8+2]; |
36 |
char *pass; |
char *pass; |
|
pass_cache_t cache; |
|
37 |
HWND main_wnd; |
HWND main_wnd; |
38 |
int cancel; |
int cancel; |
39 |
int prev_was_bad; |
int prev_was_bad; |
40 |
}; |
}; |
41 |
|
|
42 |
|
|
43 |
/* Global passphrase cache. */ |
extern char gpgoe_pass_cache[HASH_BUCKETS][256]; |
|
static pass_cache_t the_cache = NULL; |
|
|
|
|
44 |
|
|
|
/* Release the passphrase cache @ctx and invalidate all passphrases. */ |
|
|
static void |
|
|
invalidate_cache (pass_cache_t ctx) |
|
|
{ |
|
|
pass_cache_t c; |
|
45 |
|
|
46 |
while (ctx) { |
DWORD hash_string (const char *str_param); |
|
c = ctx->next; |
|
|
free_if_alloc (ctx->keyid); |
|
|
wipememory (ctx->pass, strlen (ctx->pass)); |
|
|
free_if_alloc (ctx->pass); |
|
|
ctx = c; |
|
|
} |
|
|
} |
|
47 |
|
|
48 |
/* Put the passphrase @pass into the passphrase cache @ctx. */ |
/* Store the passphrase @pass in the hash table using the keyid @keyid |
49 |
|
as the index. */ |
50 |
static void |
static void |
51 |
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) |
52 |
{ |
{ |
53 |
pass_cache_t c, n; |
int pos = hash_string (keyid) % HASH_BUCKETS; |
54 |
|
int n = 0; |
55 |
|
|
56 |
/* check if the item is already present. */ |
while (n < HASH_BUCKETS) { |
57 |
for (n = *ctx; n; n = n->next) { |
if (strlen (ctx[(pos+n) % HASH_BUCKETS]) == 0) { |
58 |
if (!strcmp (n->keyid, keyid)) |
memcpy (ctx[(pos+n) % HASH_BUCKETS], keyid, strlen (keyid)); |
59 |
return; |
strncpy (ctx[(pos+n) % HASH_BUCKETS]+strlen (keyid), pass, 240); |
60 |
} |
break; |
61 |
|
} |
62 |
c = xcalloc (1, sizeof *c); |
n++; |
|
c->keyid = xstrdup (keyid); |
|
|
c->pass = xstrdup (pass); |
|
|
|
|
|
if (!*ctx) |
|
|
*ctx = c; |
|
|
else { |
|
|
for (n = *ctx; n->next; n = n->next) |
|
|
; |
|
|
n->next = c; |
|
63 |
} |
} |
64 |
} |
} |
65 |
|
|
66 |
|
|
67 |
/* Return the passphrase for the key with the keyid @keyid |
/* Return the requested passphrase from the hash table @ctx |
68 |
or NULL if the passphrase was not cached for this key. */ |
using the keyid @keyid as the index. Or NULL if there is |
69 |
|
no stored passphrase. */ |
70 |
static const char* |
static const char* |
71 |
passphrase_get (pass_cache_t ctx, const char *keyid) |
passphrase_get (char ctx[HASH_BUCKETS][256], const char *keyid) |
72 |
{ |
{ |
73 |
pass_cache_t c; |
const char *item; |
74 |
|
int pos = hash_string (keyid) % HASH_BUCKETS; |
75 |
|
int n=0; |
76 |
|
|
77 |
for (c = ctx; c; c = c->next) { |
item = gpgoe_pass_cache[pos]; |
78 |
if (!strcmp (c->keyid, keyid)) |
while (strncmp (item, keyid, strlen (keyid)) && |
79 |
return c->pass; |
n < HASH_BUCKETS) { |
80 |
|
item = ctx[(pos+n) % HASH_BUCKETS]; |
81 |
|
n++; |
82 |
} |
} |
83 |
|
|
84 |
|
if (strlen (item) > 0 && !strncmp (item, keyid, strlen (keyid))) |
85 |
|
return item+strlen (keyid); |
86 |
return NULL; |
return NULL; |
87 |
} |
} |
88 |
|
|
89 |
|
|
|
|
|
|
|
|
90 |
/* Extract public key algorithm from passwd info. */ |
/* Extract public key algorithm from passwd info. */ |
91 |
const char* |
const char* |
92 |
get_pubkey_algo (const char *passphrase_info) |
get_pubkey_algo (const char *passphrase_info) |
173 |
ctx->pass = xcalloc (1, n+2); |
ctx->pass = xcalloc (1, n+2); |
174 |
GetDlgItemText (dlg, IDC_DECRYPT_PWD, ctx->pass, n+1); |
GetDlgItemText (dlg, IDC_DECRYPT_PWD, ctx->pass, n+1); |
175 |
|
|
176 |
/*passphrase_put (&the_cache, ctx->keyid, ctx->pass);*/ |
if (gpgoe_get_active_modes () & GPGOE_MODE_CACHEPASS) |
177 |
|
passphrase_put (gpgoe_pass_cache, ctx->keyid, ctx->pass); |
178 |
EndDialog (dlg, TRUE); |
EndDialog (dlg, TRUE); |
179 |
break; |
break; |
180 |
} |
} |
196 |
const char *pass; |
const char *pass; |
197 |
DWORD nwritten = 0; |
DWORD nwritten = 0; |
198 |
|
|
199 |
|
assert (cb); |
200 |
cb->prev_was_bad = prev_was_bad; |
cb->prev_was_bad = prev_was_bad; |
201 |
if (prev_was_bad && !cb->cancel) { |
if (prev_was_bad && !cb->cancel) { |
202 |
wipememory (cb->pass, strlen (cb->pass)); |
wipememory (cb->pass, strlen (cb->pass)); |
208 |
memset (cb->keyid, 0, sizeof (cb->keyid)); |
memset (cb->keyid, 0, sizeof (cb->keyid)); |
209 |
memcpy (cb->keyid, cb->passphrase_info+8, 8); |
memcpy (cb->keyid, cb->passphrase_info+8, 8); |
210 |
|
|
211 |
if (the_cache && (pass=passphrase_get (the_cache, cb->keyid))) |
pass = passphrase_get (gpgoe_pass_cache, cb->keyid); |
212 |
|
if (pass) |
213 |
cb->pass = xstrdup (pass); |
cb->pass = xstrdup (pass); |
214 |
else |
else |
215 |
DialogBoxParam (mod_hinst_dll, (LPCTSTR)IDD_DECRYPT, |
DialogBoxParam (mod_hinst_dll, (LPCTSTR)IDD_DECRYPT, |
259 |
} |
} |
260 |
|
|
261 |
|
|
262 |
/* Release the passphrase cache. */ |
/* Reset the passphrase cache. */ |
263 |
void |
void |
264 |
free_pass_cache (void) |
reset_pass_cache (void) |
265 |
{ |
{ |
266 |
invalidate_cache (the_cache); |
int i; |
267 |
the_cache = NULL; |
|
268 |
|
for (i=0; i < HASH_BUCKETS; i++) |
269 |
|
wipememory (gpgoe_pass_cache[i], 256); |
270 |
} |
} |