/[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 36 by werner, Thu Oct 27 15:25:13 2005 UTC revision 255 by twoaday, Tue Aug 1 16:37:23 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 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"
30  #include "wptNLS.h"  #include "wptNLS.h"
31  #include "wptW32API.h"  #include "wptW32API.h"
32  #include "wptVersion.h"  #include "wptVersion.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    
 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
# Line 66  burn_passphrase (char **pwd) Line 68  burn_passphrase (char **pwd)
68  static BOOL CALLBACK  static BOOL CALLBACK
69  passphrase_callback_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)  passphrase_callback_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
70  {      {    
71      static passphrase_cb_s * c;      static passphrase_cb_s *c;
72      gpgme_decrypt_result_t res;      gpgme_decrypt_result_t res=NULL;
73      gpgme_sign_result_t res_sig;      gpgme_sign_result_t res_sig=NULL;
74      gpgme_key_t key;      gpgme_recipient_t recip=NULL, r;
75      gpgme_recipient_t recip, r;      winpt_key_s key;
76      void *ctx = NULL, *item;      void *item;
77      const char *id;      const char *id;
78      char *info;      char *info;
79      int n;      int n;
80    
81      switch (msg) {      switch (msg) {
82        case WM_ACTIVATE:
83            safe_edit_control_init (dlg, item_ctrl_id (c->gpg_cmd));
84            break;
85    
86        case WM_DESTROY:
87            safe_edit_control_free (dlg, item_ctrl_id (c->gpg_cmd));
88            break;
89    
90      case WM_INITDIALOG:      case WM_INITDIALOG:
91          c = (passphrase_cb_s *)lparam;          c = (passphrase_cb_s *)lparam;
92          if (!c)          if (!c)
93              BUG (0);              BUG (0);
94            SetDlgItemText (dlg, IDCANCEL, _("&Cancel"));
95          SetWindowText (dlg, c->title);          SetWindowText (dlg, c->title);
96          if (c->gpg_cmd == GPG_CMD_DECRYPT) {          if (c->gpg_cmd == GPG_CMD_DECRYPT) {
97                SetDlgItemText (dlg, IDC_DECRYPT_HIDE, _("&Hide Typing"));
98              SetDlgItemText (dlg, IDC_DECRYPT_LISTINF,              SetDlgItemText (dlg, IDC_DECRYPT_LISTINF,
99                              _("Encrypted with the following public key(s)"));                              _("Encrypted with the following public key(s)"));
100              CheckDlgButton (dlg, IDC_DECRYPT_HIDE, BST_CHECKED);              CheckDlgButton (dlg, IDC_DECRYPT_HIDE, BST_CHECKED);
101          }          }
102          else if (c->gpg_cmd == GPG_CMD_SIGN)          else if (c->gpg_cmd == GPG_CMD_SIGN) {
103                SetDlgItemText (dlg, IDC_DECRYPT_SIGN_HIDE, _("&Hide Typing"));
104              CheckDlgButton (dlg, IDC_DECRYPT_SIGN_HIDE, BST_CHECKED);              CheckDlgButton (dlg, IDC_DECRYPT_SIGN_HIDE, BST_CHECKED);
105            }
106            /* Because it depends on the order the keys are stored in the
107               keyring whether res->recipients is complete or not, we also
108               support that the recipients were externally extracted and then
109               we use this list. */
110          if (c->recipients)          if (c->recipients)
111              recip = c->recipients; /* recipients were already extracted. */              recip = c->recipients; /* recipients were already extracted. */
112          else {          else {
             /* XXX: not all ENCRYPT_TO entries are listed here. */  
113              res = gpgme_op_decrypt_result (c->gpg);              res = gpgme_op_decrypt_result (c->gpg);
114              if (res && res->recipients)              if (res && res->recipients)
115                  recip = res->recipients;                  recip = res->recipients;
116          }          }
117          if (recip != NULL && c->gpg_cmd == GPG_CMD_DECRYPT) {          if (recip != NULL && c->gpg_cmd == GPG_CMD_DECRYPT) {
118              for (r = res->recipients; r; r = r->next) {              for (r = recip; r; r = r->next) {
119                  get_pubkey (r->keyid, &key);                  memset (&key, 0, sizeof (key));
120                  if (key) {                  if (!winpt_get_pubkey (r->keyid, &key)) {
121                      char *uid;                      gpgme_user_id_t u = key.ctx->uids;
122                      id = key->uids->name;  
123                        id = u->name;
124                      if (!id)                      if (!id)
125                          id = _("Invalid User ID");                          id = _("Invalid User ID");
126                      uid = utf8_to_wincp (id, strlen (id));                      n = 32+strlen (id)+1+4+strlen (r->keyid)+1;
127                      info = new char [32+strlen (uid)+1 + 4 + strlen (r->keyid)+1                      if (u->email)
128                                       + strlen (key->uids->email)+1];                          n += strlen (u->email)+1;
129                        info = new char [n+1];
130                      if (!info)                      if (!info)
131                          BUG (NULL);                          BUG (NULL);
132                      sprintf (info, "%s <%s> (%s, 0x%s)", uid, key->uids->email,                      if (!u->email || strlen (u->email) < 1)
133                               get_key_pubalgo (r->pubkey_algo), r->keyid+8);                          sprintf (info, "%s (%s, 0x%s)", id,
134                      free (uid);                                   get_key_pubalgo (r->pubkey_algo), r->keyid+8);
135                                            else
136                            sprintf (info, "%s <%s> (%s, 0x%s)", id, u->email,
137                                     get_key_pubalgo (r->pubkey_algo), r->keyid+8);
138                  }                  }
139                  else {                  else {
140                      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 143  passphrase_callback_proc (HWND dlg, UINT
143                      sprintf (info, _("Unknown key ID (%s, 0x%s)"),                      sprintf (info, _("Unknown key ID (%s, 0x%s)"),
144                               get_key_pubalgo (r->pubkey_algo), r->keyid+8);                               get_key_pubalgo (r->pubkey_algo), r->keyid+8);
145                  }                  }
146                  listbox_add_string (GetDlgItem (dlg, IDC_DECRYPT_LIST), info);                  ListBox_AddString_utf8 (GetDlgItem (dlg, IDC_DECRYPT_LIST), info);
147                  free_if_alloc (info);                  free_if_alloc (info);
148                    winpt_release_pubkey (&key);
149              }              }
150          }          }
151          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 171  passphrase_callback_proc (HWND dlg, UINT
171                  SetDlgItemText (dlg, IDC_DECRYPT_MSG, c->info);                  SetDlgItemText (dlg, IDC_DECRYPT_MSG, c->info);
172          }          }
173          else {          else {
174              SetFocus( GetDlgItem (dlg, IDC_DECRYPT_SIGN_PWD));              SetFocus (GetDlgItem (dlg, IDC_DECRYPT_SIGN_PWD));
175              SetDlgItemText (dlg, IDC_DECRYPT_SIGN_MSG, c->info);              SetDlgItemText (dlg, IDC_DECRYPT_SIGN_MSG, c->info);
176          }          }
177          center_window (dlg, NULL);          center_window (dlg, NULL);
178          SetForegroundWindow (dlg);          SetForegroundWindow (dlg);
         set_active_window (dlg);  
179          return FALSE;          return FALSE;
180    
181          case WM_SYSCOMMAND:          case WM_SYSCOMMAND:
182              if (LOWORD (wparam) == SC_CLOSE) {              if (LOWORD (wparam) == SC_CLOSE) {
                 SetDlgItemText (dlg, item_ctrl_id (c->gpg_cmd), "");  
183                  c->cancel = 1;                  c->cancel = 1;
184                  EndDialog (dlg, TRUE);                  EndDialog (dlg, TRUE);
185              }              }
# Line 193  passphrase_callback_proc (HWND dlg, UINT Line 213  passphrase_callback_proc (HWND dlg, UINT
213                      strcpy (c->pwd, "");                      strcpy (c->pwd, "");
214                  }                  }
215                  else {                  else {
216                      c->pwd = new char[n+2];                      char *p = new char[n+2];
217                      if (!c->pwd)                      if (!p)
218                          BUG (NULL);                          BUG (NULL);
219                      GetDlgItemText (dlg, item_ctrl_id (c->gpg_cmd), c->pwd, n+1);                      /* Get the passphrase and convert it utf8.
220                           This does not just the us-ascii charset. */
221                        SafeGetDlgItemText (dlg, item_ctrl_id (c->gpg_cmd), p, n+1);
222                        c->pwd = native_to_utf8 (p);
223                        sfree_if_alloc (p);
224                  }                  }
225                  res = gpgme_op_decrypt_result (c->gpg);                  res = gpgme_op_decrypt_result (c->gpg);
226                  if (!res)                  if (!res)
227                      res_sig = gpgme_op_sign_result (c->gpg);                      res_sig = gpgme_op_sign_result (c->gpg);            
228                  if (reg_prefs.cache_time > 0 && !c->is_card &&                  if (!c->is_card && reg_prefs.cache_time > 0 &&
229                      ((res && res->recipients) || (res_sig && res_sig->signatures))) {                      (res || res_sig)) {
230                      if (agent_get_cache (c->keyid, &item))                      if (agent_get_cache (c->keyid, &item))
231                          agent_unlock_cache_entry (&item);                          agent_unlock_cache_entry (&item);
232                      else                      else
233                          agent_put_cache (c->keyid, c->pwd, reg_prefs.cache_time);                          agent_put_cache (c->keyid, c->pwd, reg_prefs.cache_time);
234                  }                  }
235                  c->cancel = 0;                  c->cancel = 0;
# Line 233  parse_gpg_keyid (const char *pass_info) Line 257  parse_gpg_keyid (const char *pass_info)
257      static char keyid[16+1];      static char keyid[16+1];
258            
259      /* XXX: check for leading alpha-chars? */      /* XXX: check for leading alpha-chars? */
260      if (strlen (pass_info) < 16)      if (strlen (pass_info) < 16) {
261            log_debug ("parse_gpg_keyid: error '%s'\r\n", pass_info);
262          return NULL;          return NULL;
263        }
264      /* the format of the desc buffer looks like this:      /* the format of the desc buffer looks like this:
265         request_keyid[16] main_keyid[16] keytype[1] keylength[4]         request_keyid[16] main_keyid[16] keytype[1] keylength[4]
266         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 279  parse_gpg_description (const char *uid_h
279      gpgme_pubkey_algo_t algo;      gpgme_pubkey_algo_t algo;
280      char usedkey[16+1];      char usedkey[16+1];
281      char mainkey[16+1];      char mainkey[16+1];
282      char *uid, *p;      char *p, *uid;
283      int n=0;      int n=0;
284    
285        algo = (gpgme_pubkey_algo_t)0;
286      /* 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. */
287      if (strlen (uid_hint) < 17) {      if (strlen (uid_hint) < 17) {
288          *desc = 0;          *desc = 0;
289            log_debug ("parse_gpg_description: error '%s'\r\n", uid_hint);
290          return -1;          return -1;
291      }      }
292    
# Line 272  parse_gpg_description (const char *uid_h Line 300  parse_gpg_description (const char *uid_h
300      uid_hint += 16; /* skip keyid */      uid_hint += 16; /* skip keyid */
301      uid_hint += 1;  /* space */      uid_hint += 1;  /* space */
302    
303      uid = utf8_to_wincp (uid_hint, strlen (uid_hint));      uid = utf8_to_native (uid_hint);
   
304      if (strcmp (usedkey, mainkey))      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 (main key ID %s)\n"),                       "%s key, ID 0x%s (main key ID 0x%s)\n"),
309                     uid, get_key_pubalgo (algo), usedkey+8, mainkey+8);                     uid, get_key_pubalgo (algo), usedkey+8, mainkey+8);
310      else if (!strcmp (usedkey, mainkey))      else if (!strcmp (usedkey, mainkey))
311          _snprintf (desc, size-1,          _snprintf (desc, size-1,
312                     _("You need a passphrase to unlock the secret key for\n"                     _("You need a passphrase to unlock the secret key for user:\n"
313                       "user: \"%s\"\n"                       "\"%s\"\n"
314                       "%s key, ID %s\n"),                       "%s key, ID 0x%s\n"),
315                       uid, get_key_pubalgo (algo), usedkey+8);                       uid, get_key_pubalgo (algo), usedkey+8);
316      free (uid);      safe_free (uid);
317      return 0;      return 0;
318  }  }
319    
# Line 299  extract_serial_no (const char *id) Line 326  extract_serial_no (const char *id)
326      char *p;      char *p;
327    
328      p = strchr (id, '/');      p = strchr (id, '/');
329      if (!p)      if (!p) {
330            log_debug ("extract_serial_no: error '%s'\r\n", id);
331          return NULL;          return NULL;
332        }
333      strncpy (buf, id+(p-id)-6, 6);      strncpy (buf, id+(p-id)-6, 6);
334      return buf;      return buf;
335  }  }
# Line 315  passphrase_cb (void *hook, const char *u Line 344  passphrase_cb (void *hook, const char *u
344      passphrase_cb_s *c = (passphrase_cb_s*)hook;      passphrase_cb_s *c = (passphrase_cb_s*)hook;
345      HANDLE hd = (HANDLE)fd;      HANDLE hd = (HANDLE)fd;
346      void *item;      void *item;
347      const char *keyid, *pass;      const char *keyid=NULL, *pass;
348      DWORD n;      DWORD n;
349      int rc;      int rc = 0;
350    
351      if (!c)      if (!c) {
352            log_debug ("passphrase_cb: error '!c'\r\n");
353          return gpg_error (GPG_ERR_INV_ARG);          return gpg_error (GPG_ERR_INV_ARG);
354        }
355      c->bad_pwd = prev_was_bad? 1 : 0;      c->bad_pwd = prev_was_bad? 1 : 0;
356      if (prev_was_bad && !c->cancel) {      if (prev_was_bad && !c->cancel) {
357          if (c->pwd)          if (c->pwd)
# Line 333  passphrase_cb (void *hook, const char *u Line 364  passphrase_cb (void *hook, const char *u
364          if (strlen (passphrase_info) < 16 &&          if (strlen (passphrase_info) < 16 &&
365              !strstr (passphrase_info, "OPENPGP")) {              !strstr (passphrase_info, "OPENPGP")) {
366              /* assume symetric encryption. */              /* assume symetric encryption. */
367              int n=2;              int pos=2;
368              c->sym.sym_algo = atoi (passphrase_info);              c->sym.sym_algo = atoi (passphrase_info);
369              if (c->sym.sym_algo > 9)              if (c->sym.sym_algo > 9)
370                  n++;                  pos++;
371              /* XXX: be more strict. */              /* XXX: be more strict. */
372              c->sym.s2k_mode = atoi (passphrase_info+n);              c->sym.s2k_mode = atoi (passphrase_info+pos);
373              c->sym.s2k_hash = atoi (passphrase_info+n+2);              c->sym.s2k_hash = atoi (passphrase_info+pos+2);
374          }          }
375    
376          keyid = parse_gpg_keyid (passphrase_info);          keyid = parse_gpg_keyid (passphrase_info);
377          pass = agent_get_cache (keyid+8, &item);          pass = agent_get_cache (keyid+8, &item);        
378          if (pass) {          if (pass) {
379              agent_unlock_cache_entry (&item);              agent_unlock_cache_entry (&item);
380              c->pwd_init = 0;              c->pwd_init = 0;
# Line 372  passphrase_cb (void *hook, const char *u Line 403  passphrase_cb (void *hook, const char *u
403          }          }
404          else if (uid_hint)          else if (uid_hint)
405              parse_gpg_description (uid_hint, passphrase_info,              parse_gpg_description (uid_hint, passphrase_info,
406                                     c->info, sizeof c->info - 1);                                     c->info, sizeof (c->info) - 1);
407          if (c->gpg_cmd == GPG_CMD_DECRYPT) {          if (c->gpg_cmd == GPG_CMD_DECRYPT) {
408              rc = DialogBoxParam (glob_hinst, (LPCSTR)IDD_WINPT_DECRYPT,              rc = DialogBoxParam (glob_hinst, (LPCSTR)IDD_WINPT_DECRYPT,
409                                   (HWND)c->hwnd, passphrase_callback_proc,                                   (HWND)c->hwnd, passphrase_callback_proc,
# Line 390  passphrase_cb (void *hook, const char *u Line 421  passphrase_cb (void *hook, const char *u
421          }          }
422          c->pwd_init = 0;          c->pwd_init = 0;
423      }      }
424      if (c->cancel) {      if (c->cancel || !c->pwd) {
425          if (!WriteFile (hd, "\n", 1, &n, NULL))          if (!WriteFile (hd, "\n", 1, &n, NULL))
426              log_debug ("passphrase_cb: WriteFile() failed ec=%d\n", w32_errno);              log_debug ("passphrase_cb: WriteFile() failed ec=%d\n", w32_errno);
427          return 0;          return 0;
428      }      }
429    
430      WriteFile (hd, c->pwd, strlen (c->pwd), &n, NULL);      if (!WriteFile (hd, c->pwd, strlen (c->pwd), &n, NULL))
431      WriteFile (hd, "\n", 1, &n, NULL);          log_debug ("passphrase_cb: WriteFile() failed ec=%d\n", w32_errno);
432        if (!WriteFile (hd, "\n", 1, &n, NULL))
433            log_debug ("passphrase_cb: WriteFile() failed ec=%d\n", w32_errno);
434      return 0;      return 0;
435  }  }
436    
# Line 418  set_gpg_passphrase_cb (passphrase_cb_s * Line 451  set_gpg_passphrase_cb (passphrase_cb_s *
451      cb->pwd_init = 1;      cb->pwd_init = 1;
452      free_if_alloc (cb->title);      free_if_alloc (cb->title);
453      cb->title = m_strdup (title);      cb->title = m_strdup (title);
     if (!cb->title)  
         BUG (NULL);  
