/[winpt]/trunk/Src/wptPassphraseCB.cpp
ViewVC logotype

Diff of /trunk/Src/wptPassphraseCB.cpp

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

revision 47 by werner, Mon Oct 31 14:04:59 2005 UTC revision 214 by twoaday, Sun May 14 18:40:36 2006 UTC
# Line 1  Line 1 
1  /* wptPassphraseCB.cpp - GPGME Passphrase Callback  /* wptPassphraseCB.cpp - GPGME Passphrase Callback
2   *      Copyright (C) 2001, 2002, 2003, 2005 Timo Schulz   *      Copyright (C) 2001, 2002, 2003, 2005, 2006 Timo Schulz
3   *      Copyright (C) 2005 g10 Code GmbH   *      Copyright (C) 2005 g10 Code GmbH
4   *   *
5   * This file is part of WinPT.   * This file is part of WinPT.
# Line 24  Line 24 
24  #endif  #endif
25    
26  #include <windows.h>  #include <windows.h>
 #include <windows.h>  
27  #include <ctype.h>  #include <ctype.h>
28    
29  #include "resource.h"  #include "resource.h"
# Line 35  Line 34 
34  #include "wptCommonCtl.h"  #include "wptCommonCtl.h"
35  #include "wptContext.h"  #include "wptContext.h"
36  #include "wptDlgs.h"  #include "wptDlgs.h"
 #include "wptUTF8.h"  
37  #include "wptErrors.h"  #include "wptErrors.h"
38  #include "wptTypes.h"  #include "wptTypes.h"
39  #include "wptKeyList.h"  #include "wptKeylist.h"
40  #include "wptAgent.h"  #include "wptAgent.h"
41  #include "wptRegistry.h"  #include "wptRegistry.h"
42    #include "wptUTF8.h"
43    
44  const char* get_symkey_algo (int algo);  const char* get_symkey_algo (int algo);
45    
46  #define item_ctrl_id( cmd ) \  #define item_ctrl_id(cmd) \
47      ((cmd) == GPG_CMD_DECRYPT? IDC_DECRYPT_PWD : IDC_DECRYPT_SIGN_PWD)      ((cmd) == GPG_CMD_DECRYPT? IDC_DECRYPT_PWD : IDC_DECRYPT_SIGN_PWD)
48    
49  #define item_ctrl_id2(cmd) \  #define item_ctrl_id2(cmd) \
50      ((cmd) == GPG_CMD_DECRYPT? IDC_DECRYPT_HIDE : IDC_DECRYPT_SIGN_HIDE)      ((cmd) == GPG_CMD_DECRYPT? IDC_DECRYPT_HIDE : IDC_DECRYPT_SIGN_HIDE)
51    
52    
53    void ListBox_AddString_utf8 (HWND lb, const char *txt);
54    
55  /* Overwrite passphrase and free memory. */  /* Overwrite passphrase and free memory. */
56  static void  static void
57  burn_passphrase (char **pwd)  burn_passphrase (char **pwd)
# Line 66  burn_passphrase (char **pwd) Line 67  burn_passphrase (char **pwd)
67  static BOOL CALLBACK  static BOOL CALLBACK
68  passphrase_callback_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)  passphrase_callback_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
69  {      {    
70      static passphrase_cb_s * c;      static passphrase_cb_s *c;
71      gpgme_decrypt_result_t res;      gpgme_decrypt_result_t res=NULL;
72      gpgme_sign_result_t res_sig;      gpgme_sign_result_t res_sig=NULL;
73      gpgme_key_t key;      gpgme_key_t key;
74      gpgme_recipient_t recip, r;      gpgme_recipient_t recip=NULL, r;
75      void *ctx = NULL, *item;      void *item;
76      const char *id;      const char *id;
77      char *info;      char *info;
78      int n;      int n;
79    
80      switch (msg) {      switch (msg) {
81        case WM_ACTIVATE:
82            safe_edit_control_init (dlg, item_ctrl_id (c->gpg_cmd));
83            break;
84    
85        case WM_DESTROY:
86            safe_edit_control_free (dlg, item_ctrl_id (c->gpg_cmd));
87            break;
88    
89      case WM_INITDIALOG:      case WM_INITDIALOG:
90          c = (passphrase_cb_s *)lparam;          c = (passphrase_cb_s *)lparam;
91          if (!c)          if (!c)
92              BUG (0);              BUG (0);
93            SetDlgItemText (dlg, IDCANCEL, _("&Cancel"));
94          SetWindowText (dlg, c->title);          SetWindowText (dlg, c->title);
95          if (c->gpg_cmd == GPG_CMD_DECRYPT) {          if (c->gpg_cmd == GPG_CMD_DECRYPT) {
96                SetDlgItemText (dlg, IDC_DECRYPT_HIDE, _("&Hide Typing"));
97              SetDlgItemText (dlg, IDC_DECRYPT_LISTINF,              SetDlgItemText (dlg, IDC_DECRYPT_LISTINF,
98                              _("Encrypted with the following public key(s)"));                              _("Encrypted with the following public key(s)"));
99              CheckDlgButton (dlg, IDC_DECRYPT_HIDE, BST_CHECKED);              CheckDlgButton (dlg, IDC_DECRYPT_HIDE, BST_CHECKED);
100          }          }
101          else if (c->gpg_cmd == GPG_CMD_SIGN)          else if (c->gpg_cmd == GPG_CMD_SIGN) {
102                SetDlgItemText (dlg, IDC_DECRYPT_SIGN_HIDE, _("&Hide Typing"));
103              CheckDlgButton (dlg, IDC_DECRYPT_SIGN_HIDE, BST_CHECKED);              CheckDlgButton (dlg, IDC_DECRYPT_SIGN_HIDE, BST_CHECKED);
104            }
105            /* Because it depends on the order the keys are stored in the
106               keyring whether res->recipients is complete or not, we also
107               support that the recipients were externally extracted and then
108               we use this list. */
109          if (c->recipients)          if (c->recipients)
110              recip = c->recipients; /* recipients were already extracted. */              recip = c->recipients; /* recipients were already extracted. */
111          else {          else {
             /* XXX: not all ENCRYPT_TO entries are listed here. */  
112              res = gpgme_op_decrypt_result (c->gpg);              res = gpgme_op_decrypt_result (c->gpg);
113              if (res && res->recipients)              if (res && res->recipients)
114                  recip = res->recipients;                  recip = res->recipients;
115          }          }
116          if (recip != NULL && c->gpg_cmd == GPG_CMD_DECRYPT) {          if (recip != NULL && c->gpg_cmd == GPG_CMD_DECRYPT) {
117              for (r = res->recipients; r; r = r->next) {              for (r = recip; r; r = r->next) {
118                  get_pubkey (r->keyid, &key);                  get_pubkey (r->keyid, &key);
119                  if (key) {                  if (key) {
120                      char *uid;                      gpgme_user_id_t u = key->uids;
121                      id = key->uids->name;  
122                        id = u->name;
123                      if (!id)                      if (!id)
124                          id = _("Invalid User ID");                          id = _("Invalid User ID");
125                      uid = utf8_to_wincp (id, strlen (id));                      n = 32+strlen (id)+1+4+strlen (r->keyid)+1;
126                      info = new char [32+strlen (uid)+1 + 4 + strlen (r->keyid)+1                      if (u->email)
127                                       + strlen (key->uids->email)+1];                          n += strlen (u->email)+1;
128                        info = new char [n+1];
129                      if (!info)                      if (!info)
130                          BUG (NULL);                          BUG (NULL);
131                      sprintf (info, "%s <%s> (%s, 0x%s)", uid, key->uids->email,                      if (!u->email || strlen (u->email) < 1)
132                               get_key_pubalgo (r->pubkey_algo), r->keyid+8);                          sprintf (info, "%s (%s, 0x%s)", id,
133                      free (uid);                                   get_key_pubalgo (r->pubkey_algo), r->keyid+8);
134                                            else
135                            sprintf (info, "%s <%s> (%s, 0x%s)", id, u->email,
136                                     get_key_pubalgo (r->pubkey_algo), r->keyid+8);
137                  }                  }
138                  else {                  else {
139                      info = new char [32 + strlen (r->keyid)+1 + 4];                      info = new char [32 + strlen (r->keyid)+1 + 4];
# Line 122  passphrase_callback_proc (HWND dlg, UINT Line 142  passphrase_callback_proc (HWND dlg, UINT
142                      sprintf (info, _("Unknown key ID (%s, 0x%s)"),                      sprintf (info, _("Unknown key ID (%s, 0x%s)"),
143                               get_key_pubalgo (r->pubkey_algo), r->keyid+8);                               get_key_pubalgo (r->pubkey_algo), r->keyid+8);
144                  }                  }
145                  listbox_add_string (GetDlgItem (dlg, IDC_DECRYPT_LIST), info);                  ListBox_AddString_utf8 (GetDlgItem (dlg, IDC_DECRYPT_LIST), info);
146                  free_if_alloc (info);                  free_if_alloc (info);
147              }              }
148          }          }
# Line 149  passphrase_callback_proc (HWND dlg, UINT Line 169  passphrase_callback_proc (HWND dlg, UINT
169                  SetDlgItemText (dlg, IDC_DECRYPT_MSG, c->info);                  SetDlgItemText (dlg, IDC_DECRYPT_MSG, c->info);
170          }          }
171          else {          else {
172              SetFocus( GetDlgItem (dlg, IDC_DECRYPT_SIGN_PWD));              SetFocus (GetDlgItem (dlg, IDC_DECRYPT_SIGN_PWD));
173              SetDlgItemText (dlg, IDC_DECRYPT_SIGN_MSG, c->info);              SetDlgItemText (dlg, IDC_DECRYPT_SIGN_MSG, c->info);
174          }          }
175          center_window (dlg, NULL);          center_window (dlg, NULL);
176          SetForegroundWindow (dlg);          SetForegroundWindow (dlg);
         set_active_window (dlg);  
177          return FALSE;          return FALSE;
178    
179          case WM_SYSCOMMAND:          case WM_SYSCOMMAND:
# Line 196  passphrase_callback_proc (HWND dlg, UINT Line 215  passphrase_callback_proc (HWND dlg, UINT
215                      c->pwd = new char[n+2];                      c->pwd = new char[n+2];
216                      if (!c->pwd)                      if (!c->pwd)
217                          BUG (NULL);                          BUG (NULL);
218                      GetDlgItemText (dlg, item_ctrl_id (c->gpg_cmd), c->pwd, n+1);                      SafeGetDlgItemText (dlg, item_ctrl_id (c->gpg_cmd),
219                                            c->pwd, n+1);
220                  }                  }
221                  res = gpgme_op_decrypt_result (c->gpg);                  res = gpgme_op_decrypt_result (c->gpg);
222                  if (!res)                  if (!res)
223                      res_sig = gpgme_op_sign_result (c->gpg);                      res_sig = gpgme_op_sign_result (c->gpg);            
224                  if (reg_prefs.cache_time > 0 && !c->is_card &&                  if (reg_prefs.cache_time > 0 && !c->is_card &&
225                      ((res && res->recipients) || (res_sig && res_sig->signatures))) {                      (res || res_sig)) {
226                      if (agent_get_cache (c->keyid, &item))                      if (agent_get_cache (c->keyid, &item))                      
227                          agent_unlock_cache_entry (&item);                          agent_unlock_cache_entry (&item);
228                      else                      else
229                          agent_put_cache (c->keyid, c->pwd, reg_prefs.cache_time);                          agent_put_cache (c->keyid, c->pwd, reg_prefs.cache_time);
230                  }                  }
231                  c->cancel = 0;                  c->cancel = 0;
# Line 233  parse_gpg_keyid (const char *pass_info) Line 253  parse_gpg_keyid (const char *pass_info)
253      static char keyid[16+1];      static char keyid[16+1];
254            
255      /* XXX: check for leading alpha-chars? */      /* XXX: check for leading alpha-chars? */
256      if (strlen (pass_info) < 16)      if (strlen (pass_info) < 16) {
257            log_debug ("parse_gpg_keyid: error '%s'\r\n", pass_info);
258          return NULL;          return NULL;
259        }
260      /* the format of the desc buffer looks like this:      /* the format of the desc buffer looks like this:
261         request_keyid[16] main_keyid[16] keytype[1] keylength[4]         request_keyid[16] main_keyid[16] keytype[1] keylength[4]
262         we use the main keyid to use only one cache entry. */         we use the main keyid to use only one cache entry. */
# Line 253  parse_gpg_description (const char *uid_h Line 275  parse_gpg_description (const char *uid_h
275      gpgme_pubkey_algo_t algo;      gpgme_pubkey_algo_t algo;
276      char usedkey[16+1];      char usedkey[16+1];
277      char mainkey[16+1];      char mainkey[16+1];
278      char *uid, *p;      char *p, *uid;
279      int n=0;      int n=0;
280    
281        algo = (gpgme_pubkey_algo_t)0;
282      /* Each uid_hint contains a long key-ID so it is at least 16 bytes. */      /* Each uid_hint contains a long key-ID so it is at least 16 bytes. */
283      if (strlen (uid_hint) < 17) {      if (strlen (uid_hint) < 17) {
284          *desc = 0;          *desc = 0;
285            log_debug ("parse_gpg_description: error '%s'\r\n", uid_hint);
286          return -1;          return -1;
287      }      }
288    
# Line 272  parse_gpg_description (const char *uid_h Line 296  parse_gpg_description (const char *uid_h
296      uid_hint += 16; /* skip keyid */      uid_hint += 16; /* skip keyid */
297      uid_hint += 1;  /* space */      uid_hint += 1;  /* space */
298    
299      uid = utf8_to_wincp (uid_hint, strlen (uid_hint));      uid = utf8_to_native (uid_hint);
   
