1 |
/* wptGPGME.cpp - WinPT GPGME interface |
/* wptGPGME.cpp - WinPT GPGME interface |
2 |
* Copyright (C) 2001-2005 Timo Schulz |
* Copyright (C) 2001-2006 Timo Schulz |
3 |
* |
* |
4 |
* This file is part of WinPT. |
* This file is part of WinPT. |
5 |
* |
* |
6 |
* WinPT is free software; you can redistribute it and/or |
* WinPT is free software; you can redistribute it and/or |
7 |
* modify it under the terms of the GNU General Public License |
* modify it under the terms of the GNU General Public License |
8 |
* as published by the Free Software Foundation; either version 2 |
* as published by the Free Software Foundation; either version 2 |
9 |
* of the License, or (at your option) any later version. |
* of the License, or (at your option) any later version. |
10 |
* |
* |
11 |
* WinPT is distributed in the hope that it will be useful, |
* WinPT is distributed in the hope that it will be useful, |
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 GNU |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 |
* General Public License for more details. |
* General Public License for more details. |
15 |
* |
*/ |
16 |
* You should have received a copy of the GNU General Public License |
#ifdef HAVE_CONFIG_H |
17 |
* along with WinPT; if not, write to the Free Software Foundation, |
#include <config.h> |
18 |
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
#endif |
19 |
*/ |
|
20 |
|
#include <sys/types.h> |
21 |
#include <sys/types.h> |
#include <windows.h> |
22 |
#include <windows.h> |
|
23 |
|
#include "resource.h" |
24 |
#include "../resource.h" |
#include "wptNLS.h" |
25 |
#include "wptNLS.h" |
#include "wptGPG.h" |
26 |
#include "wptGPG.h" |
#include "wptErrors.h" |
27 |
#include "wptErrors.h" |
#include "wptTypes.h" |
28 |
#include "wptTypes.h" |
#include "wptW32API.h" |
29 |
#include "wptW32API.h" |
#include "wptVersion.h" |
30 |
#include "wptVersion.h" |
#include "wptCommonCtl.h" |
31 |
#include "wptCommonCtl.h" |
#include "wptContext.h" |
32 |
#include "wptContext.h" |
#include "wptRegistry.h" |
33 |
#include "wptRegistry.h" |
#include "wptDlgs.h" |
34 |
#include "wptDlgs.h" |
|
35 |
|
#include "openpgp.h" |
36 |
#include "openpgp.h" |
|
37 |
|
BOOL CALLBACK keycache_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, |
38 |
BOOL CALLBACK keycache_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam); |
LPARAM lparam); |
39 |
void progress_cleanup (progress_filter_s * pfx); |
void progress_cleanup (progress_filter_s * pfx); |
40 |
|
|
41 |
static gpg_keycache_t pub = NULL; |
/* Global GPG key cache contexts. */ |
42 |
static gpg_keycache_t sec = NULL; |
static gpg_keycache_t pub = NULL; |
43 |
static unsigned int reload = 0; |
static gpg_keycache_t sec = NULL; |
44 |
static char *gpg_secring = NULL; |
static char *gpg_secring = NULL; |
45 |
|
|
46 |
|
struct keycache_s *key_list = NULL; |
47 |
/* Reload the key cache. */ |
|
48 |
void |
|
49 |
keycache_reload (HWND dlg) |
/* Return 1 if no cache is available. |
50 |
{ |
This can be the case if WinPT were run in command line mode. */ |
51 |
refresh_cache_s rcs; |
int |
52 |
|
keycache_not_available (void) |
53 |
memset (&rcs, 0, sizeof rcs); |
{ |
54 |
rcs.kr_reload = rcs.kr_update = 1; |
return pub == NULL; |
55 |
rcs.tr_update = 0; |
} |
56 |
DialogBoxParam (glob_hinst, (LPCSTR)IDD_WINPT_KEYCACHE, dlg, |
|
57 |
keycache_dlg_proc, (LPARAM)&rcs); |
/* Release both key cache objects. If @cleanup is 1, |
58 |
} |
also release other global structs. */ |
59 |
|
void |
60 |
|
keycache_release (int cleanup) |
61 |
/* Release both key cache objects. If @cleanup is 1, |
{ |
62 |
also release other global structs. */ |
char tmpbuf[64]; |
63 |
void |
int n = gpg_keycache_get_size (pub); |
64 |
keycache_release (int cleanup) |
|
65 |
{ |
/* XXX: update the value when the cache has changed. */ |
66 |
int n = gpg_keycache_get_size (pub); |
_snprintf (tmpbuf, DIM (tmpbuf)-1, "%d", n); |
67 |
char tmpbuf[64]; |
set_reg_key (HKEY_CURRENT_USER, "Software\\WinPT", "nKeys", tmpbuf); |
68 |
|
|
69 |
/* XXX: update the value when the cache has changed. */ |
if (pub) { |
70 |
sprintf (tmpbuf, "%d", n); |
gpg_keycache_release (pub); |
71 |
set_reg_key (HKEY_CURRENT_USER, "Software\\WinPT", "nKeys", tmpbuf); |
pub = NULL; |
72 |
|
} |
73 |
if (pub) { |
if (sec) { |
74 |
gpg_keycache_release (pub); |
gpg_keycache_release (sec); |
75 |
pub = NULL; |
sec = NULL; |
76 |
} |
} |
77 |
if (sec) { |
if (cleanup) |
78 |
gpg_keycache_release (sec); |
safe_free (gpg_secring); |
79 |
sec = NULL; |
} |
80 |
} |
|
81 |
if (cleanup) { |
|
82 |
if (gpg_secring) |
/* Update the key with the keyid @keyid in the key cache. |
83 |
free (gpg_secring); |
If @is_sec is 1, the secret key cache is used. */ |
84 |
gpg_secring = NULL; |
gpgme_error_t |
85 |
} |
keycache_update (int is_sec, const char *keyid) |
86 |
} |
{ |
87 |
|
gpg_keycache_t ctx = pub; |
88 |
|
gpgme_error_t err; |
89 |
/* Update the key with the keyid @keyid in the key cache. |
|
90 |
If @is_sec is 1, the secret key cache is used. */ |
if (is_sec) |
91 |
gpgme_error_t |
ctx = sec; |
92 |
keycache_update (int is_sec, const char *keyid) |
err = gpg_keycache_update_key (ctx, is_sec, pub, keyid); |
93 |
{ |
if (is_sec) |
94 |
gpg_keycache_t ctx = pub; |
gpg_keycache_prepare_single (ctx, keyid, NULL, gpg_secring); |
95 |
gpgme_error_t err; |
return err; |
96 |
|
} |
97 |
if (is_sec) |
|
98 |
ctx = sec; |
|
99 |
err = gpg_keycache_update_key (ctx, is_sec, pub, keyid); |
/* Initialize both cache contexts. Use @pubring for the public |
100 |
if (is_sec) |
keyring and @secring for the secret keyring. */ |
101 |
gpg_keycache_prepare_single (ctx, keyid, NULL, gpg_secring); |
gpgme_error_t |
102 |
return err; |
keycache_init (const char *pubring, const char *secring) |
103 |
} |
{ |
104 |
|
struct progress_filter_s pfx; |
105 |
/* XXX: cache_keyring_names must be called then the GPG homedir changes! */ |
gpgme_error_t err; |
106 |
|
int val; |
107 |
/* Initialize both cache contexts. Use @pubring for the public |
char *p; |
108 |
keyring and @secring for the secret keyring. */ |
|
109 |
gpgme_error_t |
if (secring != NULL) { |
110 |
keycache_init (const char *pubring, const char *secring) |
free_if_alloc (gpg_secring); |
111 |
{ |
gpg_secring = get_gnupg_keyring (0, NO_STRICT); |
112 |
struct progress_filter_s pfx; |
log_debug ("keycache_init: secring path '%s'\r\n", gpg_secring); |
113 |
gpgme_error_t err; |
} |
114 |
int val = 0; |
|
115 |
char *p; |
val = 0; |
116 |
|
p = get_reg_entry (HKEY_CURRENT_USER, "Software\\WinPT", "nKeys"); |
117 |
if (secring != NULL) { |
if (p && *p != ' ') { |
118 |
free_if_alloc (gpg_secring); |
val = atoi (p); |
119 |
gpg_secring = get_gnupg_keyring (0, NO_STRICT); |
free_if_alloc (p); |
120 |
} |
} |
121 |
|
|
122 |
if (reload) { |
memset (&pfx, 0, sizeof (pfx)); |
123 |
keycache_release (0); |
keycache_release (0); /* Release old contexts first. */ |
124 |
reload = 0; |
|
125 |
} |
err = gpg_keycache_new (&pub); |
126 |
p = get_reg_entry (HKEY_CURRENT_USER, "Software\\WinPT", "nKeys"); |
if (err) |
127 |
if (p && *p != ' ') { |
return err; |
128 |
val = atoi (p); |
if (val != 0) |
129 |
free_if_alloc (p); |
gpg_keycache_set_cb (pub, progress_callback, &pfx, val); |
130 |
memset (&pfx, 0, sizeof (pfx)); |
err = gpg_keycache_new (&sec); |
131 |
} |
if (!err) |
132 |
|
err = gpg_keycache_init (pub, NULL, 0); |
133 |
err = gpg_keycache_new (&pub); |
if (!err) |
134 |
if (err) |
err = gpg_keycache_init (sec, NULL, 1); |
135 |
return err; |
if (!err && pubring && *pubring) |
136 |
if (val != 0) |
err = gpg_keycache_prepare (pub, pubring, NULL); |
137 |
gpg_keycache_set_cb (pub, progress_callback, &pfx, val); |
if (!err && secring && * secring) |
138 |
err = gpg_keycache_new (&sec); |
err = gpg_keycache_prepare (sec, NULL, secring); |
139 |
if (!err) |
if (!err) |
140 |
err = gpg_keycache_init (pub, NULL, 0); |
gpg_keycache_sync (pub, sec); |
141 |
if (!err) |
if (val != 0) |
142 |
err = gpg_keycache_init( sec, NULL, 1 ); |
progress_cleanup (&pfx); |
143 |
if( !err && pubring && *pubring ) |
|
144 |
err = gpg_keycache_prepare( pub, pubring, NULL ); |
return err; |
145 |
if( !err && secring && * secring ) |
} |
146 |
err = gpg_keycache_prepare( sec, NULL, secring ); |
|
147 |
if (!err) |
|
148 |
gpg_keycache_sync (pub, sec); |
/* Return the public cache context if @is_pub is set |
149 |
if (val != 0) |
the secre cache context otherwise. */ |
150 |
progress_cleanup (&pfx); |
gpg_keycache_t |
151 |
return err; |
keycache_get_ctx (int is_pub) |
152 |
} |
{ |
153 |
|
gpg_keycache_t ctx; |
154 |
|
|
155 |
/* If @val = 1 indicate to reload the cache. */ |
ctx = is_pub? pub : sec; |
156 |
void |
if (!ctx) |
157 |
keycache_set_reload (int val) |
BUG (0); |
158 |
{ |
return ctx; |
159 |
reload = val; |
} |
160 |
} |
|
161 |
|
|
162 |
|
/* Get the GPG key with keyid @keyid from the cache. Return it |
163 |
/* Return the reload cache flag. */ |
in @r_key on success. */ |
164 |
int |
static gpgme_error_t |
165 |
keycache_get_reload (void) |
get_key_from_cache (const char *keyid, int secret, gpgme_key_t *r_key, |
166 |
{ |
struct keycache_s **c) |
167 |
return reload; |
{ |
168 |
} |
gpg_keycache_t cache; |
169 |
|
gpgme_error_t err; |
170 |
|
int mode = secret? KEYCACHE_PRV : KEYCACHE_PUB; |
171 |
/* Return the public cache context if @is_pub is set |
|
172 |
the secre cache context otherwise. */ |
if (!keyid) |
173 |
gpg_keycache_t |
return gpg_error (GPG_ERR_INV_VALUE); |
174 |
keycache_get_ctx (int is_pub) |
if (r_key) |
175 |
{ |
*r_key = NULL; |
176 |
return is_pub? pub : sec; |
cache = keycache_get_ctx (mode); |
177 |
} |
if (!c) |
178 |
|
err = gpg_keycache_find_key (cache, keyid, 0, r_key); |
179 |
|
else |
180 |
/* Get the GPG key with keyid @keyid from the cache. Return it |
err = gpg_keycache_find_key2 (cache, keyid, 0, r_key, c); |
181 |
in @r_key on success. */ |
return err; |
182 |
static int |
} |
183 |
get_key_from_cache (const char *keyid, gpgme_key_t *r_key, |
|
184 |
struct keycache_s **c, int secret) |
|
185 |
{ |
/* Release the internal key structure. |
186 |
gpg_keycache_t cache; |
If allocated is 0, assume fixed cache item. */ |
187 |
gpgme_error_t err; |
void |
188 |
int mode = secret? KEYCACHE_PRV : KEYCACHE_PUB; |
winpt_release_pubkey (winpt_key_s *k) |
189 |
|
{ |
190 |
if (!keyid) |
/*log_box ("debug", 0, "alloc %d", k->allocated);*/ |
191 |
return WPTERR_GENERAL; |
if (!k->allocated) |
192 |
if (r_key) |
return; |
193 |
*r_key = NULL; |
gpg_keycache_item_release (k->ext); |
194 |
cache = keycache_get_ctx (mode); |
k->ext = NULL; |
195 |
if (!cache) |
k->allocated = 0; |
196 |
BUG( NULL ); |
} |
197 |
if (!c) |
|
198 |
err = gpg_keycache_find_key (cache, keyid, 0, r_key); |
|
199 |
else |
/* Search the public key with @keyid as the keyid in the cache and |
200 |
err = gpg_keycache_find_key2 (cache, keyid, 0, r_key, c); |
return the item in @k. */ |
201 |
return err? WPTERR_GENERAL : 0; |
gpgme_error_t |
202 |
} |
winpt_get_pubkey (const char *keyid, winpt_key_s *k) |
203 |
|
{ |
204 |
|
gpgme_error_t err = gpg_error (GPG_ERR_NO_ERROR); |
205 |
/* Get GPG key with keyid @keyid directly from GPG and return |
|
206 |
it in @r_key on success. */ |
if (pub) |
207 |
static int |
err = get_key_from_cache (keyid, 0, &k->ctx, &k->ext); |
208 |
get_key_directly (const char *keyid, gpgme_key_t *r_key, int secret) |
else |
209 |
{ |
err = gpg_keycache_fetch_key (keyid, 0, &k->ctx, &k->ext); |
210 |
gpgme_ctx_t ctx; |
if (err) |
211 |
gpgme_error_t err; |
return err; |
212 |
|
k->is_v3 = k->ctx->subkeys->pubkey_algo == GPGME_PK_RSA && |
213 |
err = gpgme_new (&ctx); |
strlen (k->ctx->subkeys->fpr) == 32; |
214 |
if (err) |
k->is_protected = k->ext->gloflags.is_protected; |
215 |
return WPTERR_GENERAL; |
k->keyid = k->ctx->subkeys->keyid+8; |
216 |
err = gpgme_get_key (ctx, keyid, r_key, secret); |
k->uid = k->ext->uids->uid; |
217 |
gpgme_release (ctx); |
k->allocated = pub? 0 : 1; |
218 |
return err? WPTERR_GENERAL : 0; |
return 0; |
219 |
} |
} |
220 |
|
|
221 |
|
|
222 |
/* Search the public key with @keyid as the keyid in the cache and |
gpgme_error_t |
223 |
return the item in @k. */ |
winpt_get_seckey (const char *keyid, winpt_key_s *k) |
224 |
int |
{ |
225 |
winpt_get_pubkey (const char *keyid, winpt_key_s *k) |
gpgme_error_t err; |
226 |
{ |
|
227 |
int rc; |
if (sec) |
228 |
|
err = get_key_from_cache (keyid, 1, &k->ctx, &k->ext); |
229 |
rc = get_key_from_cache (keyid, &k->ctx, &k->ext, 0); |
else |
230 |
if (rc) |
err = gpg_keycache_fetch_key (keyid, 1, &k->ctx, &k->ext); |
231 |
return rc; |
if (err) |
232 |
k->is_v3 = k->ctx->subkeys->pubkey_algo == GPGME_PK_RSA && |
return err; |
233 |
strlen (k->ctx->subkeys->fpr) == 32; |
k->is_v3 = k->ctx->subkeys->pubkey_algo == GPGME_PK_RSA && |
234 |
k->is_protected = k->ext->gloflags.is_protected; |
strlen (k->ctx->subkeys->fpr) == 32; |
235 |
k->keyid = k->ctx->subkeys->keyid; |
k->is_protected = k->ext->gloflags.is_protected; |
236 |
k->uid = k->ctx->uids->uid; |
k->keyid = k->ctx->subkeys->keyid+8; |
237 |
return rc; |
k->uid = k->ext->uids->uid; |
238 |
} |
k->allocated = sec? 0 : 1; |
239 |
|
return 0; |
240 |
|
} |
241 |
int |
|
242 |
winpt_get_seckey (const char *keyid, winpt_key_s *k) |
|
243 |
{ |
gpgme_error_t |
244 |
int rc; |
get_pubkey (const char *keyid, gpgme_key_t *ret_key) |
245 |
rc = get_key_from_cache (keyid, &k->ctx, &k->ext, 1); |
{ |
246 |
if (rc) |
return get_key_from_cache (keyid, 0, ret_key, NULL); |
247 |
return rc; |
} |
248 |
k->is_v3 = k->ctx->subkeys->pubkey_algo == GPGME_PK_RSA && |
|
249 |
strlen (k->ctx->subkeys->fpr) == 32; |
|
250 |
k->is_protected = k->ext->gloflags.is_protected; |
gpgme_error_t |
251 |
k->keyid = k->ctx->subkeys->keyid; |
get_seckey (const char *keyid, gpgme_key_t *ret_skey) |
252 |
k->uid = k->ctx->uids->uid; |
{ |
253 |
return rc; |
return get_key_from_cache (keyid, 1, ret_skey, NULL); |
254 |
} |
} |
255 |
|
|
256 |
|
|
257 |
int |
/* Map the signature summary in @sum to signature status table index. |
258 |
get_pubkey (const char *keyid, gpgme_key_t *ret_key) |
Return value: index to table. */ |
259 |
{ |
static int |
260 |
int rc; |
sigsum_to_index (gpgme_sigsum_t sum) |
261 |
|
{ |
262 |
if (pub && sec) |
if (sum & GPGME_SIGSUM_RED) |
263 |
rc = get_key_from_cache (keyid, ret_key, NULL, 0); |
return 2; |
264 |
else |
else if (sum & GPGME_SIGSUM_SIG_EXPIRED) |
265 |
rc = get_key_directly (keyid, ret_key, 0); |
return 8; |
266 |
return rc; |
else if (sum & GPGME_SIGSUM_KEY_REVOKED) |
267 |
} |
return 7; |
268 |
|
else if (sum & GPGME_SIGSUM_KEY_EXPIRED) |
269 |
|
return 6; |
270 |
int |
else if (sum & GPGME_SIGSUM_GREEN) |
271 |
get_seckey (const char *keyid, gpgme_key_t *ret_skey) |
return 1; |
272 |
{ |
else if (sum & GPGME_SIGSUM_KEY_MISSING) |
273 |
int rc; |
return 3; |
274 |
|
return 0; |
275 |
if (pub && sec) |
} |
276 |
rc = get_key_from_cache (keyid, ret_skey, NULL, 1); |
|
277 |
else |
|
278 |
rc = get_key_directly (keyid, ret_skey, 1); |
/* Return a humand readable description for the signature status @sum. |
279 |
return rc; |
Warning: this function does not consider the validity of the key. */ |
280 |
} |
const char* |
281 |
|
get_gpg_sigstat (gpgme_sigsum_t sum) |
282 |
|
{ |
283 |
/* Search for insecure ElGamal keys and return the |
const char *gpg_sigstat[] = { |
284 |
number of founded keys. */ |
_("Error during verification process"), |
285 |
int |
_("The signature is good"), |
286 |
count_insecure_elgkeys (void) |
_("The signature is BAD!"), |
287 |
{ |
_("The signature could not be checked due to a missing key"), |
288 |
gpg_keycache_t pc; |
_("No valid OpenPGP signature"), |
289 |
gpgme_key_t key; |
_("Signature Error"), |
290 |
int n=0; |
_("Good signature (Expired Key)"), |
291 |
|
_("Good signature (Revoked Key)"), |
292 |
pc = keycache_get_ctx (1); |
_("Good signature (Expired)"), |
293 |
if (!pc) |
NULL |
294 |
BUG (0); |
}; |
295 |
while (!gpg_keycache_next_key (pc, 0, &key)) { |
const unsigned int mask = 9; |
296 |
if (key->subkeys->pubkey_algo == GPGME_PK_ELG) |
|
297 |
n++; |
return gpg_sigstat[sigsum_to_index (sum) % mask]; |
298 |
} |
} |
299 |
gpg_keycache_rewind (pc); |
|
300 |
return n; |
|
301 |
} |
/* Return true if at least one secret key is available. */ |
302 |
|
bool |
303 |
|
secret_key_available (void) |
304 |
|
{ |
305 |
|
if (!sec || gpg_keycache_get_size (sec) == 0) |
306 |
|
return false; |
307 |
|
return true; |
308 |
|
} |