/[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 22 by twoaday, Wed Aug 10 11:33:35 2005 UTC revision 25 by twoaday, Wed Oct 12 10:04:26 2005 UTC
# Line 38  Line 38 
38  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);
39  void progress_cleanup (progress_filter_s * pfx);  void progress_cleanup (progress_filter_s * pfx);
40    
41  static gpgme_keycache_t pub = NULL;  static gpg_keycache_t pub = NULL;
42  static gpgme_keycache_t sec = NULL;  static gpg_keycache_t sec = NULL;
43  static unsigned int reload = 0;  static unsigned int reload = 0;
44  static char *gpg_secring = NULL;  static char *gpg_secring = NULL;
45    
46    
47    /* Reload the key cache. */
48  void  void
49  keycache_reload (HWND dlg)  keycache_reload (HWND dlg)
50  {        {      
# Line 53  keycache_reload (HWND dlg) Line 55  keycache_reload (HWND dlg)
55      rcs.tr_update = 0;      rcs.tr_update = 0;
56      DialogBoxParam (glob_hinst, (LPCSTR)IDD_WINPT_KEYCACHE, dlg,                                              DialogBoxParam (glob_hinst, (LPCSTR)IDD_WINPT_KEYCACHE, dlg,                                        
57                      keycache_dlg_proc, (LPARAM)&rcs);                      keycache_dlg_proc, (LPARAM)&rcs);
58  } /* keycache_reload */  }
59    
60    
61    /* Release both key cache objects. If @cleanup is 1,
62       also release other global structs. */
63  void  void
64  keycache_release (void)  keycache_release (int cleanup)
65  {  {
66      int n = gpgme_keycache_count (pub);      int n = gpg_keycache_get_size (pub);
67      char tmpbuf[64];      char tmpbuf[64];
68    
69      /* XXX: update the value when the cache has changed. */      /* XXX: update the value when the cache has changed. */
# Line 66  keycache_release (void) Line 71  keycache_release (void)
71      set_reg_key (HKEY_CURRENT_USER, "Software\\WinPT", "nKeys", tmpbuf);      set_reg_key (HKEY_CURRENT_USER, "Software\\WinPT", "nKeys", tmpbuf);
72    
73      if (pub) {      if (pub) {
74          gpgme_keycache_release (pub);          gpg_keycache_release (pub);
75          pub = NULL;          pub = NULL;
76      }      }
77      if (sec) {      if (sec) {
78          gpgme_keycache_release (sec);          gpg_keycache_release (sec);
79          sec = NULL;          sec = NULL;
80      }      }
81  } /* keycache_release */      if (cleanup) {
82            if (gpg_secring)
83                free (gpg_secring);
84            gpg_secring = NULL;
85        }
86    }
87    
88    
89    /* Update the key with the keyid @keyid in the key cache.
90       If @is_sec is 1, the secret key cache is used. */
91  gpgme_error_t  gpgme_error_t
92  keycache_update (int is_sec, const char *keyid)  keycache_update (int is_sec, const char *keyid)
93  {  {
94      gpgme_keycache_t ctx = pub;      gpg_keycache_t ctx = pub;
95      gpgme_error_t err;      gpgme_error_t err;
96    
97      if (is_sec)      if (is_sec)
98          ctx = sec;          ctx = sec;
99      err =  gpgme_keycache_update_key (ctx, is_sec, pub, keyid);      err = gpg_keycache_update_key (ctx, is_sec, pub, keyid);
100      if (is_sec)      if (is_sec)
101          gpgme_keycache_prepare_single (ctx, keyid, NULL, gpg_secring);          gpg_keycache_prepare_single (ctx, keyid, NULL, gpg_secring);
102      return err;      return err;
103  }  }
104    
# Line 106  keycache_init (const char *pubring, cons Line 118  keycache_init (const char *pubring, cons
118      }      }
119    
120      if (reload) {      if (reload) {
121          keycache_release ();          keycache_release (0);
122          reload = 0;          reload = 0;
123      }      }
124      p = get_reg_entry (HKEY_CURRENT_USER, "Software\\WinPT", "nKeys");      p = get_reg_entry (HKEY_CURRENT_USER, "Software\\WinPT", "nKeys");
# Line 116  keycache_init (const char *pubring, cons Line 128  keycache_init (const char *pubring, cons
128          memset (&pfx, 0, sizeof (pfx));          memset (&pfx, 0, sizeof (pfx));
129      }      }
130    
131      err = gpgme_keycache_new (&pub);      err = gpg_keycache_new (&pub);
132      if (err)      if (err)
133          return err;          return err;
134      if (val != 0)      if (val != 0)
135          gpgme_keycache_set_cb (pub, progress_callback, &pfx, val);          gpg_keycache_set_cb (pub, progress_callback, &pfx, val);
136      err = gpgme_keycache_new (&sec);      err = gpg_keycache_new (&sec);
137      if (!err)      if (!err)
138          err = gpgme_keycache_init (pub, NULL, 0);          err = gpg_keycache_init (pub, NULL, 0);
139      if (!err)      if (!err)
140          err = gpgme_keycache_init( sec, NULL, 1 );          err = gpg_keycache_init( sec, NULL, 1 );
141      if( !err && pubring && *pubring )      if( !err && pubring && *pubring )
142          err = gpgme_keycache_prepare( pub, pubring, NULL );          err = gpg_keycache_prepare( pub, pubring, NULL );
143      if( !err && secring && * secring )      if( !err && secring && * secring )
144          err = gpgme_keycache_prepare( sec, NULL, secring );          err = gpg_keycache_prepare( sec, NULL, secring );
145      if (!err)      if (!err)
146          gpgme_keycache_sync (pub, sec);          gpg_keycache_sync (pub, sec);
147      if (val != 0)      if (val != 0)
148          progress_cleanup (&pfx);          progress_cleanup (&pfx);
149      return err;      return err;
# Line 152  keycache_get_reload( void ) Line 164  keycache_get_reload( void )
164  } /* keycache_get_reload */  } /* keycache_get_reload */
165    
166    
167  gpgme_keycache_t  gpg_keycache_t
168  keycache_get_ctx (int is_pub)  keycache_get_ctx (int is_pub)
169  {  {
170      return is_pub? pub : sec;      return is_pub? pub : sec;
171  } /* keycache_get_ctx */  }
172    
173    
174    /* Get the GPG key with keyid @keyid from the cache. Return it
175       in @r_key on success. */
176  static int  static int
177  get_key( const char *keyid, gpgme_key_t *r_key, int secret )  get_key_from_cache (const char *keyid, gpgme_key_t *r_key,
178                        struct keycache_s **c, int secret)
179  {  {
180      gpgme_keycache_t cache;      gpg_keycache_t cache;
181      gpgme_error_t err;      gpgme_error_t err;
182      int mode = secret? KEYCACHE_PRV : KEYCACHE_PUB;      int mode = secret? KEYCACHE_PRV : KEYCACHE_PUB;
183    
184      if( !keyid )      if (!keyid)
185          return WPTERR_GENERAL;          return WPTERR_GENERAL;
186      if( r_key )      if (r_key)
187          *r_key = NULL;          *r_key = NULL;
188      cache = keycache_get_ctx( mode );      cache = keycache_get_ctx (mode);
189      if( !cache )      if (!cache)
190          BUG( NULL );          BUG( NULL );
191      err = gpgme_keycache_find_key( cache, keyid, 0, r_key );      if (!c)
192      if( err )          err = gpg_keycache_find_key (cache, keyid, 0, r_key);
193          return WPTERR_GENERAL;      else
194      return 0;          err = gpg_keycache_find_key2 (cache, keyid, 0, r_key, c);
195  } /* get_key */      return err? WPTERR_GENERAL : 0;
196    }
197    
198    
199    /* Get GPG key with keyid @keyid directly from GPG and return
200       it in @r_key on success. */
201  static int  static int
202  get_key2( const char * keyid, gpgme_key_t * r_key, int secret )  get_key_directly (const char *keyid, gpgme_key_t *r_key, int secret)
203  {  {
204      gpg_iobuf_t inp;      gpgme_ctx_t ctx;
205      char * p;      gpgme_error_t err;
     int rc;  
206    
207      p = get_gnupg_keyring (1, !NO_STRICT);      err = gpgme_new (&ctx);
208      if( !p )      if (err)
209          BUG( NULL );          return WPTERR_GENERAL;
210        err = gpgme_get_key (ctx, keyid, r_key, secret);
211        gpgme_release (ctx);
212        return err? WPTERR_GENERAL : 0;
213    }
214    
     inp = gpg_iobuf_open( p );  
     if( !inp ) {  
         const char *s = winpt_strerror( WPTERR_FILE_OPEN );  
         log_box( _("WinPT Error"), 0, "%s: %s", p, s );  
         free_if_alloc( p );  
         return NULL;  
     }  
     gpg_iobuf_ioctl( inp, 3, 1, NULL ); /* disable cache */  
     rc = gpgme_getkey_bykeyid( inp, keyid, r_key );  
215    
216      gpg_iobuf_close( inp );  int
217      free_if_alloc( p );  winpt_get_pubkey (const char *keyid, winpt_key_s *k)
218    {
219        int rc;
220        
221        rc = get_key_from_cache (keyid, &k->ctx, &k->ext, 0);
222        if (rc)
223            return rc;
224        k->is_v3 = k->ctx->subkeys->pubkey_algo == GPGME_PK_RSA && strlen (k->ctx->subkeys->fpr) == 32;
225        k->is_protected = k->ext->gloflags.is_protected;
226        k->keyid = k->ctx->subkeys->keyid;
227        k->uid = k->ctx->uids->uid;
228      return rc;      return rc;
229  } /* get_key2 */  }
230    
231    
232    int
233    winpt_get_seckey (const char *keyid, winpt_key_s *k)
234    {
235        int rc;
236        rc = get_key_from_cache (keyid, &k->ctx, &k->ext, 1);
237        if (rc)
238            return rc;
239        k->is_v3 = k->ctx->subkeys->pubkey_algo == GPGME_PK_RSA && strlen (k->ctx->subkeys->fpr) == 32;
240        k->is_protected = k->ext->gloflags.is_protected;
241        k->keyid = k->ctx->subkeys->keyid;
242        k->uid = k->ctx->uids->uid;
243        return rc;
244    }
245    
246    
247  int  int
# Line 213  get_pubkey (const char *keyid, gpgme_key Line 250  get_pubkey (const char *keyid, gpgme_key
250      int rc = 0;      int rc = 0;
251    
252      if (pub && sec)      if (pub && sec)
253          rc = get_key (keyid, ret_key, 0);          rc = get_key_from_cache (keyid, ret_key, NULL, 0);
254      else      else
255          rc = get_key2 (keyid, ret_key, 0);          rc = get_key_directly (keyid, ret_key, 0);
256      return rc;      return rc;
257  } /* get_pubkey */  }
258    
259    
260  int  int
261  get_seckey( const char *keyid, gpgme_key_t *ret_skey )  get_seckey (const char *keyid, gpgme_key_t *ret_skey)
262  {  {
263      int rc = 0;      int rc = 0;
264    
265      if( pub && sec )      if (pub && sec)
266          rc = get_key( keyid, ret_skey, 1 );          rc = get_key_from_cache (keyid, ret_skey, NULL, 1);
267      else      else
268          rc = get_key2( keyid, ret_skey, 1 );          rc = get_key_directly (keyid, ret_skey, 1);
269      return rc;      return rc;
270  } /* get_seckey */  }
271    
272    
273    /* Search for insecure ElGamal keys and return the
274       number of founded keys. */
275  int  int
276  count_insecure_elgkeys (void)  count_insecure_elgkeys (void)
277  {  {
278      gpgme_keycache_t pc;      gpg_keycache_t pc;
279      gpgme_key_t key;      gpgme_key_t key;
280      int n=0;      int n=0;
281    
282      pc = keycache_get_ctx (1);      pc = keycache_get_ctx (1);
283      if (!pc)      if (!pc)
284          BUG (0);          BUG (0);
285      while (!gpgme_keycache_next_key (pc, 0, &key)) {      while (!gpg_keycache_next_key (pc, 0, &key)) {
286          if (gpgme_key_get_ulong_attr (key, GPGME_ATTR_ALGO, NULL, 0)          if (key->subkeys->pubkey_algo == GPGME_PK_ELG)
             == GPGME_PK_ELG_ES)  
287              n++;              n++;
288      }      }
289      gpgme_keycache_rewind (pc);      gpg_keycache_rewind (pc);
290      return n;      return n;
291  }  }

Legend:
Removed from v.22  
changed lines
  Added in v.25

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26