/[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 160 by twoaday, Thu Jan 19 09:22:09 2006 UTC revision 217 by twoaday, Mon May 22 14:21:39 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 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    
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 68  passphrase_callback_proc (HWND dlg, UINT Line 70  passphrase_callback_proc (HWND dlg, UINT
70      static passphrase_cb_s *c;      static passphrase_cb_s *c;
71      gpgme_decrypt_result_t res=NULL;      gpgme_decrypt_result_t res=NULL;
72      gpgme_sign_result_t res_sig=NULL;      gpgme_sign_result_t res_sig=NULL;
     gpgme_key_t key;  
73      gpgme_recipient_t recip=NULL, r;      gpgme_recipient_t recip=NULL, r;
74        winpt_key_s key;
75      void *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"));          SetDlgItemText (dlg, IDCANCEL, _("&Cancel"));
         SetDlgItemText (dlg, IDC_DECRYPT_HIDE, _("&Hide Typing"));  
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);
# Line 92  passphrase_callback_proc (HWND dlg, UINT Line 102  passphrase_callback_proc (HWND dlg, UINT
102              SetDlgItemText (dlg, IDC_DECRYPT_SIGN_HIDE, _("&Hide Typing"));              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);                  memset (&key, 0, sizeof (key));
119                  if (key) {                  if (!winpt_get_pubkey (r->keyid, &key)) {
120                      gpgme_user_id_t u = key->uids;                      gpgme_user_id_t u = key.ctx->uids;
                     char *uid;  
121    
122                      id = u->name;                      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;
                     n = 32+strlen (uid)+1+4+strlen (r->keyid)+1;  
126                      if (u->email)                      if (u->email)
127                          n += strlen (u->email)+1;                          n += strlen (u->email)+1;
128                      info = new char [n+1];                      info = new char [n+1];
129                      if (!info)                      if (!info)
130                          BUG (NULL);                          BUG (NULL);
131                      if (!u->email || strlen (u->email) < 1)                      if (!u->email || strlen (u->email) < 1)
132                          sprintf (info, "%s (%s, 0x%s)", uid,                          sprintf (info, "%s (%s, 0x%s)", id,
133                                   get_key_pubalgo (r->pubkey_algo), r->keyid+8);                                   get_key_pubalgo (r->pubkey_algo), r->keyid+8);
134                      else                      else
135                          sprintf (info, "%s <%s> (%s, 0x%s)", uid, u->email,                          sprintf (info, "%s <%s> (%s, 0x%s)", id, u->email,
136                                   get_key_pubalgo (r->pubkey_algo), r->keyid+8);                                   get_key_pubalgo (r->pubkey_algo), r->keyid+8);
                     free (uid);  
137                  }                  }
138                  else {                  else {
139                      info = new char [32 + strlen (r->keyid)+1 + 4];                      info = new char [32 + strlen (r->keyid)+1 + 4];
# Line 132  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                    winpt_release_pubkey (&key);
148              }              }
149          }          }
150          else if (c->gpg_cmd == GPG_CMD_DECRYPT)          else if (c->gpg_cmd == GPG_CMD_DECRYPT)
# Line 159  passphrase_callback_proc (HWND dlg, UINT Line 170  passphrase_callback_proc (HWND dlg, UINT
170                  SetDlgItemText (dlg, IDC_DECRYPT_MSG, c->info);                  SetDlgItemText (dlg, IDC_DECRYPT_MSG, c->info);
171          }          }
172          else {          else {
173              SetFocus( GetDlgItem (dlg, IDC_DECRYPT_SIGN_PWD));              SetFocus (GetDlgItem (dlg, IDC_DECRYPT_SIGN_PWD));
174              SetDlgItemText (dlg, IDC_DECRYPT_SIGN_MSG, c->info);              SetDlgItemText (dlg, IDC_DECRYPT_SIGN_MSG, c->info);
175          }          }
176          center_window (dlg, NULL);          center_window (dlg, NULL);
# Line 205  passphrase_callback_proc (HWND dlg, UINT Line 216  passphrase_callback_proc (HWND dlg, UINT
216                      c->pwd = new char[n+2];                      c->pwd = new char[n+2];
217                      if (!c->pwd)                      if (!c->pwd)
218                          BUG (NULL);                          BUG (NULL);
219                      GetDlgItemText (dlg, item_ctrl_id (c->gpg_cmd), c->pwd, n+1);                      SafeGetDlgItemText (dlg, item_ctrl_id (c->gpg_cmd),
220                                            c->pwd, n+1);
221                  }                  }
222                  res = gpgme_op_decrypt_result (c->gpg);                  res = gpgme_op_decrypt_result (c->gpg);
223                  if (!res)                  if (!res)
# Line 264  parse_gpg_description (const char *uid_h Line 276  parse_gpg_description (const char *uid_h
276      gpgme_pubkey_algo_t algo;      gpgme_pubkey_algo_t algo;
277      char usedkey[16+1];      char usedkey[16+1];
278      char mainkey[16+1];      char mainkey[16+1];
279      char *uid, *p;      char *p, *uid;
280      int n=0;      int n=0;
281    
282      algo = (gpgme_pubkey_algo_t)0;      algo = (gpgme_pubkey_algo_t)0;
# Line 285  parse_gpg_description (const char *uid_h Line 297  parse_gpg_description (const char *uid_h
297      uid_hint += 16; /* skip keyid */      uid_hint += 16; /* skip keyid */
298      uid_hint += 1;  /* space */      uid_hint += 1;  /* space */
299    
300      uid = utf8_to_wincp (uid_hint, strlen (uid_hint));      uid = utf8_to_native (uid_hint);
   
