/[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 1 by twoaday, Fri Mar 24 13:36:54 2006 UTC revision 18 by twoaday, Thu Apr 13 07:41:30 2006 UTC
# Line 27  Line 27 
27  #include "gpgme.h"  #include "gpgme.h"
28  #include "GPGOE.h"  #include "GPGOE.h"
29    
30    
31    /* Structure for the passphrase cache. */
32    struct pass_cache_s {
33        struct pass_cache_s *next;
34        char *keyid;
35        char *pass;
36    };
37    typedef struct pass_cache_s *pass_cache_t;
38    
39  /* Structure for the passphrase callback. */  /* Structure for the passphrase callback. */
40  struct pass_cb_s {  struct pass_cb_s {
41      const char *uid_hint;      const char *uid_hint;
42      const char *passphrase_info;      const char *passphrase_info;
43        char keyid[8+2];
44      char *pass;      char *pass;
45      HWND main_hwnd;      pass_cache_t cache;
46        HWND main_wnd;
47      int cancel;      int cancel;
48      int prev_was_bad;      int prev_was_bad;
49  };  };
50    
51    
52    /* Global passphrase cache. */
53    static pass_cache_t the_cache = NULL;
54    
55    
56    /* Release the passphrase cache @ctx and invalidate all passphrases. */
57    static void
58    invalidate_cache (pass_cache_t ctx)
59    {
60        pass_cache_t c;
61    
62        while (ctx) {
63            c = ctx->next;
64            free_if_alloc (ctx->keyid);
65            wipememory (ctx->pass, strlen (ctx->pass));
66            free_if_alloc (ctx->pass);
67            ctx = c;
68        }
69    }
70    
71    /* Put the passphrase @pass into the passphrase cache @ctx. */
72    static void
73    passphrase_put (pass_cache_t *ctx, const char *keyid, const char *pass)
74    {
75        pass_cache_t c, n;
76    
77        /* check if the item is already present. */
78        for (n = *ctx; n; n = n->next) {
79            if (!strcmp (n->keyid, keyid))
80                return;
81        }
82    
83        c = xcalloc (1, sizeof *c);
84        c->keyid = xstrdup (keyid);
85        c->pass = xstrdup (pass);
86    
87        if (!*ctx)
88            *ctx = c;
89        else {
90            for (n = *ctx; n->next; n = n->next)
91                ;
92            n->next = c;
93        }
94    }
95    
96    
97    /* Return the passphrase for the key with the keyid @keyid
98       or NULL if the passphrase was not cached for this key. */
99    static const char*
100    passphrase_get (pass_cache_t ctx, const char *keyid)
101    {
102        pass_cache_t c;
103    
104        for (c = ctx; c; c = c->next) {
105            if (!strcmp (c->keyid, keyid))
106                return c->pass;
107        }
108        return NULL;
109    }
110    
111    
112    
113    
114  /* Extract public key algorithm from passwd info. */  /* Extract public key algorithm from passwd info. */
115  const char*  const char*
116  get_pubkey_algo (const char *passphrase_info)  get_pubkey_algo (const char *passphrase_info)
# Line 47  get_pubkey_algo (const char *passphrase_ Line 120  get_pubkey_algo (const char *passphrase_
120      /* AA6F7AB7DD3B4A21 E71D9BC8C93A8529 1 0 */      /* AA6F7AB7DD3B4A21 E71D9BC8C93A8529 1 0 */
121      assert (strlen (passphrase_info) > pos);      assert (strlen (passphrase_info) > pos);
122      switch (atol (passphrase_info+pos)) {      switch (atol (passphrase_info+pos)) {
123      case 1: return "RSA";      case  1: return "RSA";
124      case 17: return "DSA";      case 17: return "DSA";
125      case 16: return "ELG";      case 16: return "ELG";
126      }      }
# Line 60  BOOL CALLBACK Line 133  BOOL CALLBACK
133  pass_cb_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)  pass_cb_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
134  {  {
135      static pass_cb_t ctx;      static pass_cb_t ctx;
136      char *info, keyid[8+2] = {0};      char *info;
137      int n;      int n;
138            
139      switch (msg) {      switch (msg) {
# Line 69  pass_cb_dlg_proc (HWND dlg, UINT msg, WP Line 142  pass_cb_dlg_proc (HWND dlg, UINT msg, WP
142          assert (ctx);          assert (ctx);
143          if (ctx->uid_hint && ctx->passphrase_info) {          if (ctx->uid_hint && ctx->passphrase_info) {
144              char *utf8_uid = utf8_to_native (ctx->uid_hint+17);              char *utf8_uid = utf8_to_native (ctx->uid_hint+17);
145              memcpy (keyid, ctx->passphrase_info+8, 8);              info = xcalloc (1, strlen (utf8_uid) +  strlen (ctx->keyid) + 32);
             info = xcalloc (1, strlen (utf8_uid) +  strlen (keyid) + 32);  
146              sprintf (info, _("%s\n%s key, ID %s"), utf8_uid,              sprintf (info, _("%s\n%s key, ID %s"), utf8_uid,
147                              get_pubkey_algo (ctx->passphrase_info), keyid);                              get_pubkey_algo (ctx->passphrase_info), ctx->keyid);
148              SetDlgItemText (dlg, IDC_DECRYPT_MSG, info);              SetDlgItemText (dlg, IDC_DECRYPT_MSG, info);
149              free_if_alloc (utf8_uid);              free_if_alloc (utf8_uid);
150              free_if_alloc (info);              free_if_alloc (info);
151          }          }
152            SetDlgItemText (dlg, IDOK, _("&OK"));
153            SetDlgItemText (dlg, IDCANCEL, _("&Cancel"));
154            SetDlgItemText (dlg, IDC_DECRYPT_PWDINFO, _("Please enter your passphrase"));
155          SendDlgItemMessage (dlg, IDC_DECRYPT_PWD, EM_SETPASSWORDCHAR,          SendDlgItemMessage (dlg, IDC_DECRYPT_PWD, EM_SETPASSWORDCHAR,
156                              (WPARAM)(UINT)'*', 0);                              (WPARAM)(UINT)'*', 0);
157            CheckDlgButton (dlg, IDC_DECRYPT_HIDE, BST_CHECKED);
158            SetDlgItemText (dlg, IDC_DECRYPT_HIDE, _("&Hide typing"));
159            SetWindowText (dlg, _("Decryption"));
160          SetForegroundWindow (dlg);          SetForegroundWindow (dlg);
161            center_window (dlg, ctx->main_wnd);
162          break;          break;
163    
164      case WM_COMMAND:      case WM_COMMAND:
165            if (HIWORD (wparam) == BN_CLICKED &&
166                LOWORD (wparam) == IDC_DECRYPT_HIDE) {
167                HWND hwnd;
168                hwnd = GetDlgItem (dlg, IDC_DECRYPT_PWD);
169                SendMessage (hwnd, EM_SETPASSWORDCHAR,
170                             IsDlgButtonChecked (dlg, IDC_DECRYPT_HIDE)? '*' : 0, 0);
171                SetFocus (hwnd);
172                break;
173            }
174    
175          switch (LOWORD (wparam)) {          switch (LOWORD (wparam)) {
176          case IDCANCEL:          case IDCANCEL:
177              ctx->cancel = 1;              ctx->cancel = 1;
# Line 97  pass_cb_dlg_proc (HWND dlg, UINT msg, WP Line 186  pass_cb_dlg_proc (HWND dlg, UINT msg, WP
186              else              else
187                  SetDlgItemText (dlg, IDC_DECRYPT_PWDINFO,                  SetDlgItemText (dlg, IDC_DECRYPT_PWDINFO,
188                                  _("Please enter your passphrase"));                                  _("Please enter your passphrase"));
189              n = SendDlgItemMessage (dlg, IDC_DECRYPT_PWD,              n = SendDlgItemMessage (dlg, IDC_DECRYPT_PWD,
190                                      WM_GETTEXTLENGTH, 0, 0);                                      WM_GETTEXTLENGTH, 0, 0);
191              if (n < 1) {              if (n < 1) {
192                  MessageBox (dlg, _("Please enter your passphrase"),                  MessageBox (dlg, _("Please enter your passphrase"),
# Line 107  pass_cb_dlg_proc (HWND dlg, UINT msg, WP Line 196  pass_cb_dlg_proc (HWND dlg, UINT msg, WP
196              ctx->cancel = 0;              ctx->cancel = 0;
197              ctx->pass = xcalloc (1, n+2);              ctx->pass = xcalloc (1, n+2);
198              GetDlgItemText (dlg, IDC_DECRYPT_PWD, ctx->pass, n+1);              GetDlgItemText (dlg, IDC_DECRYPT_PWD, ctx->pass, n+1);
199    
200                /*passphrase_put (&the_cache, ctx->keyid, ctx->pass);*/
201              EndDialog (dlg, TRUE);              EndDialog (dlg, TRUE);
202              break;              break;
203          }          }
# Line 125  passphrase_cb (void *hook, Line 216  passphrase_cb (void *hook,
216  {  {
217      pass_cb_t cb = (pass_cb_t)hook;      pass_cb_t cb = (pass_cb_t)hook;
218      HANDLE fp = (HANDLE)fd;      HANDLE fp = (HANDLE)fd;
219        const char *pass;
220      DWORD nwritten = 0;      DWORD nwritten = 0;
221            
222      cb->prev_was_bad = prev_was_bad;      cb->prev_was_bad = prev_was_bad;
# Line 135  passphrase_cb (void *hook, Line 227  passphrase_cb (void *hook,
227      if (!cb->pass && !cb->cancel) {      if (!cb->pass && !cb->cancel) {
228          cb->uid_hint = uid_hint;          cb->uid_hint = uid_hint;
229          cb->passphrase_info = passphrase_info;          cb->passphrase_info = passphrase_info;
230          DialogBoxParam (mod_hinst_dll, (LPCTSTR)IDD_DECRYPT,          memset (cb->keyid, 0, sizeof (cb->keyid));
231                          cb->main_hwnd, pass_cb_dlg_proc, (LPARAM)cb);          memcpy (cb->keyid, cb->passphrase_info+8, 8);
     }  
232    
233            if (the_cache && (pass=passphrase_get (the_cache, cb->keyid)))
234                cb->pass = xstrdup (pass);
235            else
236                DialogBoxParam (mod_hinst_dll, (LPCTSTR)IDD_DECRYPT,
237                                cb->main_wnd, pass_cb_dlg_proc, (LPARAM)cb);
238        }
239      if (cb->cancel)      if (cb->cancel)
240          WriteFile (fp, "\n", 1, &nwritten, NULL);          WriteFile (fp, "\n", 1, &nwritten, NULL);
241      else if (cb->pass != NULL) {      else if (cb->pass != NULL) {
# Line 156  new_pass_cb (HWND main) Line 253  new_pass_cb (HWND main)
253      pass_cb_t cb_val;      pass_cb_t cb_val;
254    
255      cb_val = xcalloc (1, sizeof *cb_val);      cb_val = xcalloc (1, sizeof *cb_val);
256      cb_val->main_hwnd = main;      cb_val->main_wnd = main;
257      return cb_val;      return cb_val;
258  }  }
259    
# Line 174  free_pass_cb (pass_cb_t cb) Line 271  free_pass_cb (pass_cb_t cb)
271      }      }
272      free_if_alloc (cb);      free_if_alloc (cb);
273  }  }
274    
275    
276    int
277    pass_cb_cancelled (pass_cb_t cb)
278    {
279        return cb->cancel;
280    }
281    
282    
283    /* Release the passphrase cache. */
284    void
285    free_pass_cache (void)
286    {
287        invalidate_cache (the_cache);
288        the_cache = NULL;
289    }

Legend:
Removed from v.1  
changed lines
  Added in v.18

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26