/[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 211 by twoaday, Sun May 7 12:36:48 2006 UTC revision 260 by twoaday, Wed Aug 16 10:01:30 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 47  static gpg_keycache_t sec = NULL; Line 47  static gpg_keycache_t sec = NULL;
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 125  keycache_init (const char *pubring, cons Line 117  keycache_init (const char *pubring, cons
117      }      }
118    
119      memset (&pfx, 0, sizeof (pfx));      memset (&pfx, 0, sizeof (pfx));
120      /* Release old contexts first. */      keycache_release (0); /* Release old contexts first. */
     keycache_release (0);  
121    
122      err = gpg_keycache_new (&pub);      err = gpg_keycache_new (&pub);
123      if (err)      if (err)
# Line 167  keycache_get_ctx (int is_pub) Line 158  keycache_get_ctx (int is_pub)
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 gpgme_error_t  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;
# Line 187  get_key_from_cache (const char *keyid, g Line 178  get_key_from_cache (const char *keyid, g
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 gpgme_error_t  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 err;      k->allocated = 0;
     err = gpgme_get_key (ctx, keyid, r_key, secret);  
     gpgme_release (ctx);  
     return err;  
192  }  }
193    
194    
# Line 209  get_key_directly (const char *keyid, gpg Line 197  get_key_directly (const char *keyid, gpg
197  gpgme_error_t  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      gpgme_error_t err;      gpgme_error_t err = gpg_error (GPG_ERR_NO_ERROR);
201            
202      err = get_key_from_cache (keyid, &k->ctx, &k->ext, 0);      if (pub)
203            err = get_key_from_cache (keyid, 0, &k->ctx, &k->ext);
204        else
205            err = gpg_keycache_fetch_key (keyid, 0, &k->ctx, &k->ext);
206      if (err)      if (err)
207          return err;          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        k->allocated = pub? 0 : 1;
214      return 0;      return 0;
215  }  }
216    
# Line 228  winpt_get_seckey (const char *keyid, win Line 220  winpt_get_seckey (const char *keyid, win
220  {  {
221      gpgme_error_t err;      gpgme_error_t err;
222    
223      err = get_key_from_cache (keyid, &k->ctx, &k->ext, 1);      if (sec)
224            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)      if (err)
228          return err;          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        k->allocated = sec? 0 : 1;
235      return 0;      return 0;
236  }  }
237    
# Line 243  winpt_get_seckey (const char *keyid, win Line 239  winpt_get_seckey (const char *keyid, win
239  gpgme_error_t  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      gpgme_error_t 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  gpgme_error_t  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      gpgme_error_t 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;  
 }  
   
   
 /* Search for insecure ElGamal keys and return the  
    number of founded keys. */  
 int  
 count_insecure_elgkeys (void)  
 {  
     gpg_keycache_t pc;  
     gpgme_key_t key;  
     int n=0;  
   
     pc = keycache_get_ctx (1);  
     while (!gpg_keycache_next_key (pc, 0, &key)) {  
         if (key->subkeys->pubkey_algo == GPGME_PK_ELG)  
             n++;  
     }  
     gpg_keycache_rewind (pc);  
     return n;  
250  }  }
251    
252    
# Line 291  count_insecure_elgkeys (void) Line 256  count_insecure_elgkeys (void)
256  static int  static int
257  sigsum_to_index (gpgme_sigsum_t sum)  sigsum_to_index (gpgme_sigsum_t sum)
258  {  {
259        /* xxx: gpgme should set _valid if the sig is good but the
260                key has been expired; check the code */
261        if ((sum & GPGME_SIGSUM_VALID) && (sum & GPGME_SIGSUM_SIG_EXPIRED))
262            return 8;
263      if ((sum & GPGME_SIGSUM_VALID) && (sum & GPGME_SIGSUM_KEY_REVOKED))      if ((sum & GPGME_SIGSUM_VALID) && (sum & GPGME_SIGSUM_KEY_REVOKED))
264          return 7;          return 7;
265      if ((sum & GPGME_SIGSUM_VALID) && (sum & GPGME_SIGSUM_SIG_EXPIRED))      if ((sum & GPGME_SIGSUM_VALID) && (sum & GPGME_SIGSUM_KEY_EXPIRED))
266          return 6;          return 6;
267      if (sum & GPGME_SIGSUM_GREEN)      if (sum & GPGME_SIGSUM_GREEN)
268          return 1;          return 1;
# Line 310  const char* Line 279  const char*
279  get_gpg_sigstat (gpgme_sigsum_t sum)  get_gpg_sigstat (gpgme_sigsum_t sum)
280  {  {
281      const char *gpg_sigstat[] = {      const char *gpg_sigstat[] = {
282          _("Error during verification process."),          _("Error during verification process"),
283          _("The signature is good."),          _("The signature is good"),
284          _("The signature is BAD!"),          _("The signature is BAD!"),
285          _("The signature could not be checked due to a missing key."),          _("The signature could not be checked due to a missing key"),
286          _("No valid OpenPGP signature."),          _("No valid OpenPGP signature"),
287          _("Signature Error"),          _("Signature Error"),
288          _("Good Signature (Expired Key)"),          _("Good signature (Expired Key)"),
289          _("Good Signature (Revoked Key)"),          _("Good signature (Revoked Key)"),
290            _("Good signature (Expired)"),
291          NULL          NULL
292      };      };
293      const unsigned int mask = 8;      const unsigned int mask = 9;
294    
295      return gpg_sigstat[sigsum_to_index (sum) % mask];      return gpg_sigstat[sigsum_to_index (sum) % mask];
296  }  }
297    
298    
299  /* Check if the secret keyring contains at least one  /* Return true if at least one secret key is available. */
300     key with ultimate trust.  bool
301     Return value: 0 on success. */  secret_key_available (void)
302  int  {
303  check_ultimate_trusted_key (void)      if (!sec || gpg_keycache_get_size (sec) == 0)
304  {          return false;
305      struct keycache_s *n;      return true;
   
     for (n = sec->item; n; n = n->next) {  
         if (n->pubpart &&  
             n->pubpart->key->owner_trust == GPGME_VALIDITY_ULTIMATE)  
         return 0;  
     }  
     return -1;  
306  }  }

Legend:
Removed from v.211  
changed lines
  Added in v.260

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26