/[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 16 by twoaday, Tue Apr 11 06:56:23 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            SetWindowText (dlg, _("Decryption"));
159          SetForegroundWindow (dlg);          SetForegroundWindow (dlg);
160            center_window (dlg, ctx->main_wnd);
161          break;          break;
162    
163      case WM_COMMAND:      case WM_COMMAND:
164            if (HIWORD (wparam) == BN_CLICKED &&
165                LOWORD (wparam) == IDC_DECRYPT_HIDE) {
166                HWND hwnd;
167                hwnd = GetDlgItem (dlg, IDC_DECRYPT_PWD);
168                SendMessage (hwnd, EM_SETPASSWORDCHAR,
169                             IsDlgButtonChecked (dlg, IDC_DECRYPT_HIDE)? '*' : 0, 0);
170                SetFocus (hwnd);
171                break;
172            }
173    
174          switch (LOWORD (wparam)) {          switch (LOWORD (wparam)) {
175          case IDCANCEL:          case IDCANCEL:
176              ctx->cancel = 1;              ctx->cancel = 1;
# Line 97  pass_cb_dlg_proc (HWND dlg, UINT msg, WP Line 185  pass_cb_dlg_proc (HWND dlg, UINT msg, WP
185              else              else
186                  SetDlgItemText (dlg, IDC_DECRYPT_PWDINFO,                  SetDlgItemText (dlg, IDC_DECRYPT_PWDINFO,
187                                  _("Please enter your passphrase"));                                  _("Please enter your passphrase"));
188              n = SendDlgItemMessage (dlg, IDC_DECRYPT_PWD,              n = SendDlgItemMessage (dlg, IDC_DECRYPT_PWD,
189                                      WM_GETTEXTLENGTH, 0, 0);                                      WM_GETTEXTLENGTH, 0, 0);
190              if (n < 1) {              if (n < 1) {
191                  MessageBox (dlg, _("Please enter your passphrase"),                  MessageBox (dlg, _("Please enter your passphrase"),
# Line 107  pass_cb_dlg_proc (HWND dlg, UINT msg, WP Line 195  pass_cb_dlg_proc (HWND dlg, UINT msg, WP
195              ctx->cancel = 0;              ctx->cancel = 0;
196              ctx->pass = xcalloc (1, n+2);              ctx->pass = xcalloc (1, n+2);
197              GetDlgItemText (dlg, IDC_DECRYPT_PWD, ctx->pass, n+1);              GetDlgItemText (dlg, IDC_DECRYPT_PWD, ctx->pass, n+1);
198    
199                /*passphrase_put (&the_cache, ctx->keyid, ctx->pass);*/
200              EndDialog (dlg, TRUE);              EndDialog (dlg, TRUE);
201              break;              break;
202          }          }
# Line 125  passphrase_cb (void *hook, Line 215  passphrase_cb (void *hook,
215  {  {
216      pass_cb_t cb = (pass_cb_t)hook;      pass_cb_t cb = (pass_cb_t)hook;
217      HANDLE fp = (HANDLE)fd;      HANDLE fp = (HANDLE)fd;
218        const char *pass;
219      DWORD nwritten = 0;      DWORD nwritten = 0;
220            
221      cb->prev_was_bad = prev_was_bad;      cb->prev_was_bad = prev_was_bad;
# Line 135  passphrase_cb (void *hook, Line 226  passphrase_cb (void *hook,
226      if (!cb->pass && !cb->cancel) {      if (!cb->pass && !cb->cancel) {
227          cb->uid_hint = uid_hint;          cb->uid_hint = uid_hint;
228          cb->passphrase_info = passphrase_info;          cb->passphrase_info = passphrase_info;
229          DialogBoxParam (mod_hinst_dll, (LPCTSTR)IDD_DECRYPT,          memset (cb->keyid, 0, sizeof (cb->keyid));
230                          cb->main_hwnd, pass_cb_dlg_proc, (LPARAM)cb);          memcpy (cb->keyid, cb->passphrase_info+8, 8);
     }  
231    
232            if (the_cache && (pass=passphrase_get (the_cache, cb->keyid)))
233                cb->pass = xstrdup (pass);
234            else
235                DialogBoxParam (mod_hinst_dll, (LPCTSTR)IDD_DECRYPT,
236                                cb->main_wnd, pass_cb_dlg_proc, (LPARAM)cb);
237        }
238      if (cb->cancel)      if (cb->cancel)
239          WriteFile (fp, "\n", 1, &nwritten, NULL);          WriteFile (fp, "\n", 1, &nwritten, NULL);
240      else if (cb->pass != NULL) {      else if (cb->pass != NULL) {
# Line 156  new_pass_cb (HWND main) Line 252  new_pass_cb (HWND main)
252      pass_cb_t cb_val;      pass_cb_t cb_val;
253    
254      cb_val = xcalloc (1, sizeof *cb_val);      cb_val = xcalloc (1, sizeof *cb_val);
255      cb_val->main_hwnd = main;      cb_val->main_wnd = main;
256      return cb_val;      return cb_val;
257  }  }
258    
# Line 174  free_pass_cb (pass_cb_t cb) Line 270  free_pass_cb (pass_cb_t cb)
270      }      }
271      free_if_alloc (cb);      free_if_alloc (cb);
272  }  }
273    
274    
275    /* Release the passphrase cache. */
276    void
277    free_pass_cache (void)
278    {
279        invalidate_cache (the_cache);
280        the_cache = NULL;
281    }

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26