/[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 105 by twoaday, Wed Nov 30 10:22:00 2005 UTC revision 278 by twoaday, Mon Jan 15 22:02:04 2007 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-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 13  Line 13 
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * General Public License for more details.   * General Public License for more details.
  *  
  * You should have received a copy of the GNU General Public License  
  * along with WinPT; if not, write to the Free Software Foundation,  
  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA  
16   */   */
   
17  #ifdef HAVE_CONFIG_H  #ifdef HAVE_CONFIG_H
18  #include <config.h>  #include <config.h>
19  #endif  #endif
# Line 34  Line 29 
29  #include "wptCommonCtl.h"  #include "wptCommonCtl.h"
30  #include "wptContext.h"  #include "wptContext.h"
31  #include "wptDlgs.h"  #include "wptDlgs.h"
 #include "wptUTF8.h"  
32  #include "wptErrors.h"  #include "wptErrors.h"
33  #include "wptTypes.h"  #include "wptTypes.h"
34  #include "wptKeylist.h"  #include "wptKeylist.h"
35  #include "wptAgent.h"  #include "wptAgent.h"
36  #include "wptRegistry.h"  #include "wptRegistry.h"
37    #include "wptUTF8.h"
38    #include "StringBuffer.h"
39    
 const char* get_symkey_algo (int algo);  
40    
41  #define item_ctrl_id( cmd ) \  /* Return the control ID dependent on the mode (sign or decrypt). */
42    #define item_ctrl_id(cmd) \
43      ((cmd) == GPG_CMD_DECRYPT? IDC_DECRYPT_PWD : IDC_DECRYPT_SIGN_PWD)      ((cmd) == GPG_CMD_DECRYPT? IDC_DECRYPT_PWD : IDC_DECRYPT_SIGN_PWD)
44    
45  #define item_ctrl_id2(cmd) \  #define item_ctrl_id2(cmd) \
46      ((cmd) == GPG_CMD_DECRYPT? IDC_DECRYPT_HIDE : IDC_DECRYPT_SIGN_HIDE)      ((cmd) == GPG_CMD_DECRYPT? IDC_DECRYPT_HIDE : IDC_DECRYPT_SIGN_HIDE)
47    
48    const char* get_symkey_algo (int algo);
49    void ListBox_AddString_utf8 (HWND lb, const char *txt);
50    
51    
52  /* Overwrite passphrase and free memory. */  /* Overwrite passphrase and free memory. */
53  static void  static void
54  burn_passphrase (char **pwd)  burn_passphrase (char **pwd)
55  {  {
56      char *pass = *pwd;      char *pass = *pwd;
57    
58      wipememory (pass, strlen (pass));      wipememory (pass, strlen (pass));
59      delete []pass;      delete []pass;
60      *pwd = NULL;      *pwd = NULL;
# Line 68  passphrase_callback_proc (HWND dlg, UINT Line 68  passphrase_callback_proc (HWND dlg, UINT
68      static passphrase_cb_s *c;      static passphrase_cb_s *c;
69      gpgme_decrypt_result_t res=NULL;      gpgme_decrypt_result_t res=NULL;
70      gpgme_sign_result_t res_sig=NULL;      gpgme_sign_result_t res_sig=NULL;
     gpgme_key_t key;  
71      gpgme_recipient_t recip=NULL, r;      gpgme_recipient_t recip=NULL, r;
72        winpt_key_s key;
73      void *item;      void *item;
     const char *id;  
     char *info;  
74      int n;      int n;
75    
76      switch (msg) {      switch (msg) {
77        case WM_ACTIVATE:
78            safe_edit_control_init (dlg, item_ctrl_id (c->gpg_cmd));
79            break;
80    
81        case WM_DESTROY:
82            safe_edit_control_free (dlg, item_ctrl_id (c->gpg_cmd));
83            break;
84    
85      case WM_INITDIALOG:      case WM_INITDIALOG:
86          c = (passphrase_cb_s *)lparam;          c = (passphrase_cb_s *)lparam;
87          if (!c)          if (!c)
88              BUG (0);              BUG (0);
89          SetDlgItemText (dlg, IDCANCEL, _("&Cancel"));          SetDlgItemText (dlg, IDCANCEL, _("&Cancel"));
         SetDlgItemText (dlg, IDC_DECRYPT_HIDE, _("&Hide Typing"));  
90          SetWindowText (dlg, c->title);          SetWindowText (dlg, c->title);
91          if (c->gpg_cmd == GPG_CMD_DECRYPT) {          if (c->gpg_cmd == GPG_CMD_DECRYPT) {
92                SetDlgItemText (dlg, IDC_DECRYPT_HIDE, _("&Hide Typing"));
93              SetDlgItemText (dlg, IDC_DECRYPT_LISTINF,              SetDlgItemText (dlg, IDC_DECRYPT_LISTINF,
94                              _("Encrypted with the following public key(s)"));                              _("Encrypted with the following public key(s)"));
95              CheckDlgButton (dlg, IDC_DECRYPT_HIDE, BST_CHECKED);              CheckDlgButton (dlg, IDC_DECRYPT_HIDE, BST_CHECKED);
# Line 92  passphrase_callback_proc (HWND dlg, UINT Line 98  passphrase_callback_proc (HWND dlg, UINT
98              SetDlgItemText (dlg, IDC_DECRYPT_SIGN_HIDE, _("&Hide Typing"));              SetDlgItemText (dlg, IDC_DECRYPT_SIGN_HIDE, _("&Hide Typing"));
99              CheckDlgButton (dlg, IDC_DECRYPT_SIGN_HIDE, BST_CHECKED);              CheckDlgButton (dlg, IDC_DECRYPT_SIGN_HIDE, BST_CHECKED);
100          }          }
101            /* Because it depends on the order the keys are stored in the
102               keyring whether res->recipients is complete or not, we also
103               support that the recipients were externally extracted and then
104               we use this list. */
105          if (c->recipients)          if (c->recipients)
106              recip = c->recipients; /* recipients were already extracted. */              recip = c->recipients; /* recipients were already extracted. */
107          else {          else {
             /* XXX: not all ENCRYPT_TO entries are listed here. */  
108              res = gpgme_op_decrypt_result (c->gpg);              res = gpgme_op_decrypt_result (c->gpg);
109              if (res && res->recipients)              if (res && res->recipients)
110                  recip = res->recipients;                  recip = res->recipients;
111          }          }
112          if (recip != NULL && c->gpg_cmd == GPG_CMD_DECRYPT) {          if (recip != NULL && c->gpg_cmd == GPG_CMD_DECRYPT) {
113              for (r = res->recipients; r; r = r->next) {              StringBuffer inf;
114                  get_pubkey (r->keyid, &key);  
115                  if (key) {              for (r = recip; r; r = r->next) {
116                      char *uid;                  memset (&key, 0, sizeof (key));
117                      id = key->uids->name;                  if (!winpt_get_pubkey (r->keyid, &key)) {
118                      if (!id)                      gpgme_user_id_t u = key.ctx->uids;
                         id = _("Invalid User ID");  
                     uid = utf8_to_wincp (id, strlen (id));  
                     info = new char [32+strlen (uid)+1 + 4 + strlen (r->keyid)+1  
                                      + strlen (key->uids->email)+1];  
                     if (!info)  
                         BUG (NULL);  
                     sprintf (info, "%s <%s> (%s, 0x%s)", uid, key->uids->email,  
                              get_key_pubalgo (r->pubkey_algo), r->keyid+8);  
                     free (uid);  
119                                            
120                        inf = (u->name? u->name : _("Invalid User ID"));
121                        if (u->email != NULL && strlen (u->email) > 1)
122                            inf = inf + " <" + u->email + ">";
123                        inf = inf + " (" + get_key_pubalgo (r->pubkey_algo);
124                        inf = inf + ", 0x" + (r->keyid+8) + ")";
125                  }                  }
126                  else {                  else {
127                      info = new char [32 + strlen (r->keyid)+1 + 4];                      inf = _("Unknown key ID");
128                      if (!info)                      inf = inf + " (" + get_key_pubalgo (r->pubkey_algo);
129                          BUG (NULL);                      inf = inf + ", 0x" + (r->keyid+8) + ")";
                     sprintf (info, _("Unknown key ID (%s, 0x%s)"),  
                              get_key_pubalgo (r->pubkey_algo), r->keyid+8);  
130                  }                  }
131                  listbox_add_string (GetDlgItem (dlg, IDC_DECRYPT_LIST), info);                  ListBox_AddString_utf8 (GetDlgItem (dlg, IDC_DECRYPT_LIST),
132                  free_if_alloc (info);                                          inf.getBuffer ());
133                    winpt_release_pubkey (&key);
134              }              }
135          }          }
136          else if (c->gpg_cmd == GPG_CMD_DECRYPT)          else if (c->gpg_cmd == GPG_CMD_DECRYPT)
# Line 138  passphrase_callback_proc (HWND dlg, UINT Line 142  passphrase_callback_proc (HWND dlg, UINT
142          if (c->gpg_cmd == GPG_CMD_DECRYPT) {          if (c->gpg_cmd == GPG_CMD_DECRYPT) {
143              SetFocus (GetDlgItem (dlg, IDC_DECRYPT_PWD));              SetFocus (GetDlgItem (dlg, IDC_DECRYPT_PWD));
144              if (res && !res->recipients) {              if (res && !res->recipients) {
145                  const char *s = _("Symmetric encryption.\n"                  StringBuffer sinf;
146                                    "%s encrypted data.");  
147                  const char *alg = get_symkey_algo (c->sym.sym_algo);                  sinf = _("Symmetric encryption.");
148                  info = new char[strlen (s) + strlen (alg) + 2];                  sinf = sinf + "\n" + get_symkey_algo (c->sym.sym_algo);
149                  if (!info)                  sinf = sinf + " " + _("encrypted data.");
150                      BUG (NULL);                  SetDlgItemText (dlg, IDC_DECRYPT_MSG, sinf.getBuffer ());
                 sprintf (info, s, alg);  
                 SetDlgItemText (dlg, IDC_DECRYPT_MSG, info);  
                 free_if_alloc (info);  
151              }              }
152              else              else
153                  SetDlgItemText (dlg, IDC_DECRYPT_MSG, c->info);                  SetDlgItemText (dlg, IDC_DECRYPT_MSG, c->info);
154          }          }
155          else {          else {
156              SetFocus( GetDlgItem (dlg, IDC_DECRYPT_SIGN_PWD));              SetFocus (GetDlgItem (dlg, IDC_DECRYPT_SIGN_PWD));
157              SetDlgItemText (dlg, IDC_DECRYPT_SIGN_MSG, c->info);              SetDlgItemText (dlg, IDC_DECRYPT_SIGN_MSG, c->info);
158          }          }
159          center_window (dlg, NULL);          center_window (dlg, NULL);
160          SetForegroundWindow (dlg);          SetForegroundWindow (dlg);
         set_active_window (dlg);  
161          return FALSE;          return FALSE;
162    
         case WM_SYSCOMMAND:  
             if (LOWORD (wparam) == SC_CLOSE) {  
                 SetDlgItemText (dlg, item_ctrl_id (c->gpg_cmd), "");  
                 c->cancel = 1;  
                 EndDialog (dlg, TRUE);  
             }  
             break;  
   
163          case WM_COMMAND:          case WM_COMMAND:
164              switch (HIWORD (wparam)) {              switch (HIWORD (wparam)) {
165              case BN_CLICKED:              case BN_CLICKED:
# Line 196  passphrase_callback_proc (HWND dlg, UINT Line 188  passphrase_callback_proc (HWND dlg, UINT
188                      strcpy (c->pwd, "");                      strcpy (c->pwd, "");
189                  }                  }
190                  else {                  else {
191                      c->pwd = new char[n+2];                      char *p = new char[n+2];
192                      if (!c->pwd)                      if (!p)
193                          BUG (NULL);                          BUG (NULL);
194                      GetDlgItemText (dlg, item_ctrl_id (c->gpg_cmd), c->pwd, n+1);                      /* Get the passphrase and convert it utf8.
195                           This does not just the us-ascii charset. */
196                        SafeGetDlgItemText (dlg, item_ctrl_id (c->gpg_cmd), p, n+1);
197                        c->pwd = native_to_utf8 (p);
198                        sfree_if_alloc (p);
199                  }                  }
200                  res = gpgme_op_decrypt_result (c->gpg);                  res = gpgme_op_decrypt_result (c->gpg);
201                  if (!res)                  if (!res)
202                      res_sig = gpgme_op_sign_result (c->gpg);                                  res_sig = gpgme_op_sign_result (c->gpg);
203                  if (reg_prefs.cache_time > 0 && !c->is_card &&                  if (!c->is_card && reg_prefs.cache_time > 0 &&
204                      (res || res_sig)) {                      (res || res_sig)) {
205                      if (agent_get_cache (c->keyid, &item))                                            if (agent_get_cache (c->keyid, &item))
206                          agent_unlock_cache_entry (&item);                          agent_unlock_cache_entry (&item);
207                      else                      else
208                          agent_put_cache (c->keyid, c->pwd, reg_prefs.cache_time);                          agent_put_cache (c->keyid, c->pwd, reg_prefs.cache_time);
# Line 219  passphrase_callback_proc (HWND dlg, UINT Line 215  passphrase_callback_proc (HWND dlg, UINT
215                  SetDlgItemText (dlg, item_ctrl_id (c->gpg_cmd), "");                  SetDlgItemText (dlg, item_ctrl_id (c->gpg_cmd), "");
216                  c->cancel = 1;                  c->cancel = 1;
217                  EndDialog (dlg, FALSE);                  EndDialog (dlg, FALSE);
218                  return FALSE;                  return TRUE;
219              }              }
220              break;              break;
221      }      }
# Line 258  parse_gpg_description (const char *uid_h Line 254  parse_gpg_description (const char *uid_h
254      gpgme_pubkey_algo_t algo;      gpgme_pubkey_algo_t algo;
255      char usedkey[16+1];      char usedkey[16+1];
256      char mainkey[16+1];      char mainkey[16+1];
257      char *uid, *p;      char *p, *uid;
258      int n=0;      int n=0;
259    
260      algo = (gpgme_pubkey_algo_t)0;      algo = (gpgme_pubkey_algo_t)0;
# Line 279  parse_gpg_description (const char *uid_h Line 275  parse_gpg_description (const char *uid_h
275      uid_hint += 16; /* skip keyid */      uid_hint += 16; /* skip keyid */
276      uid_hint += 1;  /* space */      uid_hint += 1;  /* space */
277    
278      uid = utf8_to_wincp (uid_hint, strlen (uid_hint));      uid = utf8_to_native (uid_hint);
   
279      if (strcmp (usedkey, mainkey))      if (strcmp (usedkey, mainkey))
280          _snprintf (desc, size-1,          _snprintf (desc, size-1,
281                     _("You need a passphrase to unlock the secret key for\n"                     _("You need a passphrase to unlock the secret key for user:\n"
282                       "user: \"%s\"\n"                       "\"%s\"\n"
283                       "%s key, ID %s (main key ID %s)\n"),                       "%s key, ID 0x%s (main key ID 0x%s)\n"),
284                     uid, get_key_pubalgo (algo), usedkey+8, mainkey+8);                     uid, get_key_pubalgo (algo), usedkey+8, mainkey+8);
285      else if (!strcmp (usedkey, mainkey))      else if (!strcmp (usedkey, mainkey))
286          _snprintf (desc, size-1,          _snprintf (desc, size-1,
287                     _("You need a passphrase to unlock the secret key for\n"                     _("You need a passphrase to unlock the secret key for user:\n"
288                       "user: \"%s\"\n"                       "\"%s\"\n"
289                       "%s key, ID %s\n"),                       "%s key, ID 0x%s\n"),
290                       uid, get_key_pubalgo (algo), usedkey+8);                       uid, get_key_pubalgo (algo), usedkey+8);
291      free (uid);      safe_free (uid);
292      return 0;      return 0;
293  }  }
294    
# Line 308  extract_serial_no (const char *id) Line 303  extract_serial_no (const char *id)
303      p = strchr (id, '/');      p = strchr (id, '/');
304      if (!p) {      if (!p) {
305          log_debug ("extract_serial_no: error '%s'\r\n", id);          log_debug ("extract_serial_no: error '%s'\r\n", id);
306          return NULL;          return "";
307      }      }
308        memset (buf, 0, sizeof (buf));
309      strncpy (buf, id+(p-id)-6, 6);      strncpy (buf, id+(p-id)-6, 6);
310      return buf;      return buf;
311  }  }
# Line 329  passphrase_cb (void *hook, const char *u Line 325  passphrase_cb (void *hook, const char *u
325      int rc = 0;      int rc = 0;
326    
327      if (!c) {      if (!c) {
328          log_debug ("passphrase_cb: error '!c'\r\n");          log_debug ("passphrase_cb: error no valid callback\r\n");
329          return gpg_error (GPG_ERR_INV_ARG);          return gpg_error (GPG_ERR_INV_ARG);
330      }      }
331      c->bad_pwd = prev_was_bad? 1 : 0;      c->bad_pwd = prev_was_bad? 1 : 0;
# Line 376  passphrase_cb (void *hook, const char *u Line 372  passphrase_cb (void *hook, const char *u
372              const char *s=passphrase_info;              const char *s=passphrase_info;
373              while (s && *s && *s != 'D')              while (s && *s && *s != 'D')
374                  s++;                  s++;
375              _snprintf (c->info, sizeof c->info-1,              _snprintf (c->info, sizeof (c->info)-1,
376                         _("Please enter the PIN to unlock your secret card key\n"                         _("Please enter the PIN to unlock your secret card key\n"
377                           "Card: %s"), extract_serial_no (s));                           "Card: %s"), extract_serial_no (s));
378              c->is_card = 1;              c->is_card = 1;
379          }          }
380          else if (uid_hint)          else if (uid_hint)
381              parse_gpg_description (uid_hint, passphrase_info,              parse_gpg_description (uid_hint, passphrase_info,
382                                     c->info, sizeof c->info - 1);                                     c->info, sizeof (c->info) - 1);
383          if (c->gpg_cmd == GPG_CMD_DECRYPT) {          if (c->gpg_cmd == GPG_CMD_DECRYPT) {
384              rc = DialogBoxParam (glob_hinst, (LPCSTR)IDD_WINPT_DECRYPT,              rc = DialogBoxParam (glob_hinst, (LPCSTR)IDD_WINPT_DECRYPT,
385                                   (HWND)c->hwnd, passphrase_callback_proc,                                   (HWND)c->hwnd, passphrase_callback_proc,
# Line 397  passphrase_cb (void *hook, const char *u Line 393  passphrase_cb (void *hook, const char *u
393          if (rc == -1) {          if (rc == -1) {
394              if (!WriteFile (hd, "\n", 1, &n, NULL))              if (!WriteFile (hd, "\n", 1, &n, NULL))
395                  log_debug ("passphrase_cb: WriteFile() failed ec=%d\n", w32_errno);                  log_debug ("passphrase_cb: WriteFile() failed ec=%d\n", w32_errno);
396                log_debug ("passphrase_cb: could not create dialog box\n");
397              return 0;              return 0;
398          }          }
399          c->pwd_init = 0;          c->pwd_init = 0;
400      }      }
401      if (c->cancel) {      if (c->cancel || !c->pwd) {
402          if (!WriteFile (hd, "\n", 1, &n, NULL))          if (!WriteFile (hd, "\n", 1, &n, NULL))
403              log_debug ("passphrase_cb: WriteFile() failed ec=%d\n", w32_errno);              log_debug ("passphrase_cb: WriteFile() failed ec=%d\n", w32_errno);
404          return 0;          return 0;
# Line 431  set_gpg_passphrase_cb (passphrase_cb_s * Line 428  set_gpg_passphrase_cb (passphrase_cb_s *
428      cb->pwd_init = 1;      cb->pwd_init = 1;
429      free_if_alloc (cb->title);      free_if_alloc (cb->title);
430      cb->title = m_strdup (title);      cb->title = m_strdup (title);
     if (!cb->title)  
         BUG (NULL);  
431      gpgme_set_passphrase_cb (ctx, passphrase_cb, cb);      gpgme_set_passphrase_cb (ctx, passphrase_cb, cb);
432      cb->gpg = ctx;      cb->gpg = ctx;
433  }  }
434    
435    
436  /* Release a passphrase callback @ctx. */  /* Release the gpg recipient list. */
437  void  void
438  release_gpg_passphrase_cb (passphrase_cb_s *ctx)  release_gpg_recipients (gpgme_recipient_t *recipients)
439  {  {
440      gpgme_recipient_t r, n;      gpgme_recipient_t r, n;
441    
442      if (!ctx)      r = *recipients;
443          return;      while (r != NULL) {
     sfree_if_alloc (ctx->pwd);  
     free_if_alloc (ctx->title);  
     r = ctx->recipients;  
     while (r) {  
444          n = r->next;          n = r->next;
445          safe_free (r->keyid);          safe_free (r->keyid);
446          safe_free (r);          safe_free (r);
447          r = n;          r = n;
448      }      }
449        *recipients = NULL;
450    }
451    
452    
453    
454    /* Release a passphrase callback @ctx. */
455    void
456    release_gpg_passphrase_cb (passphrase_cb_s *ctx)
457    {
458        if (!ctx)
459            return;
460        sfree_if_alloc (ctx->pwd);
461        free_if_alloc (ctx->title);
462        release_gpg_recipients (&ctx->recipients);
463  }  }
464    
465    
466  /* Simple check to measure passphrase (@pass) quality.  /* _Simple_ check to measure passphrase (@pass) quality.
467     Return value: 0 on success. */     Return value: 0 on success. */
468  int  int
469  check_passwd_quality (const char *pass, int strict)  check_passwd_quality (const char *pass, int strict)
470  {  {
471      int i, nd=0, nc=0, n;      int i, nd=0, nc=0, n;
472    
473        /* A good passphrase should be at least 8 characters. */
474      n = strlen (pass);      n = strlen (pass);
475      if (n < 8)      if (n < 8)
476          return -1;          return -1;
477    
478      for (i=0; i < n; i++) {      for (i=0; i < n; i++) {
479          if (isdigit (pass[i]))          if (isdigit (pass[i]))
480              nd++;              nd++;
481          if (isalpha (pass[i]))          if (isalpha (pass[i]))
482              nc++;              nc++;
483      }      }
484    
485      /* check that the passphrase contains letters and numbers. */      /* Check that the passphrase contains letters and numbers. */
486      if (nd == n || nc == n)      if (nd == n || nc == n)
487          return -1;          return -1;
488    

Legend:
Removed from v.105  
changed lines
  Added in v.278

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26