/[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 36 by werner, Thu Oct 27 15:25:13 2005 UTC revision 262 by twoaday, Sat Sep 30 10:24:34 2006 UTC
# Line 1  Line 1 
1  /* wptGPGME.cpp - WinPT GPGME interface  /* wptGPGME.cpp - WinPT GPGME interface
2   *      Copyright (C) 2001-2005 Timo Schulz   *      Copyright (C) 2001-2006 Timo Schulz
3   *   *
4   * This file is part of WinPT.   * This file is part of WinPT.
5   *   *
# Line 17  Line 17 
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  #ifdef HAVE_CONFIG_H  #ifdef HAVE_CONFIG_H
21  #include <config.h>  #include <config.h>
22  #endif  #endif
23    
24  #include <sys/types.h>  #include <sys/types.h>
 #include <sys/types.h>  
25  #include <windows.h>  #include <windows.h>
26    
27  #include "../resource.h"  #include "resource.h"
28  #include "wptNLS.h"  #include "wptNLS.h"
29  #include "wptGPG.h"  #include "wptGPG.h"
30  #include "wptErrors.h"  #include "wptErrors.h"
# Line 43  Line 41 
41  BOOL CALLBACK  keycache_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam);  BOOL CALLBACK  keycache_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam);
42  void progress_cleanup (progress_filter_s * pfx);  void progress_cleanup (progress_filter_s * pfx);
43    
44    /* Global GPG key cache contexts. */
45  static gpg_keycache_t pub = NULL;  static gpg_keycache_t pub = NULL;
46  static gpg_keycache_t sec = NULL;  static gpg_keycache_t sec = NULL;
 static unsigned int reload = 0;  
47  static char *gpg_secring = NULL;  static char *gpg_secring = NULL;
48    
49    
50  /* Reload the key cache. */  int
51  void  keycache_not_available (void)
52  keycache_reload (HWND dlg)  {
53  {            return pub == NULL;
     refresh_cache_s rcs;  
   
     memset (&rcs, 0, sizeof rcs);  
     rcs.kr_reload = rcs.kr_update = 1;  
     rcs.tr_update = 0;  
     DialogBoxParam (glob_hinst, (LPCSTR)IDD_WINPT_KEYCACHE, dlg,                                          
                     keycache_dlg_proc, (LPARAM)&rcs);  
54  }  }
55    
   
56  /* Release both key cache objects. If @cleanup is 1,  /* Release both key cache objects. If @cleanup is 1,
57     also release other global structs. */     also release other global structs. */
58  void  void
59  keycache_release (int cleanup)  keycache_release (int cleanup)
60  {  {
     int n = gpg_keycache_get_size (pub);  
61      char tmpbuf[64];      char tmpbuf[64];
62        int n = gpg_keycache_get_size (pub);
63    
64      /* XXX: update the value when the cache has changed. */      /* XXX: update the value when the cache has changed. */
65      sprintf (tmpbuf, "%d", n);      sprintf (tmpbuf, "%d", n);
# Line 83  keycache_release (int cleanup) Line 73  keycache_release (int cleanup)
73          gpg_keycache_release (sec);          gpg_keycache_release (sec);
74          sec = NULL;          sec = NULL;
75      }      }
76      if (cleanup) {      if (cleanup)
77          if (gpg_secring)          safe_free (gpg_secring);
             free (gpg_secring);  
         gpg_secring = NULL;  
     }  
78  }  }
79    
80    
# Line 107  keycache_update (int is_sec, const char Line 94  keycache_update (int is_sec, const char
94      return err;      return err;
95  }  }
96    
 /* XXX: cache_keyring_names must be called then the GPG homedir changes! */  
97    
98  /* Initialize both cache contexts. Use @pubring for the public  /* Initialize both cache contexts. Use @pubring for the public
99     keyring and @secring for the secret keyring. */     keyring and @secring for the secret keyring. */
# Line 123  keycache_init (const char *pubring, cons Line 109  keycache_init (const char *pubring, cons
109          free_if_alloc (gpg_secring);          free_if_alloc (gpg_secring);
110          gpg_secring = get_gnupg_keyring (0, NO_STRICT);          gpg_secring = get_gnupg_keyring (0, NO_STRICT);
111      }      }
112        
     if (reload) {  
         keycache_release (0);  
         reload = 0;  
     }  