300      if (strcmp (usedkey, mainkey))      if (strcmp (usedkey, mainkey))
301          _snprintf (desc, size-1,          _snprintf (desc, size-1,
302                     _("You need a passphrase to unlock the secret key for\n"                     _("You need a passphrase to unlock the secret key for\n"
# Line 286  parse_gpg_description (const char *uid_h Line 309  parse_gpg_description (const char *uid_h
309                       "user: \"%s\"\n"                       "user: \"%s\"\n"
310                       "%s key, ID %s\n"),                       "%s key, ID %s\n"),
311                       uid, get_key_pubalgo (algo), usedkey+8);                       uid, get_key_pubalgo (algo), usedkey+8);
312      free (uid);      safe_free (uid);
313      return 0;      return 0;
314  }  }
315    
# Line 299  extract_serial_no (const char *id) Line 322  extract_serial_no (const char *id)
322      char *p;      char *p;
323    
324      p = strchr (id, '/');      p = strchr (id, '/');
325      if (!p)      if (!p) {
326            log_debug ("extract_serial_no: error '%s'\r\n", id);
327          return NULL;          return NULL;
328        }
329      strncpy (buf, id+(p-id)-6, 6);      strncpy (buf, id+(p-id)-6, 6);
330      return buf;      return buf;
331  }  }
# Line 315  passphrase_cb (void *hook, const char *u Line 340  passphrase_cb (void *hook, const char *u
340      passphrase_cb_s *c = (passphrase_cb_s*)hook;      passphrase_cb_s *c = (passphrase_cb_s*)hook;
341      HANDLE hd = (HANDLE)fd;      HANDLE hd = (HANDLE)fd;
342      void *item;      void *item;
343      const char *keyid, *pass;      const char *keyid=NULL, *pass;
344      DWORD n;      DWORD n;
345      int rc;      int rc = 0;
346    
347      if (!c)      if (!c) {
348            log_debug ("passphrase_cb: error '!c'\r\n");
349          return gpg_error (GPG_ERR_INV_ARG);          return gpg_error (GPG_ERR_INV_ARG);
350        }
351      c->bad_pwd = prev_was_bad? 1 : 0;      c->bad_pwd = prev_was_bad? 1 : 0;
352      if (prev_was_bad && !c->cancel) {      if (prev_was_bad && !c->cancel) {
353          if (c->pwd)          if (c->pwd)
# Line 333  passphrase_cb (void *hook, const char *u Line 360  passphrase_cb (void *hook, const char *u
360          if (strlen (passphrase_info) < 16 &&          if (strlen (passphrase_info) < 16 &&
361              !strstr (passphrase_info, "OPENPGP")) {              !strstr (passphrase_info, "OPENPGP")) {
362              /* assume symetric encryption. */              /* assume symetric encryption. */
363              int n=2;              int pos=2;
364              c->sym.sym_algo = atoi (passphrase_info);              c->sym.sym_algo = atoi (passphrase_info);
365              if (c->sym.sym_algo > 9)              if (c->sym.sym_algo > 9)
366                  n++;                  pos++;
367              /* XXX: be more strict. */              /* XXX: be more strict. */
368              c->sym.s2k_mode = atoi (passphrase_info+n);              c->sym.s2k_mode = atoi (passphrase_info+pos);
369              c->sym.s2k_hash = atoi (passphrase_info+n+2);              c->sym.s2k_hash = atoi (passphrase_info+pos+2);
370          }          }
371    
372          keyid = parse_gpg_keyid (passphrase_info);          keyid = parse_gpg_keyid (passphrase_info);
373          pass = agent_get_cache (keyid+8, &item);          pass = agent_get_cache (keyid+8, &item);        
374          if (pass) {          if (pass) {
375              agent_unlock_cache_entry (&item);              agent_unlock_cache_entry (&item);
376              c->pwd_init = 0;              c->pwd_init = 0;
# Line 372  passphrase_cb (void *hook, const char *u Line 399  passphrase_cb (void *hook, const char *u
399          }          }
400          else if (uid_hint)          else if (uid_hint)
401              parse_gpg_description (uid_hint, passphrase_info,              parse_gpg_description (uid_hint, passphrase_info,
402                                     c->info, sizeof c->info - 1);                                     c->info, sizeof (c->info) - 1);
403          if (c->gpg_cmd == GPG_CMD_DECRYPT) {          if (c->gpg_cmd == GPG_CMD_DECRYPT) {
404              rc = DialogBoxParam (glob_hinst, (LPCSTR)IDD_WINPT_DECRYPT,              rc = DialogBoxParam (glob_hinst, (LPCSTR)IDD_WINPT_DECRYPT,
405                                   (HWND)c->hwnd, passphrase_callback_proc,                                   (HWND)c->hwnd, passphrase_callback_proc,
# Line 390  passphrase_cb (void *hook, const char *u Line 417  passphrase_cb (void *hook, const char *u
417          }          }
418          c->pwd_init = 0;          c->pwd_init = 0;
419      }      }
420      if (c->cancel) {      if (c->cancel || !c->pwd) {
421          if (!WriteFile (hd, "\n", 1, &n, NULL))          if (!WriteFile (hd, "\n", 1, &n, NULL))
422              log_debug ("passphrase_cb: WriteFile() failed ec=%d\n", w32_errno);              log_debug ("passphrase_cb: WriteFile() failed ec=%d\n", w32_errno);
423          return 0;          return 0;
424      }      }
425    
426      WriteFile (hd, c->pwd, strlen (c->pwd), &n, NULL);      if (!WriteFile (hd, c->pwd, strlen (c->pwd), &n, NULL))
427      WriteFile (hd, "\n", 1, &n, NULL);          log_debug ("passphrase_cb: WriteFile() failed ec=%d\n", w32_errno);
428        if (!WriteFile (hd, "\n", 1, &n, NULL))
429            log_debug ("passphrase_cb: WriteFile() failed ec=%d\n", w32_errno);
430      return 0;      return 0;
431  }  }
432    
# Line 418  set_gpg_passphrase_cb (passphrase_cb_s * Line 447  set_gpg_passphrase_cb (passphrase_cb_s *
447      cb->pwd_init = 1;      cb->pwd_init = 1;
448      free_if_alloc (cb->title);      free_if_alloc (cb->title);
449      cb->title = m_strdup (title);      cb->title = m_strdup (title);
     if (!cb->title)  
         BUG (NULL);  