301      if (strcmp (usedkey, mainkey))      if (strcmp (usedkey, mainkey))
302          _snprintf (desc, size-1,          _snprintf (desc, size-1,
303                     _("You need a passphrase to unlock the secret key for\n"                     _("You need a passphrase to unlock the secret key for\n"
# Line 299  parse_gpg_description (const char *uid_h Line 310  parse_gpg_description (const char *uid_h
310                       "user: \"%s\"\n"                       "user: \"%s\"\n"
311                       "%s key, ID %s\n"),                       "%s key, ID %s\n"),
312                       uid, get_key_pubalgo (algo), usedkey+8);                       uid, get_key_pubalgo (algo), usedkey+8);
313      free (uid);      safe_free (uid);
314      return 0;      return 0;
315  }  }
316    
# Line 389  passphrase_cb (void *hook, const char *u Line 400  passphrase_cb (void *hook, const char *u
400          }          }
401          else if (uid_hint)          else if (uid_hint)
402              parse_gpg_description (uid_hint, passphrase_info,              parse_gpg_description (uid_hint, passphrase_info,
403                                     c->info, sizeof c->info - 1);                                     c->info, sizeof (c->info) - 1);
404          if (c->gpg_cmd == GPG_CMD_DECRYPT) {          if (c->gpg_cmd == GPG_CMD_DECRYPT) {
405              rc = DialogBoxParam (glob_hinst, (LPCSTR)IDD_WINPT_DECRYPT,              rc = DialogBoxParam (glob_hinst, (LPCSTR)IDD_WINPT_DECRYPT,
406                                   (HWND)c->hwnd, passphrase_callback_proc,                                   (HWND)c->hwnd, passphrase_callback_proc,
# Line 407  passphrase_cb (void *hook, const char *u Line 418  passphrase_cb (void *hook, const char *u
418          }          }
419          c->pwd_init = 0;          c->pwd_init = 0;
420      }      }
421      if (c->cancel) {      if (c->cancel || !c->pwd) {
422          if (!WriteFile (hd, "\n", 1, &n, NULL))          if (!WriteFile (hd, "\n", 1, &n, NULL))
423              log_debug ("passphrase_cb: WriteFile() failed ec=%d\n", w32_errno);              log_debug ("passphrase_cb: WriteFile() failed ec=%d\n", w32_errno);
424          return 0;          return 0;
# Line 437  set_gpg_passphrase_cb (passphrase_cb_s * Line 448  set_gpg_passphrase_cb (passphrase_cb_s *
448      cb->pwd_init = 1;      cb->pwd_init = 1;
449      free_if_alloc (cb->title);      free_if_alloc (cb->title);
450      cb->title = m_strdup (title);      cb->title = m_strdup (title);
     if (!cb->title)  
         BUG (NULL);  
451      gpgme_set_passphrase_cb (ctx, passphrase_cb, cb);      gpgme_set_passphrase_cb (ctx, passphrase_cb, cb);
452      cb->gpg = ctx;      cb->gpg = ctx;
453  }  }
454    
455    
456  /* Release a passphrase callback @ctx. */  /* Release the gpg recipient list. */
457  void  void
458  release_gpg_passphrase_cb (passphrase_cb_s *ctx)  release_gpg_recipients (gpgme_recipient_t *recipients)
459  {  {
460      gpgme_recipient_t r, n;      gpgme_recipient_t r, n;
461    
462      if (!ctx)      r = *recipients;
463          return;      while (r != NULL) {
     sfree_if_alloc (ctx->pwd);  
     free_if_alloc (ctx->title);  
     r = ctx->recipients;  
     while (r) {  
464          n = r->next;          n = r->next;
465          safe_free (r->keyid);          safe_free (r->keyid);
466          safe_free (r);          safe_free (r);
467          r = n;          r = n;
468      }      }
469        *recipients = NULL;
470    }
471    
472    
473    
474    /* Release a passphrase callback @ctx. */
475    void
476    release_gpg_passphrase_cb (passphrase_cb_s *ctx)
477    {
478        if (!ctx)
479            return;
480        sfree_if_alloc (ctx->pwd);
481        free_if_alloc (ctx->title);
482        release_gpg_recipients (&ctx->recipients);
483  }  }
484    
485    

Legend:
Removed from v.160  
changed lines
  Added in v.217

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26