/[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 2 by twoaday, Mon Jan 31 11:02:21 2005 UTC revision 36 by werner, Thu Oct 27 15:25:13 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_init( const char *pubring, const char * secring )      if (pub) {
79  {          gpg_keycache_release (pub);
80      struct progress_filter_s pfx;          pub = NULL;
81      gpgme_error_t err;      }
82      int val = 0;      if (sec) {
83      char * p;          gpg_keycache_release (sec);
84            sec = NULL;
85      if (reload) {      }
86          keycache_release ();      if (cleanup) {
87          reload = 0;          if (gpg_secring)
88      }              free (gpg_secring);
89      p = get_reg_entry (HKEY_CURRENT_USER, "Software\\WinPT", "nKeys");          gpg_secring = NULL;
90      if (p && *p != ' ')      }
91      {  }
92          val = atoi (p);  
93          free_if_alloc (p);  
94          memset (&pfx, 0, sizeof pfx);  /* Update the key with the keyid @keyid in the key cache.
95      }     If @is_sec is 1, the secret key cache is used. */
96    gpgme_error_t
97      err = gpgme_keycache_new (&pub);  keycache_update (int is_sec, const char *keyid)
98      if (err)  {
99          return err;      gpg_keycache_t ctx = pub;
100      if (val != 0)      gpgme_error_t err;
101          gpgme_keycache_set_cb (pub, progress_callback, &pfx, val);  
102      err = gpgme_keycache_new (&sec);      if (is_sec)
103      if (!err)          ctx = sec;
104          err = gpgme_keycache_init (pub, NULL, 0);      err = gpg_keycache_update_key (ctx, is_sec, pub, keyid);
105      if (!err)      if (is_sec)
106          err = gpgme_keycache_init( sec, NULL, 1 );          gpg_keycache_prepare_single (ctx, keyid, NULL, gpg_secring);
107      if( !err && pubring && *pubring )      return err;
108          err = gpgme_keycache_prepare( pub, pubring, NULL );  }
109      if( !err && secring && * secring )  
110          err = gpgme_keycache_prepare( sec, NULL, secring );  /* XXX: cache_keyring_names must be called then the GPG homedir changes! */
111      if (!err)  
112          gpgme_keycache_sync (pub, sec);  /* Initialize both cache contexts. Use @pubring for the public
113      if (val != 0)     keyring and @secring for the secret keyring. */
114          progress_cleanup (&pfx);  gpgme_error_t
115      return err;  keycache_init (const char *pubring, const char *secring)
116  } /* keycache_init */  {
117        struct progress_filter_s pfx;
118        gpgme_error_t err;
119  void      int val = 0;
120  keycache_set_reload( int yes )      char *p;
121  {  
122      reload = yes;      if (secring != NULL) {
123  } /* keycache_set_reload */          free_if_alloc (gpg_secring);
124            gpg_secring = get_gnupg_keyring (0, NO_STRICT);
125        }
126  int  
127  keycache_get_reload( void )      if (reload) {
128  {          keycache_release (0);
129      return reload;          reload = 0;
130  } /* keycache_get_reload */      }
131        p = get_reg_entry (HKEY_CURRENT_USER, "Software\\WinPT", "nKeys");
132        if (p && *p != ' ') {
133  gpgme_keycache_t          val = atoi (p);
134  keycache_get_ctx( int is_pub )          free_if_alloc (p);
135  {          memset (&pfx, 0, sizeof (pfx));
136      return is_pub? pub : sec;      }
137  } /* keycache_get_ctx */  
138        err = gpg_keycache_new (&pub);
139        if (err)
140  static int          return err;
141  get_key( const char *keyid, gpgme_key_t *r_key, int secret )      if (val != 0)
142  {          gpg_keycache_set_cb (pub, progress_callback, &pfx, val);
143      gpgme_keycache_t cache;      err = gpg_keycache_new (&sec);
144      gpgme_error_t err;      if (!err)
145      int mode = secret? KEYCACHE_PRV : KEYCACHE_PUB;          err = gpg_keycache_init (pub, NULL, 0);
146        if (!err)
147      if( !keyid )          err = gpg_keycache_init( sec, NULL, 1 );
148          return WPTERR_GENERAL;      if( !err && pubring && *pubring )
149      if( r_key )          err = gpg_keycache_prepare( pub, pubring, NULL );
150          *r_key = NULL;      if( !err && secring && * secring )
151      cache = keycache_get_ctx( mode );          err = gpg_keycache_prepare( sec, NULL, secring );
152      if( !cache )      if (!err)
153          BUG( NULL );          gpg_keycache_sync (pub, sec);
154      err = gpgme_keycache_find_key( cache, keyid, 0, r_key );      if (val != 0)
155      if( err )          progress_cleanup (&pfx);
156          return WPTERR_GENERAL;      return err;
157      return 0;  }
158  } /* get_key */  
159    
160    /* If @val = 1 indicate to reload the cache. */
161  static int  void
162  get_key2( const char * keyid, gpgme_key_t * r_key, int secret )  keycache_set_reload (int val)
163  {  {
164      gpg_iobuf_t inp;      reload = val;
165      char * p;  }
166      int rc;  
167    
168      p = get_gnupg_keyring (1, !NO_STRICT);  /* Return the reload cache flag. */
169      if( !p )  int
170          BUG( NULL );  keycache_get_reload (void)
171    {
172      inp = gpg_iobuf_open( p );      return reload;
173      if( !inp ) {  }
174          const char *s = winpt_strerror( WPTERR_FILE_OPEN );  
175          log_box( _("WinPT Error"), 0, "%s: %s", p, s );  
176          free_if_alloc( p );  /* Return the public cache context if @is_pub is set
177          return NULL;     the secre cache context otherwise. */
178      }  gpg_keycache_t
179      gpg_iobuf_ioctl( inp, 3, 1, NULL ); /* disable cache */  keycache_get_ctx (int is_pub)
180      rc = gpgme_getkey_bykeyid( inp, keyid, r_key );  {
181        return is_pub? pub : sec;
182      gpg_iobuf_close( inp );  }
183      free_if_alloc( p );  
184      return rc;  
185  } /* get_key2 */  /* Get the GPG key with keyid @keyid from the cache. Return it
186       in @r_key on success. */
187    static int
188  int  get_key_from_cache (const char *keyid, gpgme_key_t *r_key,
189  get_pubkey( const char *keyid, gpgme_key_t *ret_key )                      struct keycache_s **c, int secret)
190  {  {
191      int rc = 0;      gpg_keycache_t cache;
192        gpgme_error_t err;
193      if( pub && sec )      int mode = secret? KEYCACHE_PRV : KEYCACHE_PUB;
194          rc = get_key( keyid, ret_key, 0 );  
195      else      if (!keyid)
196          rc = get_key2( keyid, ret_key, 0 );          return WPTERR_GENERAL;
197      return rc;      if (r_key)
198  } /* get_pubkey */          *r_key = NULL;
199        cache = keycache_get_ctx (mode);
200        if (!cache)
201  int          BUG( NULL );
202  get_seckey( const char *keyid, gpgme_key_t *ret_skey )      if (!c)
203  {          err = gpg_keycache_find_key (cache, keyid, 0, r_key);
204      int rc = 0;      else
205            err = gpg_keycache_find_key2 (cache, keyid, 0, r_key, c);
206      if( pub && sec )      return err? WPTERR_GENERAL : 0;
207          rc = get_key( keyid, ret_skey, 1 );  }
208      else  
209          rc = get_key2( keyid, ret_skey, 1 );  
210      return rc;  /* Get GPG key with keyid @keyid directly from GPG and return
211  } /* get_seckey */     it in @r_key on success. */
212    static int
213    get_key_directly (const char *keyid, gpgme_key_t *r_key, int secret)
214  int  {
215  count_insecure_elgkeys (void)      gpgme_ctx_t ctx;
216  {      gpgme_error_t err;
217      gpgme_keycache_t pc;  
218      gpgme_key_t key;      err = gpgme_new (&ctx);
219      int n=0;      if (err)
220            return WPTERR_GENERAL;
221      pc = keycache_get_ctx (1);      err = gpgme_get_key (ctx, keyid, r_key, secret);
222      if (!pc)      gpgme_release (ctx);
223          BUG (0);      return err? WPTERR_GENERAL : 0;
224      while (!gpgme_keycache_next_key (pc, 0, &key)) {  }
225          if (gpgme_key_get_ulong_attr (key, GPGME_ATTR_ALGO, NULL, 0)  
226              == GPGME_PK_ELG_ES)  
227              n++;  /* Search the public key with @keyid as the keyid in the cache and
228      }     return the item in @k. */
229      gpgme_keycache_rewind (pc);  int
230      return n;  winpt_get_pubkey (const char *keyid, winpt_key_s *k)
231  }  {
232        int rc;
233        
234        rc = get_key_from_cache (keyid, &k->ctx, &k->ext, 0);
235        if (rc)
236            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.2  
changed lines
  Added in v.36

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26