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

Annotation of /trunk/Src/wptGPGME.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 23 - (hide annotations)
Fri Sep 30 10:10:16 2005 UTC (19 years, 5 months ago) by twoaday
File size: 6898 byte(s)
Almost finished phase 1 of the WinPT GPGME port.
Still need more cleanup, comments and tests.


1 twoaday 2 /* wptGPGME.cpp - WinPT GPGME interface
2 twoaday 22 * Copyright (C) 2001-2005 Timo Schulz
3 twoaday 2 *
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     * You should have received a copy of the GNU General Public License
17     * along with WinPT; if not, write to the Free Software Foundation,
18     * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19     */
20    
21     #include <sys/types.h>
22     #include <windows.h>
23    
24     #include "../resource.h"
25     #include "wptNLS.h"
26     #include "wptGPG.h"
27     #include "wptErrors.h"
28     #include "wptTypes.h"
29     #include "wptW32API.h"
30     #include "wptVersion.h"
31     #include "wptCommonCtl.h"
32     #include "wptContext.h"
33     #include "wptRegistry.h"
34     #include "wptDlgs.h"
35    
36     #include "openpgp.h"
37    
38     BOOL CALLBACK keycache_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam);
39     void progress_cleanup (progress_filter_s * pfx);
40    
41     static gpgme_keycache_t pub = NULL;
42     static gpgme_keycache_t sec = NULL;
43     static unsigned int reload = 0;
44 twoaday 22 static char *gpg_secring = NULL;
45 twoaday 2
46     void
47     keycache_reload (HWND dlg)
48     {
49 twoaday 22 refresh_cache_s rcs;
50 twoaday 2
51 twoaday 22 memset (&rcs, 0, sizeof rcs);
52 twoaday 2 rcs.kr_reload = rcs.kr_update = 1;
53 twoaday 22 rcs.tr_update = 0;
54     DialogBoxParam (glob_hinst, (LPCSTR)IDD_WINPT_KEYCACHE, dlg,
55     keycache_dlg_proc, (LPARAM)&rcs);
56 twoaday 2 } /* keycache_reload */
57    
58     void
59     keycache_release (void)
60     {
61     int n = gpgme_keycache_count (pub);
62     char tmpbuf[64];
63    
64 twoaday 22 /* XXX: update the value when the cache has changed. */
65 twoaday 2 sprintf (tmpbuf, "%d", n);
66     set_reg_key (HKEY_CURRENT_USER, "Software\\WinPT", "nKeys", tmpbuf);
67    
68     if (pub) {
69 twoaday 22 gpgme_keycache_release (pub);
70 twoaday 2 pub = NULL;
71     }
72     if (sec) {
73 twoaday 22 gpgme_keycache_release (sec);
74 twoaday 2 sec = NULL;
75     }
76     } /* keycache_release */
77    
78    
79     gpgme_error_t
80 twoaday 20 keycache_update (int is_sec, const char *keyid)
81 twoaday 2 {
82 twoaday 20 gpgme_keycache_t ctx = pub;
83 twoaday 22 gpgme_error_t err;
84    
85 twoaday 20 if (is_sec)
86     ctx = sec;
87 twoaday 22 err = gpgme_keycache_update_key (ctx, is_sec, pub, keyid);
88     if (is_sec)
89     gpgme_keycache_prepare_single (ctx, keyid, NULL, gpg_secring);
90     return err;
91 twoaday 19 }
92    
93 twoaday 22 /* XXX: cache_keyring_names must be called then the GPG homedir changes! */
94 twoaday 19
95     gpgme_error_t
96     keycache_init (const char *pubring, const char * secring)
97     {
98 twoaday 2 struct progress_filter_s pfx;
99     gpgme_error_t err;
100     int val = 0;
101     char * p;
102    
103 twoaday 22 if (secring != NULL) {
104     free_if_alloc (gpg_secring);
105     gpg_secring = get_gnupg_keyring (0, NO_STRICT);
106     }
107    
108 twoaday 2 if (reload) {
109     keycache_release ();
110     reload = 0;
111     }
112     p = get_reg_entry (HKEY_CURRENT_USER, "Software\\WinPT", "nKeys");
113 twoaday 19 if (p && *p != ' ') {
114 twoaday 2 val = atoi (p);
115     free_if_alloc (p);
116 twoaday 22 memset (&pfx, 0, sizeof (pfx));
117 twoaday 2 }
118    
119     err = gpgme_keycache_new (&pub);
120     if (err)
121     return err;
122     if (val != 0)
123     gpgme_keycache_set_cb (pub, progress_callback, &pfx, val);
124     err = gpgme_keycache_new (&sec);
125     if (!err)
126     err = gpgme_keycache_init (pub, NULL, 0);
127     if (!err)
128     err = gpgme_keycache_init( sec, NULL, 1 );
129     if( !err && pubring && *pubring )
130     err = gpgme_keycache_prepare( pub, pubring, NULL );
131     if( !err && secring && * secring )
132     err = gpgme_keycache_prepare( sec, NULL, secring );
133     if (!err)
134     gpgme_keycache_sync (pub, sec);
135     if (val != 0)
136     progress_cleanup (&pfx);
137     return err;
138     } /* keycache_init */
139    
140    
141     void
142     keycache_set_reload( int yes )
143     {
144     reload = yes;
145     } /* keycache_set_reload */
146    
147    
148     int
149     keycache_get_reload( void )
150     {
151     return reload;
152     } /* keycache_get_reload */
153    
154    
155     gpgme_keycache_t
156 twoaday 22 keycache_get_ctx (int is_pub)
157 twoaday 2 {
158     return is_pub? pub : sec;
159     } /* keycache_get_ctx */
160    
161    
162     static int
163 twoaday 23 get_key (const char *keyid, gpgme_key_t *r_key, struct keycache_s **c, int secret)
164 twoaday 2 {
165     gpgme_keycache_t cache;
166     gpgme_error_t err;
167     int mode = secret? KEYCACHE_PRV : KEYCACHE_PUB;
168    
169     if( !keyid )
170     return WPTERR_GENERAL;
171     if( r_key )
172     *r_key = NULL;
173 twoaday 23 cache = keycache_get_ctx (mode);
174 twoaday 2 if( !cache )
175     BUG( NULL );
176 twoaday 23 if (!c)
177     err = gpgme_keycache_find_key( cache, keyid, 0, r_key );
178     else
179     err = gpgme_keycache_find_key2 (cache, keyid, 0, r_key, c);
180 twoaday 2 if( err )
181     return WPTERR_GENERAL;
182     return 0;
183     } /* get_key */
184    
185    
186     static int
187     get_key2( const char * keyid, gpgme_key_t * r_key, int secret )
188     {
189     gpg_iobuf_t inp;
190     char * p;
191     int rc;
192    
193     p = get_gnupg_keyring (1, !NO_STRICT);
194     if( !p )
195     BUG( NULL );
196    
197     inp = gpg_iobuf_open( p );
198     if( !inp ) {
199     const char *s = winpt_strerror( WPTERR_FILE_OPEN );
200     log_box( _("WinPT Error"), 0, "%s: %s", p, s );
201     free_if_alloc( p );
202     return NULL;
203     }
204     gpg_iobuf_ioctl( inp, 3, 1, NULL ); /* disable cache */
205     rc = gpgme_getkey_bykeyid( inp, keyid, r_key );
206    
207     gpg_iobuf_close( inp );
208     free_if_alloc( p );
209     return rc;
210     } /* get_key2 */
211    
212    
213     int
214 twoaday 23 winpt_get_pubkey (const char *keyid, winpt_key_s *k)
215     {
216     int rc;
217    
218     rc = get_key (keyid, &k->ctx, &k->ext, 0);
219     if (rc)
220     return rc;
221     k->is_v3 = k->ctx->subkeys->pubkey_algo == GPGME_PK_RSA && strlen (k->ctx->subkeys->fpr) == 32;
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;
226     }
227    
228    
229     int
230     winpt_get_seckey (const char *keyid, winpt_key_s *k)
231     {
232     int rc;
233     rc = get_key (keyid, &k->ctx, &k->ext, 1);
234     if (rc)
235     return rc;
236     k->is_v3 = k->ctx->subkeys->pubkey_algo == GPGME_PK_RSA && strlen (k->ctx->subkeys->fpr) == 32;
237     k->is_protected = k->ext->gloflags.is_protected;
238     k->keyid = k->ctx->subkeys->keyid;
239     k->uid = k->ctx->uids->uid;
240     return rc;
241     }
242    
243    
244     int
245 twoaday 22 get_pubkey (const char *keyid, gpgme_key_t *ret_key)
246 twoaday 2 {
247     int rc = 0;
248    
249 twoaday 22 if (pub && sec)
250 twoaday 23 rc = get_key (keyid, ret_key, NULL, 0);
251 twoaday 2 else
252 twoaday 22 rc = get_key2 (keyid, ret_key, 0);
253 twoaday 2 return rc;
254     } /* get_pubkey */
255    
256    
257     int
258     get_seckey( const char *keyid, gpgme_key_t *ret_skey )
259     {
260     int rc = 0;
261    
262     if( pub && sec )
263 twoaday 23 rc = get_key( keyid, ret_skey, NULL, 1 );
264 twoaday 2 else
265     rc = get_key2( keyid, ret_skey, 1 );
266     return rc;
267     } /* get_seckey */
268    
269    
270     int
271     count_insecure_elgkeys (void)
272     {
273     gpgme_keycache_t pc;
274     gpgme_key_t key;
275     int n=0;
276    
277     pc = keycache_get_ctx (1);
278     if (!pc)
279     BUG (0);
280     while (!gpgme_keycache_next_key (pc, 0, &key)) {
281 twoaday 23 if (key->subkeys->pubkey_algo == GPGME_PK_ELG)
282 twoaday 2 n++;
283     }
284     gpgme_keycache_rewind (pc);
285     return n;
286     }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26