454      gpgme_set_passphrase_cb (ctx, passphrase_cb, cb);      gpgme_set_passphrase_cb (ctx, passphrase_cb, cb);
455      cb->gpg = ctx;      cb->gpg = ctx;
456  }  }
457    
458    
459  /* Release a passphrase callback @ctx. */  /* Release the gpg recipient list. */
460  void  void
461  release_gpg_passphrase_cb (passphrase_cb_s *ctx)  release_gpg_recipients (gpgme_recipient_t *recipients)
462  {  {
463      gpgme_recipient_t r, n;      gpgme_recipient_t r, n;
464    
465      if (!ctx)      r = *recipients;
466          return;      while (r != NULL) {
     sfree_if_alloc (ctx->pwd);  
     free_if_alloc (ctx->title);  
     r = ctx->recipients;  
     while (r) {  
467          n = r->next;          n = r->next;
468          safe_free (r->keyid);          safe_free (r->keyid);
469          safe_free (r);          safe_free (r);
470          r = n;          r = n;
471      }      }
472        *recipients = NULL;
473    }
474    
475    
476    
477    /* Release a passphrase callback @ctx. */
478    void
479    release_gpg_passphrase_cb (passphrase_cb_s *ctx)
480    {
481        if (!ctx)
482            return;
483        sfree_if_alloc (ctx->pwd);
484        free_if_alloc (ctx->title);
485        release_gpg_recipients (&ctx->recipients);
486  }  }
487    
488    

Legend:
Removed from v.36  
changed lines
  Added in v.255

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26