113      p = get_reg_entry (HKEY_CURRENT_USER, "Software\\WinPT", "nKeys");      p = get_reg_entry (HKEY_CURRENT_USER, "Software\\WinPT", "nKeys");
114      if (p && *p != ' ') {      if (p && *p != ' ') {
115          val = atoi (p);          val = atoi (p);
116          free_if_alloc (p);          free_if_alloc (p);      
         memset (&pfx, 0, sizeof (pfx));  
117      }      }
118    
119        memset (&pfx, 0, sizeof (pfx));
120        keycache_release (0); /* Release old contexts first. */
121    
122      err = gpg_keycache_new (&pub);      err = gpg_keycache_new (&pub);
123      if (err)      if (err)
124          return err;          return err;
# Line 144  keycache_init (const char *pubring, cons Line 128  keycache_init (const char *pubring, cons
128      if (!err)      if (!err)
129          err = gpg_keycache_init (pub, NULL, 0);          err = gpg_keycache_init (pub, NULL, 0);
130      if (!err)      if (!err)
131          err = gpg_keycache_init( sec, NULL, 1 );          err = gpg_keycache_init (sec, NULL, 1);
132      if( !err && pubring && *pubring )      if (!err && pubring && *pubring)
133          err = gpg_keycache_prepare( pub, pubring, NULL );          err = gpg_keycache_prepare (pub, pubring, NULL);
134      if( !err && secring && * secring )      if (!err && secring && * secring)
135          err = gpg_keycache_prepare( sec, NULL, secring );          err = gpg_keycache_prepare (sec, NULL, secring);
136      if (!err)      if (!err)
137          gpg_keycache_sync (pub, sec);          gpg_keycache_sync (pub, sec);
138      if (val != 0)      if (val != 0)
# Line 157  keycache_init (const char *pubring, cons Line 141  keycache_init (const char *pubring, cons
141  }  }
142    
143    
 /* If @val = 1 indicate to reload the cache. */  
 void  
 keycache_set_reload (int val)  
 {  
     reload = val;  
 }  
   
   
 /* Return the reload cache flag. */  
 int  
 keycache_get_reload (void)  
 {  
     return reload;  
 }  
   
   
