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

Diff of /trunk/Src/wptGPGME.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

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

Legend:
Removed from v.19  
changed lines
  Added in v.47

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26