/[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 109 by twoaday, Fri Dec 2 07:32:13 2005 UTC revision 255 by twoaday, Tue Aug 1 16:37:23 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;
 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 81  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 105  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 121  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 142  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 155  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;  
 }  
   
   
 /* 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 329  const char* Line 275  const char*
275  get_gpg_sigstat (gpgme_sigsum_t sum)  get_gpg_sigstat (gpgme_sigsum_t sum)
276  {  {
277      const char *gpg_sigstat[] = {      const char *gpg_sigstat[] = {
278          _("Error during verification process."),          _("Error during verification process"),
279          _("The signature is good."),          _("The signature is good"),
280          _("The signature is BAD!"),          _("The signature is BAD!"),
281          _("The signature could not be checked due to a missing key."),          _("The signature could not be checked due to a missing key"),
282          _("No valid OpenPGP signature."),          _("No valid OpenPGP signature"),
283          _("Signature Error"),          _("Signature Error"),
284          _("Good Signature (Expired Key)"),          _("Good Signature (Expired Key)"),
285          _("Good Signature (Revoked Key)"),          _("Good Signature (Revoked Key)"),
# Line 343  get_gpg_sigstat (gpgme_sigsum_t sum) Line 289  get_gpg_sigstat (gpgme_sigsum_t sum)
289    
290      return gpg_sigstat[sigsum_to_index (sum) % mask];      return gpg_sigstat[sigsum_to_index (sum) % mask];
291  }  }
292    
293    
294    bool
295    secret_key_available (void)
296    {
297        if (!sec || gpg_keycache_get_size (sec) == 0)
298            return false;
299        return true;
300    }

Legend:
Removed from v.109  
changed lines
  Added in v.255

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26