/[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 85 by twoaday, Fri Nov 18 07:20:40 2005 UTC revision 271 by twoaday, Sun Nov 5 08:57:45 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-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 34  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    
 const char* get_symkey_algo (int algo);  
44    
45  #define item_ctrl_id( cmd ) \  /* Return the control ID dependent on the mode (sign or decrypt). */
46    #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    const char* get_symkey_algo (int algo);
53    void ListBox_AddString_utf8 (HWND lb, const char *txt);
54    
55    
56  /* Overwrite passphrase and free memory. */  /* Overwrite passphrase and free memory. */
57  static void  static void
58  burn_passphrase (char **pwd)  burn_passphrase (char **pwd)
59  {  {
60      char *pass = *pwd;      char *pass = *pwd;
61    
62      wipememory (pass, strlen (pass));      wipememory (pass, strlen (pass));
63      delete []pass;      delete []pass;
64      *pwd = NULL;      *pwd = NULL;
# Line 68  passphrase_callback_proc (HWND dlg, UINT Line 72  passphrase_callback_proc (HWND dlg, UINT
72      static passphrase_cb_s *c;      static passphrase_cb_s *c;
73      gpgme_decrypt_result_t res=NULL;      gpgme_decrypt_result_t res=NULL;
74      gpgme_sign_result_t res_sig=NULL;      gpgme_sign_result_t res_sig=NULL;
     gpgme_key_t key;  
75      gpgme_recipient_t recip=NULL, r;      gpgme_recipient_t recip=NULL, r;
76        winpt_key_s key;
77      void *item;      void *item;
78      const char *id;      const char *id;
79      char *info;      char *info;
80      int n;      int n;
81    
82      switch (msg) {      switch (msg) {
83        case WM_ACTIVATE:
84            safe_edit_control_init (dlg, item_ctrl_id (c->gpg_cmd));
85            break;
86    
87        case WM_DESTROY:
88            safe_edit_control_free (dlg, item_ctrl_id (c->gpg_cmd));
89            break;
90    
91      case WM_INITDIALOG:      case WM_INITDIALOG:
92          c = (passphrase_cb_s *)lparam;          c = (passphrase_cb_s *)lparam;
93          if (!c)          if (!c)
94              BUG (0);              BUG (0);
95          SetDlgItemText (dlg, IDC_DECRYPT_HIDE, _("&Hide Typing"));          SetDlgItemText (dlg, IDCANCEL, _("&Cancel"));
96          SetWindowText (dlg, c->title);          SetWindowText (dlg, c->title);
97          if (c->gpg_cmd == GPG_CMD_DECRYPT) {          if (c->gpg_cmd == GPG_CMD_DECRYPT) {
98                SetDlgItemText (dlg, IDC_DECRYPT_HIDE, _("&Hide Typing"));
99              SetDlgItemText (dlg, IDC_DECRYPT_LISTINF,              SetDlgItemText (dlg, IDC_DECRYPT_LISTINF,
100                              _("Encrypted with the following public key(s)"));                              _("Encrypted with the following public key(s)"));
101              CheckDlgButton (dlg, IDC_DECRYPT_HIDE, BST_CHECKED);              CheckDlgButton (dlg, IDC_DECRYPT_HIDE, BST_CHECKED);
102          }          }
103          else if (c->gpg_cmd == GPG_CMD_SIGN)          else if (c->gpg_cmd == GPG_CMD_SIGN) {
104                SetDlgItemText (dlg, IDC_DECRYPT_SIGN_HIDE, _("&Hide Typing"));
105              CheckDlgButton (dlg, IDC_DECRYPT_SIGN_HIDE, BST_CHECKED);              CheckDlgButton (dlg, IDC_DECRYPT_SIGN_HIDE, BST_CHECKED);
106            }
107            /* Because it depends on the order the keys are stored in the
108               keyring whether res->recipients is complete or not, we also
109               support that the recipients were externally extracted and then
110               we use this list. */
111          if (c->recipients)          if (c->recipients)
112              recip = c->recipients; /* recipients were already extracted. */              recip = c->recipients; /* recipients were already extracted. */
113          else {          else {
             /* XXX: not all ENCRYPT_TO entries are listed here. */  
114              res = gpgme_op_decrypt_result (c->gpg);              res = gpgme_op_decrypt_result (c->gpg);
115              if (res && res->recipients)              if (res && res->recipients)
116                  recip = res->recipients;                  recip = res->recipients;
117          }          }
118          if (recip != NULL && c->gpg_cmd == GPG_CMD_DECRYPT) {          if (recip != NULL && c->gpg_cmd == GPG_CMD_DECRYPT) {
119              for (r = res->recipients; r; r = r->next) {              for (r = recip; r; r = r->next) {
120                  get_pubkey (r->keyid, &key);                  memset (&key, 0, sizeof (key));
121                  if (key) {                  if (!winpt_get_pubkey (r->keyid, &key)) {
122                      char *uid;                      gpgme_user_id_t u = key.ctx->uids;
123                      id = key->uids->name;  
124                        id = u->name;
125                      if (!id)                      if (!id)
126                          id = _("Invalid User ID");                          id = _("Invalid User ID");
127                      uid = utf8_to_wincp (id, strlen (id));                      n = 32+strlen (id)+1+4+strlen (r->keyid)+1;
128                      info = new char [32+strlen (uid)+1 + 4 + strlen (r->keyid)+1                      if (u->email)
129                                       + strlen (key->uids->email)+1];                          n += strlen (u->email)+1;
130                        info = new char [n+1];
131                      if (!info)                      if (!info)
132                          BUG (NULL);                          BUG (NULL);
133                      sprintf (info, "%s <%s> (%s, 0x%s)", uid, key->uids->email,                      if (!u->email || strlen (u->email) < 1)
134                               get_key_pubalgo (r->pubkey_algo), r->keyid+8);                          sprintf (info, "%s (%s, 0x%s)", id,
135                      free (uid);                                   get_key_pubalgo (r->pubkey_algo), r->keyid+8);
136                                            else
137                            sprintf (info, "%s <%s> (%s, 0x%s)", id, u->email,
138                                     get_key_pubalgo (r->pubkey_algo), r->keyid+8);
139                  }                  }
140                  else {                  else {
141                      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 144  passphrase_callback_proc (HWND dlg, UINT
144                      sprintf (info, _("Unknown key ID (%s, 0x%s)"),                      sprintf (info, _("Unknown key ID (%s, 0x%s)"),
145                               get_key_pubalgo (r->pubkey_algo), r->keyid+8);                               get_key_pubalgo (r->pubkey_algo), r->keyid+8);
146                  }                  }
147                  listbox_add_string (GetDlgItem (dlg, IDC_DECRYPT_LIST), info);                  ListBox_AddString_utf8 (GetDlgItem (dlg, IDC_DECRYPT_LIST), info);
148                  free_if_alloc (info);                  free_if_alloc (info);
149                    winpt_release_pubkey (&key);
150              }              }
151          }          }
152          else if (c->gpg_cmd == GPG_CMD_DECRYPT)          else if (c->gpg_cmd == GPG_CMD_DECRYPT)
# Line 149  passphrase_callback_proc (HWND dlg, UINT Line 172  passphrase_callback_proc (HWND dlg, UINT
172                  SetDlgItemText (dlg, IDC_DECRYPT_MSG, c->info);                  SetDlgItemText (dlg, IDC_DECRYPT_MSG, c->info);
173          }          }
174          else {          else {
175              SetFocus( GetDlgItem (dlg, IDC_DECRYPT_SIGN_PWD));              SetFocus (GetDlgItem (dlg, IDC_DECRYPT_SIGN_PWD));
176              SetDlgItemText (dlg, IDC_DECRYPT_SIGN_MSG, c->info);              SetDlgItemText (dlg, IDC_DECRYPT_SIGN_MSG, c->info);
177          }          }
178          center_window (dlg, NULL);          center_window (dlg, NULL);
179          SetForegroundWindow (dlg);          SetForegroundWindow (dlg);
         set_active_window (dlg);  
180          return FALSE;          return FALSE;
181    
         case WM_SYSCOMMAND:  
             if (LOWORD (wparam) == SC_CLOSE) {  
                 SetDlgItemText (dlg, item_ctrl_id (c->gpg_cmd), "");  
                 c->cancel = 1;  
                 EndDialog (dlg, TRUE);  
             }  
             break;  
   
182          case WM_COMMAND:          case WM_COMMAND:
183              switch (HIWORD (wparam)) {              switch (HIWORD (wparam)) {
184              case BN_CLICKED:              case BN_CLICKED:
# Line 193  passphrase_callback_proc (HWND dlg, UINT Line 207  passphrase_callback_proc (HWND dlg, UINT
207                      strcpy (c->pwd, "");                      strcpy (c->pwd, "");
208                  }                  }
209                  else {                  else {
210                      c->pwd = new char[n+2];                      char *p = new char[n+2];
211                      if (!c->pwd)                      if (!p)
212                          BUG (NULL);                          BUG (NULL);
213                      GetDlgItemText (dlg, item_ctrl_id (c->gpg_cmd), c->pwd, n+1);                      /* Get the passphrase and convert it utf8.
214                           This does not just the us-ascii charset. */
215                        SafeGetDlgItemText (dlg, item_ctrl_id (c->gpg_cmd), p, n+1);
216                        c->pwd = native_to_utf8 (p);
217                        sfree_if_alloc (p);
218                  }                  }
219                  res = gpgme_op_decrypt_result (c->gpg);                  res = gpgme_op_decrypt_result (c->gpg);
220                  if (!res)                  if (!res)
221                      res_sig = gpgme_op_sign_result (c->gpg);                                  res_sig = gpgme_op_sign_result (c->gpg);
222                  if (reg_prefs.cache_time > 0 && !c->is_card &&                  if (!c->is_card && reg_prefs.cache_time > 0 &&
223                      (res || res_sig)) {                      (res || res_sig)) {
224                      if (agent_get_cache (c->keyid, &item))                                            if (agent_get_cache (c->keyid, &item))
225                          agent_unlock_cache_entry (&item);                          agent_unlock_cache_entry (&item);
226                      else                      else
227                          agent_put_cache (c->keyid, c->pwd, reg_prefs.cache_time);                          agent_put_cache (c->keyid, c->pwd, reg_prefs.cache_time);
# Line 216  passphrase_callback_proc (HWND dlg, UINT Line 234  passphrase_callback_proc (HWND dlg, UINT
234                  SetDlgItemText (dlg, item_ctrl_id (c->gpg_cmd), "");                  SetDlgItemText (dlg, item_ctrl_id (c->gpg_cmd), "");
235                  c->cancel = 1;                  c->cancel = 1;
236                  EndDialog (dlg, FALSE);                  EndDialog (dlg, FALSE);
237                  return FALSE;                  return TRUE;
238              }              }
239              break;              break;
240      }      }
# Line 255  parse_gpg_description (const char *uid_h Line 273  parse_gpg_description (const char *uid_h
273      gpgme_pubkey_algo_t algo;      gpgme_pubkey_algo_t algo;
274      char usedkey[16+1];      char usedkey[16+1];
275      char mainkey[16+1];      char mainkey[16+1];
276      char *uid, *p;      char *p, *uid;
277      int n=0;      int n=0;
278    
279      algo = (gpgme_pubkey_algo_t)0;      algo = (gpgme_pubkey_algo_t)0;
# Line 276  parse_gpg_description (const char *uid_h Line 294  parse_gpg_description (const char *uid_h
294      uid_hint += 16; /* skip keyid */      uid_hint += 16; /* skip keyid */
295      uid_hint += 1;  /* space */      uid_hint += 1;  /* space */
296    
297      uid = utf8_to_wincp (uid_hint, strlen (uid_hint));      uid = utf8_to_native (uid_hint);
   
298      if (strcmp (usedkey, mainkey))      if (strcmp (usedkey, mainkey))
299          _snprintf (desc, size-1,          _snprintf (desc, size-1,
300                     _("You need a passphrase to unlock the secret key for\n"                     _("You need a passphrase to unlock the secret key for user:\n"
301                       "user: \"%s\"\n"                       "\"%s\"\n"
302                       "%s key, ID %s (main key ID %s)\n"),                       "%s key, ID 0x%s (main key ID 0x%s)\n"),
303                     uid, get_key_pubalgo (algo), usedkey+8, mainkey+8);                     uid, get_key_pubalgo (algo), usedkey+8, mainkey+8);
304      else if (!strcmp (usedkey, mainkey))      else if (!strcmp (usedkey, mainkey))
305          _snprintf (desc, size-1,          _snprintf (desc, size-1,
306                     _("You need a passphrase to unlock the secret key for\n"                     _("You need a passphrase to unlock the secret key for user:\n"
307                       "user: \"%s\"\n"                       "\"%s\"\n"
308                       "%s key, ID %s\n"),                       "%s key, ID 0x%s\n"),
309                       uid, get_key_pubalgo (algo), usedkey+8);                       uid, get_key_pubalgo (algo), usedkey+8);
310      free (uid);      safe_free (uid);
311      return 0;      return 0;
312  }  }
313    
# Line 305  extract_serial_no (const char *id) Line 322  extract_serial_no (const char *id)
322      p = strchr (id, '/');      p = strchr (id, '/');
323      if (!p) {      if (!p) {
324          log_debug ("extract_serial_no: error '%s'\r\n", id);          log_debug ("extract_serial_no: error '%s'\r\n", id);
325          return NULL;          return "";
326      }      }
327        memset (buf, 0, sizeof (buf));
328      strncpy (buf, id+(p-id)-6, 6);      strncpy (buf, id+(p-id)-6, 6);
329      return buf;      return buf;
330  }  }
# Line 326  passphrase_cb (void *hook, const char *u Line 344  passphrase_cb (void *hook, const char *u
344      int rc = 0;      int rc = 0;
345    
346      if (!c) {      if (!c) {
347          log_debug ("passphrase_cb: error '!c'\r\n");          log_debug ("passphrase_cb: error no valid callback\r\n");
348          return gpg_error (GPG_ERR_INV_ARG);          return gpg_error (GPG_ERR_INV_ARG);
349      }      }
350      c->bad_pwd = prev_was_bad? 1 : 0;      c->bad_pwd = prev_was_bad? 1 : 0;
# Line 380  passphrase_cb (void *hook, const char *u Line 398  passphrase_cb (void *hook, const char *u
398          }          }
399          else if (uid_hint)          else if (uid_hint)
400              parse_gpg_description (uid_hint, passphrase_info,              parse_gpg_description (uid_hint, passphrase_info,
401                                     c->info, sizeof c->info - 1);                                     c->info, sizeof (c->info) - 1);
402          if (c->gpg_cmd == GPG_CMD_DECRYPT) {          if (c->gpg_cmd == GPG_CMD_DECRYPT) {
403              rc = DialogBoxParam (glob_hinst, (LPCSTR)IDD_WINPT_DECRYPT,              rc = DialogBoxParam (glob_hinst, (LPCSTR)IDD_WINPT_DECRYPT,
404                                   (HWND)c->hwnd, passphrase_callback_proc,                                   (HWND)c->hwnd, passphrase_callback_proc,
# Line 394  passphrase_cb (void *hook, const char *u Line 412  passphrase_cb (void *hook, const char *u
412          if (rc == -1) {          if (rc == -1) {
413              if (!WriteFile (hd, "\n", 1, &n, NULL))              if (!WriteFile (hd, "\n", 1, &n, NULL))
414                  log_debug ("passphrase_cb: WriteFile() failed ec=%d\n", w32_errno);                  log_debug ("passphrase_cb: WriteFile() failed ec=%d\n", w32_errno);
415                log_debug ("passphrase_cb: could not create dialog box\n");
416              return 0;              return 0;
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;
# Line 428  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    
485  /* Simple check to measure passphrase (@pass) quality.  /* _Simple_ check to measure passphrase (@pass) quality.
486     Return value: 0 on success. */     Return value: 0 on success. */
487  int  int
488  check_passwd_quality (const char *pass, int strict)  check_passwd_quality (const char *pass, int strict)
489  {  {
490      int i, nd=0, nc=0, n;      int i, nd=0, nc=0, n;
491    
492        /* A good passphrase should be at least 8 characters. */
493      n = strlen (pass);      n = strlen (pass);
494      if (n < 8)      if (n < 8)
495          return -1;          return -1;
496    
497      for (i=0; i < n; i++) {      for (i=0; i < n; i++) {
498          if (isdigit (pass[i]))          if (isdigit (pass[i]))
499              nd++;              nd++;
500          if (isalpha (pass[i]))          if (isalpha (pass[i]))
501              nc++;              nc++;
502      }      }
503    
504      /* check that the passphrase contains letters and numbers. */      /* Check that the passphrase contains letters and numbers. */
505      if (nd == n || nc == n)      if (nd == n || nc == n)
506          return -1;          return -1;
507    

Legend:
Removed from v.85  
changed lines
  Added in v.271

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26