/[winpt]/trunk/Src/wptGPGME.cpp
ViewVC logotype

Contents of /trunk/Src/wptGPGME.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 328 - (show annotations)
Fri Sep 25 16:07:38 2009 UTC (15 years, 5 months ago) by twoaday
File size: 7669 byte(s)


1 /* wptGPGME.cpp - WinPT GPGME interface
2 * Copyright (C) 2001-2006 Timo Schulz
3 *
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 #include "resource.h"
24 #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 BOOL CALLBACK keycache_dlg_proc (HWND dlg, UINT msg, WPARAM wparam,
38 LPARAM lparam);
39 void progress_cleanup (progress_filter_s * pfx);
40
41 /* Global GPG key cache contexts. */
42 static gpg_keycache_t pub = NULL;
43 static gpg_keycache_t sec = NULL;
44 static char *gpg_secring = NULL;
45
46 struct keycache_s *key_list = NULL;
47
48
49 /* Return 1 if no cache is available.
50 This can be the case if WinPT were run in command line mode. */
51 int
52 keycache_not_available (void)
53 {
54 return pub == NULL;
55 }
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 char tmpbuf[64];
63 int n = gpg_keycache_get_size (pub);
64
65 /* XXX: update the value when the cache has changed. */
66 _snprintf (tmpbuf, DIM (tmpbuf)-1, "%d", n);
67 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 if (cleanup)
78 safe_free (gpg_secring);
79 }
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 int val;
107 char *p;
108
109 if (secring != NULL) {
110 free_if_alloc (gpg_secring);
111 gpg_secring = get_gnupg_keyring (0, NO_STRICT);
112 log_debug ("keycache_init: secring path '%s'\r\n", gpg_secring);
113 }
114
115 val = 0;
116 p = get_reg_entry (HKEY_CURRENT_USER, "Software\\WinPT", "nKeys");
117 if (p && *p != ' ') {
118 val = atoi (p);
119 free_if_alloc (p);
120 }
121
122 memset (&pfx, 0, sizeof (pfx));
123 keycache_release (0); /* Release old contexts first. */
124
125 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 err = gpg_keycache_init (sec, NULL, 1);
135 if (!err && pubring && *pubring)
136 err = gpg_keycache_prepare (pub, pubring, NULL);
137 if (!err && secring && * secring)
138 err = gpg_keycache_prepare (sec, NULL, secring);
139 if (!err)
140 gpg_keycache_sync (pub, sec);
141 if (val != 0)
142 progress_cleanup (&pfx);
143
144 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 gpg_keycache_t ctx;
154
155 ctx = is_pub? pub : sec;
156 if (!ctx)
157 BUG (0);
158 return ctx;
159 }
160
161
162 /* Get the GPG key with keyid @keyid from the cache. Return it
163 in @r_key on success. */
164 static gpgme_error_t
165 get_key_from_cache (const char *keyid, int secret, gpgme_key_t *r_key,
166 struct keycache_s **c)
167 {
168 gpg_keycache_t cache;
169 gpgme_error_t err;
170 int mode = secret? KEYCACHE_PRV : KEYCACHE_PUB;
171
172 if (!keyid)
173 return gpg_error (GPG_ERR_INV_VALUE);
174 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 return err;
182 }
183
184
185 /* 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 {
190 /*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 }
197
198
199 /* Search the public key with @keyid as the keyid in the cache and
200 return the item in @k. */
201 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
206 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 if (err)
211 return err;
212 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 k->keyid = k->ctx->subkeys->keyid+8;
216 k->uid = k->ext->uids->uid;
217 k->allocated = pub? 0 : 1;
218 return 0;
219 }
220
221
222 gpgme_error_t
223 winpt_get_seckey (const char *keyid, winpt_key_s *k)
224 {
225 gpgme_error_t err;
226
227 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 if (err)
232 return err;
233 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 k->keyid = k->ctx->subkeys->keyid+8;
237 k->uid = k->ext->uids->uid;
238 k->allocated = sec? 0 : 1;
239 return 0;
240 }
241
242
243 gpgme_error_t
244 get_pubkey (const char *keyid, gpgme_key_t *ret_key)
245 {
246 return get_key_from_cache (keyid, 0, ret_key, NULL);
247 }
248
249
250 gpgme_error_t
251 get_seckey (const char *keyid, gpgme_key_t *ret_skey)
252 {
253 return get_key_from_cache (keyid, 1, ret_skey, NULL);
254 }
255
256
257 /* 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 if (sum & GPGME_SIGSUM_RED)
263 return 2;
264 else if (sum & GPGME_SIGSUM_SIG_EXPIRED)
265 return 8;
266 else if (sum & GPGME_SIGSUM_KEY_REVOKED)
267 return 7;
268 else if (sum & GPGME_SIGSUM_KEY_EXPIRED)
269 return 6;
270 else if (sum & GPGME_SIGSUM_GREEN)
271 return 1;
272 else if (sum & GPGME_SIGSUM_KEY_MISSING)
273 return 3;
274 return 0;
275 }
276
277
278 /* Return a humand readable description for the signature status @sum.
279 Warning: this function does not consider the validity of the key. */
280 const char*
281 get_gpg_sigstat (gpgme_sigsum_t sum)
282 {
283 const char *gpg_sigstat[] = {
284 _("Error during verification process"),
285 _("The signature is good"),
286 _("The signature is BAD!"),
287 _("The signature could not be checked due to a missing key"),
288 _("No valid OpenPGP signature"),
289 _("Signature Error"),
290 _("Good signature (Expired Key)"),
291 _("Good signature (Revoked Key)"),
292 _("Good signature (Expired)"),
293 NULL
294 };
295 const unsigned int mask = 9;
296
297 return gpg_sigstat[sigsum_to_index (sum) % mask];
298 }
299
300
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 }

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26