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