144  /* Return the public cache context if @is_pub is set  /* Return the public cache context if @is_pub is set
145     the secre cache context otherwise. */     the secre cache context otherwise. */
146  gpg_keycache_t  gpg_keycache_t
147  keycache_get_ctx (int is_pub)  keycache_get_ctx (int is_pub)
148  {  {
149      return is_pub? pub : sec;      gpg_keycache_t ctx;
150    
151        ctx = is_pub? pub : sec;
152        if (!ctx)
153            BUG (0);
154        return ctx;
155  }  }
156    
157    
158  /* Get the GPG key with keyid @keyid from the cache. Return it  /* Get the GPG key with keyid @keyid from the cache. Return it
159     in @r_key on success. */     in @r_key on success. */
160  static int  static gpgme_error_t
161  get_key_from_cache (const char *keyid, gpgme_key_t *r_key,  get_key_from_cache (const char *keyid, int secret, gpgme_key_t *r_key,
162                      struct keycache_s **c, int secret)                      struct keycache_s **c)
163  {  {
164      gpg_keycache_t cache;      gpg_keycache_t cache;
165      gpgme_error_t err;      gpgme_error_t err;
166      int mode = secret? KEYCACHE_PRV : KEYCACHE_PUB;      int mode = secret? KEYCACHE_PRV : KEYCACHE_PUB;
167    
168      if (!keyid)      if (!keyid)
169          return WPTERR_GENERAL;          return gpg_error (GPG_ERR_INV_VALUE);
170      if (r_key)      if (r_key)
171          *r_key = NULL;          *r_key = NULL;
172      cache = keycache_get_ctx (mode);      cache = keycache_get_ctx (mode);
     if (!cache)  
         BUG( NULL );  
173      if (!c)      if (!c)
174          err = gpg_keycache_find_key (cache, keyid, 0, r_key);          err = gpg_keycache_find_key (cache, keyid, 0, r_key);
175      else      else
176          err = gpg_keycache_find_key2 (cache, keyid, 0, r_key, c);          err = gpg_keycache_find_key2 (cache, keyid, 0, r_key, c);
177      return err? WPTERR_GENERAL : 0;      return err;
178  }  }
179    
180    
181  /* Get GPG key with keyid @keyid directly from GPG and return  /* Release the internal key structure.
182     it in @r_key on success. */     If allocated is 0, assume fixed cache item. */
183  static int  void
184  get_key_directly (const char *keyid, gpgme_key_t *r_key, int secret)  winpt_release_pubkey (winpt_key_s *k)
185  {  {
186      gpgme_ctx_t ctx;      /*log_box ("debug", 0, "alloc %d", k->allocated);*/
187      gpgme_error_t err;      if (!k->allocated)
188            return;
189      err = gpgme_new (&ctx);      gpg_keycache_item_release (k->ext);
190      if (err)      k->ext = NULL;
191          return WPTERR_GENERAL;      k->allocated = 0;
     err = gpgme_get_key (ctx, keyid, r_key, secret);  
     gpgme_release (ctx);  
     return err? WPTERR_GENERAL : 0;  
192  }  }
193    
194    
195  /* Search the public key with @keyid as the keyid in the cache and  /* Search the public key with @keyid as the keyid in the cache and
196     return the item in @k. */     return the item in @k. */
197  int  gpgme_error_t
198  winpt_get_pubkey (const char *keyid, winpt_key_s *k)  winpt_get_pubkey (const char *keyid, winpt_key_s *k)
199  {  {
200      int rc;      gpgme_error_t err = gpg_error (GPG_ERR_NO_ERROR);
201            
202      rc = get_key_from_cache (keyid, &k->ctx, &k->ext, 0);      if (pub)
203      if (rc)          err = get_key_from_cache (keyid, 0, &k->ctx, &k->ext);
204          return rc;      else
205            err = gpg_keycache_fetch_key (keyid, 0, &k->ctx, &k->ext);
206        if (err)
207            return err;
208      k->is_v3 = k->ctx->subkeys->pubkey_algo == GPGME_PK_RSA &&      k->is_v3 = k->ctx->subkeys->pubkey_algo == GPGME_PK_RSA &&
209                  strlen (k->ctx->subkeys->fpr) == 32;                  strlen (k->ctx->subkeys->fpr) == 32;
210      k->is_protected = k->ext->gloflags.is_protected;      k->is_protected = k->ext->gloflags.is_protected;
211      k->keyid = k->ctx->subkeys->keyid;      k->keyid = k->ctx->subkeys->keyid+8;
212      k->uid = k->ctx->uids->uid;      k->uid = k->ext->uids->uid;
213      return rc;      k->allocated = pub? 0 : 1;
214        return 0;
215  }  }
216    
217    
218  int  gpgme_error_t
219  winpt_get_seckey (const char *keyid, winpt_key_s *k)  winpt_get_seckey (const char *keyid, winpt_key_s *k)
220  {  {
221      int rc;      gpgme_error_t err;
222      rc = get_key_from_cache (keyid, &k->ctx, &k->ext, 1);  
223      if (rc)      if (sec)
224          return rc;          err = get_key_from_cache (keyid, 1, &k->ctx, &k->ext);
225        else
226            err = gpg_keycache_fetch_key (keyid, 1, &k->ctx, &k->ext);
227        if (err)
228            return err;
229      k->is_v3 = k->ctx->subkeys->pubkey_algo == GPGME_PK_RSA &&      k->is_v3 = k->ctx->subkeys->pubkey_algo == GPGME_PK_RSA &&
230                  strlen (k->ctx->subkeys->fpr) == 32;                  strlen (k->ctx->subkeys->fpr) == 32;
231      k->is_protected = k->ext->gloflags.is_protected;      k->is_protected = k->ext->gloflags.is_protected;
232      k->keyid = k->ctx->subkeys->keyid;      k->keyid = k->ctx->subkeys->keyid+8;
233      k->uid = k->ctx->uids->uid;      k->uid = k->ext->uids->uid;
234      return rc;      k->allocated = sec? 0 : 1;
235        return 0;
236  }  }
237    
238    
239  int  gpgme_error_t
240  get_pubkey (const char *keyid, gpgme_key_t *ret_key)  get_pubkey (const char *keyid, gpgme_key_t *ret_key)
241  {  {
242      int rc;      return get_key_from_cache (keyid, 0, ret_key, NULL);
   
     if (pub && sec)  
         rc = get_key_from_cache (keyid, ret_key, NULL, 0);  
     else  
         rc = get_key_directly (keyid, ret_key, 0);  
     return rc;  
243  }  }
244    
245    
246  int  gpgme_error_t
247  get_seckey (const char *keyid, gpgme_key_t *ret_skey)  get_seckey (const char *keyid, gpgme_key_t *ret_skey)
248  {  {
249      int rc;      return get_key_from_cache (keyid, 1, ret_skey, NULL);
   
     if (pub && sec)  
         rc = get_key_from_cache (keyid, ret_skey, NULL, 1);  
     else  
         rc = get_key_directly (keyid, ret_skey, 1);  
     return rc;  
250  }  }
251    
252    
253  /* Search for insecure ElGamal keys and return the  
254     number of founded keys. */  /* Map the signature summary in @sum to signature status table index.
255  int     Return value: index to table. */
256  count_insecure_elgkeys (void)  static int
257    sigsum_to_index (gpgme_sigsum_t sum)
258  {  {
259      gpg_keycache_t pc;      if (sum & GPGME_SIGSUM_RED)
260      gpgme_key_t key;          return 2;
261      int n=0;      else if (sum & GPGME_SIGSUM_SIG_EXPIRED)
262            return 8;
263        else if (sum & GPGME_SIGSUM_KEY_REVOKED)
264            return 7;
265        else if (sum & GPGME_SIGSUM_KEY_EXPIRED)
266            return 6;
267        else if (sum & GPGME_SIGSUM_GREEN)
268            return 1;
269        else if (sum & GPGME_SIGSUM_KEY_MISSING)
270            return 3;
271        return 0;
272    }
273    
274      pc = keycache_get_ctx (1);  
275      if (!pc)  /* Return a humand readable description for the signature status @sum.
276          BUG (0);     Warning: this function does not consider the validity of the key. */
277      while (!gpg_keycache_next_key (pc, 0, &key)) {  const char*
278          if (key->subkeys->pubkey_algo == GPGME_PK_ELG)  get_gpg_sigstat (gpgme_sigsum_t sum)
279              n++;  {
280      }      const char *gpg_sigstat[] = {
281      gpg_keycache_rewind (pc);          _("Error during verification process"),
282      return n;          _("The signature is good"),
283            _("The signature is BAD!"),
284            _("The signature could not be checked due to a missing key"),
285            _("No valid OpenPGP signature"),
286            _("Signature Error"),
287            _("Good signature (Expired Key)"),
288            _("Good signature (Revoked Key)"),
289            _("Good signature (Expired)"),
290            NULL
291        };
292        const unsigned int mask = 9;
293    
294        return gpg_sigstat[sigsum_to_index (sum) % mask];
295    }
296    
297    
298    /* Return true if at least one secret key is available. */
299    bool
300    secret_key_available (void)
301    {
302        if (!sec || gpg_keycache_get_size (sec) == 0)
303            return false;
304        return true;
305  }  }

Legend:
Removed from v.36  
changed lines
  Added in v.262

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26