/[gpgoe]/trunk/src/OEPassphraseCBDlg.c
ViewVC logotype

Diff of /trunk/src/OEPassphraseCBDlg.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 16 by twoaday, Tue Apr 11 06:56:23 2006 UTC revision 19 by twoaday, Sun Jun 4 10:12:47 2006 UTC
# Line 28  Line 28 
28  #include "GPGOE.h"  #include "GPGOE.h"
29    
30    
 /* Structure for the passphrase cache. */  
 struct pass_cache_s {  
     struct pass_cache_s *next;  
     char *keyid;  
     char *pass;  
 };  
 typedef struct pass_cache_s *pass_cache_t;  
   
31  /* Structure for the passphrase callback. */  /* Structure for the passphrase callback. */
32  struct pass_cb_s {  struct pass_cb_s {
33      const char *uid_hint;      const char *uid_hint;
34      const char *passphrase_info;      const char *passphrase_info;
35      char keyid[8+2];      char keyid[8+2];
36      char *pass;      char *pass;
     pass_cache_t cache;  
37      HWND main_wnd;      HWND main_wnd;
38      int cancel;      int cancel;
39      int prev_was_bad;      int prev_was_bad;
40  };  };
41    
42    
43  /* Global passphrase cache. */  extern char gpgoe_pass_cache[HASH_BUCKETS][256];
 static pass_cache_t the_cache = NULL;  
44    
45    
46  /* Release the passphrase cache @ctx and invalidate all passphrases. */  DWORD hash_string (const char *str_param);
 static void  
 invalidate_cache (pass_cache_t ctx)  
 {  
     pass_cache_t c;  
47    
48      while (ctx) {  /* Store the passphrase @pass in the hash table using the keyid @keyid
49          c = ctx->next;     as the index. */
         free_if_alloc (ctx->keyid);  
         wipememory (ctx->pass, strlen (ctx->pass));  
         free_if_alloc (ctx->pass);  
         ctx = c;  
     }  
 }  
   
 /* Put the passphrase @pass into the passphrase cache @ctx. */  
