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