450      gpgme_set_passphrase_cb (ctx, passphrase_cb, cb);      gpgme_set_passphrase_cb (ctx, passphrase_cb, cb);
451      cb->gpg = ctx;      cb->gpg = ctx;
452  }  }
453    
454    
455  /* Release a passphrase callback @ctx. */  /* Release the gpg recipient list. */
456  void  void
457  release_gpg_passphrase_cb (passphrase_cb_s *ctx)  release_gpg_recipients (gpgme_recipient_t *recipients)
458  {  {
459      gpgme_recipient_t r, n;      gpgme_recipient_t r, n;
460    
461      if (!ctx)      r = *recipients;
462          return;      while (r != NULL) {
     sfree_if_alloc (ctx->pwd);  
     free_if_alloc (ctx->title);  
     r = ctx->recipients;  
     while (r) {  
463          n = r->next;          n = r->next;
464          safe_free (r->keyid);          safe_free (r->keyid);
465          safe_free (r);          safe_free (r);
466          r = n;          r = n;
467      }      }
468        *recipients = NULL;
469    }
470    
471    
472    
473    /* Release a passphrase callback @ctx. */
474    void
475    release_gpg_passphrase_cb (passphrase_cb_s *ctx)
476    {
477        if (!ctx)
478            return;
479        sfree_if_alloc (ctx->pwd);
480        free_if_alloc (ctx->title);
481        release_gpg_recipients (&ctx->recipients);
482  }  }
483    
484    

Legend:
Removed from v.47  
changed lines
  Added in v.214

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26