50  static void  static void
51  passphrase_put (pass_cache_t *ctx, const char *keyid, const char *pass)  passphrase_put (char ctx[HASH_BUCKETS][256], const char *keyid, const char *pass)
52  {  {
53      pass_cache_t c, n;      int pos = hash_string (keyid) % HASH_BUCKETS;
54        int n = 0;
     /* check if the item is already present. */  
     for (n = *ctx; n; n = n->next) {  
         if (!strcmp (n->keyid, keyid))  
             return;  
     }  
   
     c = xcalloc (1, sizeof *c);  
     c->keyid = xstrdup (keyid);  
     c->pass = xstrdup (pass);  
55    
56      if (!*ctx)      while (n < HASH_BUCKETS) {
57          *ctx = c;          if (strlen (ctx[(pos+n) % HASH_BUCKETS]) == 0) {
58      else {              memcpy (ctx[(pos+n) % HASH_BUCKETS], keyid, strlen (keyid));
59          for (n = *ctx; n->next; n = n->next)              strncpy (ctx[(pos+n) % HASH_BUCKETS]+strlen (keyid), pass, 240);
60              ;              break;
61          n->next = c;          }
62            n++;
63      }      }
64  }  }
65    
66    
67  /* Return the passphrase for the key with the keyid @keyid  /* Return the requested passphrase from the hash table @ctx
68     or NULL if the passphrase was not cached for this key. */     using the keyid @keyid as the index. Or NULL if there is
69       no stored passphrase. */
70  static const char*  static const char*
71  passphrase_get (pass_cache_t ctx, const char *keyid)  passphrase_get (char ctx[HASH_BUCKETS][256], const char *keyid)
72  {  {
73      pass_cache_t c;      const char *item;
74        int pos = hash_string (keyid) % HASH_BUCKETS;
75        int n=0;
76    
77      for (c = ctx; c; c = c->next) {      item = gpgoe_pass_cache[pos];
78          if (!strcmp (c->keyid, keyid))      while (strncmp (item, keyid, strlen (keyid)) &&
79              return c->pass;              n < HASH_BUCKETS) {
80            item = ctx[(pos+n) % HASH_BUCKETS];
81            n++;
82      }      }
83    
84        if (strlen (item) > 0 && !strncmp (item, keyid, strlen (keyid)))
85            return item+strlen (keyid);
86      return NULL;      return NULL;
87  }  }
88    
89    
   
   
90  /* Extract public key algorithm from passwd info. */  /* Extract public key algorithm from passwd info. */
91  const char*  const char*
92  get_pubkey_algo (const char *passphrase_info)  get_pubkey_algo (const char *passphrase_info)
# Line 155  pass_cb_dlg_proc (HWND dlg, UINT msg, WP Line 131  pass_cb_dlg_proc (HWND dlg, UINT msg, WP
131          SendDlgItemMessage (dlg, IDC_DECRYPT_PWD, EM_SETPASSWORDCHAR,          SendDlgItemMessage (dlg, IDC_DECRYPT_PWD, EM_SETPASSWORDCHAR,
132                              (WPARAM)(UINT)'*', 0);                              (WPARAM)(UINT)'*', 0);
133          CheckDlgButton (dlg, IDC_DECRYPT_HIDE, BST_CHECKED);          CheckDlgButton (dlg, IDC_DECRYPT_HIDE, BST_CHECKED);
134            SetDlgItemText (dlg, IDC_DECRYPT_HIDE, _("&Hide typing"));
135          SetWindowText (dlg, _("Decryption"));          SetWindowText (dlg, _("Decryption"));
136          SetForegroundWindow (dlg);          SetForegroundWindow (dlg);
137          center_window (dlg, ctx->main_wnd);          center_window (dlg, ctx->main_wnd);
# Line 196  pass_cb_dlg_proc (HWND dlg, UINT msg, WP Line 173  pass_cb_dlg_proc (HWND dlg, UINT msg, WP
173              ctx->pass = xcalloc (1, n+2);              ctx->pass = xcalloc (1, n+2);
174              GetDlgItemText (dlg, IDC_DECRYPT_PWD, ctx->pass, n+1);              GetDlgItemText (dlg, IDC_DECRYPT_PWD, ctx->pass, n+1);
175    
176              /*passphrase_put (&the_cache, ctx->keyid, ctx->pass);*/              if (gpgoe_get_active_modes () & GPGOE_MODE_CACHEPASS)
177                    passphrase_put (gpgoe_pass_cache, ctx->keyid, ctx->pass);
178              EndDialog (dlg, TRUE);              EndDialog (dlg, TRUE);
179              break;              break;
180          }          }
# Line 218  passphrase_cb (void *hook, Line 196  passphrase_cb (void *hook,
196      const char *pass;      const char *pass;
197      DWORD nwritten = 0;      DWORD nwritten = 0;
198            
199        assert (cb);
200      cb->prev_was_bad = prev_was_bad;      cb->prev_was_bad = prev_was_bad;
201      if (prev_was_bad && !cb->cancel) {      if (prev_was_bad && !cb->cancel) {
202          wipememory (cb->pass, strlen (cb->pass));          wipememory (cb->pass, strlen (cb->pass));
# Line 229  passphrase_cb (void *hook, Line 208  passphrase_cb (void *hook,
208          memset (cb->keyid, 0, sizeof (cb->keyid));          memset (cb->keyid, 0, sizeof (cb->keyid));
209          memcpy (cb->keyid, cb->passphrase_info+8, 8);          memcpy (cb->keyid, cb->passphrase_info+8, 8);
210    
211          if (the_cache && (pass=passphrase_get (the_cache, cb->keyid)))          pass = passphrase_get (gpgoe_pass_cache, cb->keyid);
212            if (pass)
213              cb->pass = xstrdup (pass);              cb->pass = xstrdup (pass);
214          else          else
215              DialogBoxParam (mod_hinst_dll, (LPCTSTR)IDD_DECRYPT,              DialogBoxParam (mod_hinst_dll, (LPCTSTR)IDD_DECRYPT,
# Line 272  free_pass_cb (pass_cb_t cb) Line 252  free_pass_cb (pass_cb_t cb)
252  }  }
253    
254    
255  /* Release the passphrase cache. */  int
256    pass_cb_cancelled (pass_cb_t cb)
257    {
258        return cb->cancel;
259    }
260    
261    
262    /* Reset the passphrase cache. */
263  void  void
264  free_pass_cache (void)  reset_pass_cache (void)
265  {  {
266      invalidate_cache (the_cache);      int i;
267      the_cache = NULL;  
268        for (i=0; i < HASH_BUCKETS; i++)
269            wipememory (gpgoe_pass_cache[i], 256);
270  }  }

Legend:
Removed from v.16  
changed lines
  Added in v.19

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26