/[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 208 by twoaday, Mon May 1 12:22:18 2006 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 41  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;
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 80  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 127  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 168  keycache_get_ctx (int is_pub) Line 157  keycache_get_ctx (int is_pub)
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 (0);  
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;  
 }  
   
   
 /* 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);  
     if (!pc)  
         BUG (0);  
     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 296  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      if ((sum & GPGME_SIGSUM_VALID) && (sum & GPGME_SIGSUM_KEY_REVOKED))      if (sum & GPGME_SIGSUM_RED)
260            return 2;
261        else if (sum & GPGME_SIGSUM_SIG_EXPIRED)
262            return 8;
263        else if (sum & GPGME_SIGSUM_KEY_REVOKED)
264          return 7;          return 7;
265      if ((sum & GPGME_SIGSUM_VALID) && (sum & GPGME_SIGSUM_SIG_EXPIRED))      else if (sum & GPGME_SIGSUM_KEY_EXPIRED)
266          return 6;          return 6;
267      if (sum & GPGME_SIGSUM_GREEN)      else if (sum & GPGME_SIGSUM_GREEN)
268          return 1;          return 1;
     else if (sum & GPGME_SIGSUM_RED)  
         return 2;  
269      else if (sum & GPGME_SIGSUM_KEY_MISSING)      else if (sum & GPGME_SIGSUM_KEY_MISSING)
270          return 3;          return 3;
271      return 0;      return 0;
272  }  }
273    
274    
275  /* Return a humand readable description for the signature status @sum. */  /* Return a humand readable description for the signature status @sum.
276       Warning: this function does not consider the validity of the key. */
277  const char*  const char*
278  get_gpg_sigstat (gpgme_sigsum_t sum)  get_gpg_sigstat (gpgme_sigsum_t sum)
279  {  {
280      const char *gpg_sigstat[] = {      const char *gpg_sigstat[] = {
281          _("Error during verification process."),          _("Error during verification process"),
282          _("The signature is good."),          _("The signature is good"),
283          _("The signature is BAD!"),          _("The signature is BAD!"),
284          _("The signature could not be checked due to a missing key."),          _("The signature could not be checked due to a missing key"),
285          _("No valid OpenPGP signature."),          _("No valid OpenPGP signature"),
286          _("Signature Error"),          _("Signature Error"),
287          _("Good Signature (Expired Key)"),          _("Good signature (Expired Key)"),
288          _("Good Signature (Revoked Key)"),          _("Good signature (Revoked Key)"),
289            _("Good signature (Expired)"),
290          NULL          NULL
291      };      };
292      const unsigned int mask = 8;      const unsigned int mask = 9;
293    
294      return gpg_sigstat[sigsum_to_index (sum) % mask];      return gpg_sigstat[sigsum_to_index (sum) % mask];
295  }  }
296    
297    
298  /* Check if the secret keyring contains at least one  /* Return true if at least one secret key is available. */
299     key with ultimate trust.  bool
300     Return value: 0 on success. */  secret_key_available (void)
301  int  {
302  check_ultimate_trusted_key (void)      if (!sec || gpg_keycache_get_size (sec) == 0)
303  {          return false;
304      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;  
305  }  }

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26