/[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 24 by twoaday, Sat Oct 8 10:43:08 2005 UTC revision 205 by twoaday, Thu Apr 27 12:46:03 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.
6   *   *
7   * WinPT is free software; you can redistribute it and/or   * WinPT is free software; you can redistribute it and/or
8   * modify it under the terms of the GNU General Public License   * modify it under the terms of the GNU General Public License
9   * as published by the Free Software Foundation; either version 2   * as published by the Free Software Foundation; either version 2
10   * of the License, or (at your option) any later version.   * of the License, or (at your option) any later version.
11   *     *  
12   * WinPT is distributed in the hope that it will be useful,   * WinPT is distributed in the hope that it will be useful,
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.
16   *   *
17   * You should have received a copy of the GNU General Public License   * You should have received a copy of the GNU General Public License
18   * along with WinPT; if not, write to the Free Software Foundation,   * along with WinPT; if not, write to the Free Software Foundation,
19   * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA   * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20   */   */
21    
22  #include <windows.h>  #ifdef HAVE_CONFIG_H
23  #include <ctype.h>  #include <config.h>
24    #endif
25  #include "../resource.h"  
26  #include "wptNLS.h"  #include <windows.h>
27  #include "wptW32API.h"  #include <ctype.h>
28  #include "wptVersion.h"  
29  #include "wptGPG.h"  #include "resource.h"
30  #include "wptCommonCtl.h"  #include "wptNLS.h"
31  #include "wptContext.h"  #include "wptW32API.h"
32  #include "wptDlgs.h"  #include "wptVersion.h"
33  #include "wptUTF8.h"  #include "wptGPG.h"
34  #include "wptErrors.h"  #include "wptCommonCtl.h"
35  #include "wptTypes.h"  #include "wptContext.h"
36  #include "wptKeyList.h"  #include "wptDlgs.h"
37  #include "wptAgent.h"  #include "wptErrors.h"
38  #include "wptRegistry.h"  #include "wptTypes.h"
39    #include "wptKeylist.h"
40    #include "wptAgent.h"
41  #define item_ctrl_id( cmd ) \  #include "wptRegistry.h"
42      ((cmd) == GPG_CMD_DECRYPT? IDC_DECRYPT_PWD : IDC_DECRYPT_SIGN_PWD)  #include "wptUTF8.h"
43    
44  #define item_ctrl_id2(cmd) \  const char* get_symkey_algo (int algo);
45      ((cmd) == GPG_CMD_DECRYPT? IDC_DECRYPT_HIDE : IDC_DECRYPT_SIGN_HIDE)  
46    #define item_ctrl_id(cmd) \
47        ((cmd) == GPG_CMD_DECRYPT? IDC_DECRYPT_PWD : IDC_DECRYPT_SIGN_PWD)
48  /* Dialog procedure for the passphrase callback. */  
49  static BOOL CALLBACK  #define item_ctrl_id2(cmd) \
50  passphrase_callback_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)      ((cmd) == GPG_CMD_DECRYPT? IDC_DECRYPT_HIDE : IDC_DECRYPT_SIGN_HIDE)
51  {      
52      static passphrase_cb_s * c;  
53      gpgme_decrypt_result_t res;  void ListBox_AddString_utf8 (HWND lb, const char *txt);
54      gpgme_sign_result_t res_sig;  
55      gpgme_key_t key;  /* Overwrite passphrase and free memory. */
56      const char *id;  static void
57      char *info;  burn_passphrase (char **pwd)
58      void *ctx = NULL, *item;  {
59      int n;      char *pass = *pwd;
60        wipememory (pass, strlen (pass));
61      switch (msg) {      delete []pass;
62      case WM_INITDIALOG:      *pwd = NULL;
63          c = (passphrase_cb_s *)lparam;  }
64          if (!c)  
65              BUG (0);  
66          SetWindowText (dlg, c->title);  /* Dialog procedure for the passphrase callback. */
67          if (c->gpg_cmd == GPG_CMD_DECRYPT) {  static BOOL CALLBACK
68              SetDlgItemText( dlg, IDC_DECRYPT_LISTINF,  passphrase_callback_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
69                  _("Encrypted with the following public key(s)") );  {    
70              CheckDlgButton( dlg, IDC_DECRYPT_HIDE, BST_CHECKED );      static passphrase_cb_s *c;
71          }      gpgme_decrypt_result_t res=NULL;
72          else if( c->gpg_cmd == GPG_CMD_SIGN )      gpgme_sign_result_t res_sig=NULL;
73              CheckDlgButton (dlg, IDC_DECRYPT_SIGN_HIDE, BST_CHECKED);      gpgme_key_t key;
74          res = gpgme_op_decrypt_result (c->gpg);      gpgme_recipient_t recip=NULL, r;
75          if (res != NULL && c->gpg_cmd == GPG_CMD_DECRYPT) {      void *item;
76              gpgme_recipient_t r;      const char *id;
77        char *info;
78              for (r = res->recipients; r; r = r->next) {      int n;
79                  get_pubkey (r->keyid, &key);  
80                  if (key) {      switch (msg) {
81                      char *uid;      case WM_ACTIVATE:
82                      id = key->uids->name;          safe_edit_control_init (dlg, item_ctrl_id (c->gpg_cmd));
83                      if (!id)          break;
84                          id = _("Invalid User ID");  
85                      uid = utf8_to_wincp (id, strlen (id));      case WM_DESTROY:
86                      info = new char [16+strlen (uid) + 4 + strlen (r->keyid) + strlen (key->uids->email) +  3];          safe_edit_control_free (dlg, item_ctrl_id (c->gpg_cmd));
87                      if (!info)          break;
88                          BUG (NULL);  
89                      sprintf (info, "%s <%s> (%s, 0x%s)", uid, key->uids->email,      case WM_INITDIALOG:
90                               get_key_pubalgo (r->pubkey_algo), r->keyid+8);          c = (passphrase_cb_s *)lparam;
91                      free (uid);          if (!c)
92                                    BUG (0);
93                  }          SetDlgItemText (dlg, IDCANCEL, _("&Cancel"));
94                  else {          SetDlgItemText (dlg, IDC_DECRYPT_HIDE, _("&Hide Typing"));
95                      info = new char [32 + strlen (r->keyid) + 2];          SetWindowText (dlg, c->title);
96                      if (!info)          if (c->gpg_cmd == GPG_CMD_DECRYPT) {
97                          BUG (NULL);              SetDlgItemText (dlg, IDC_DECRYPT_LISTINF,
98                      sprintf (info, _("Unknown (key ID 0x%s)"),                              _("Encrypted with the following public key(s)"));
99                               r->keyid? r->keyid+8 : "????????");              CheckDlgButton (dlg, IDC_DECRYPT_HIDE, BST_CHECKED);
100                  }          }
101                  listbox_add_string (GetDlgItem (dlg, IDC_DECRYPT_LIST), info);          else if (c->gpg_cmd == GPG_CMD_SIGN) {
102                  free (info);              SetDlgItemText (dlg, IDC_DECRYPT_SIGN_HIDE, _("&Hide Typing"));
103              }              CheckDlgButton (dlg, IDC_DECRYPT_SIGN_HIDE, BST_CHECKED);
104          }          }
105          else if (c->gpg_cmd == GPG_CMD_DECRYPT)          /* Because it depends on the order the keys are stored in the
106              EnableWindow (GetDlgItem (dlg, IDC_DECRYPT_LIST), FALSE);             keyring whether res->recipients is complete or not, we also
107          SetDlgItemText( dlg, c->gpg_cmd == GPG_CMD_DECRYPT?             support that the recipients were externally extracted and then
108                          IDC_DECRYPT_PWDINFO : IDC_DECRYPT_SIGN_PWDINFO,             we use this list. */
109                          _("Please enter your passphrase") );          if (c->recipients)
110          if (c->gpg_cmd == GPG_CMD_DECRYPT) {              recip = c->recipients; /* recipients were already extracted. */
111              SetFocus (GetDlgItem (dlg, IDC_DECRYPT_PWD));          else {
112              if (res && !res->recipients)              res = gpgme_op_decrypt_result (c->gpg);
113                  SetDlgItemText (dlg, IDC_DECRYPT_MSG, _("Symmetric encryption.\n"));                                                                  if (res && res->recipients)
114              else                  recip = res->recipients;
115                  SetDlgItemText (dlg, IDC_DECRYPT_MSG, c->info);          }
116          }          if (recip != NULL && c->gpg_cmd == GPG_CMD_DECRYPT) {
117          else {              for (r = recip; r; r = r->next) {
118              SetFocus( GetDlgItem (dlg, IDC_DECRYPT_SIGN_PWD));                  get_pubkey (r->keyid, &key);
119              SetDlgItemText (dlg, IDC_DECRYPT_SIGN_MSG, c->info);                  if (key) {
120          }                      gpgme_user_id_t u = key->uids;
121          center_window (dlg, NULL);  
122          SetForegroundWindow (dlg);                      id = u->name;
123          set_active_window (dlg);                      if (!id)
124          return FALSE;                          id = _("Invalid User ID");
125                        n = 32+strlen (id)+1+4+strlen (r->keyid)+1;
126          case WM_SYSCOMMAND:                      if (u->email)
127              if( LOWORD( wparam ) == SC_CLOSE ) {                          n += strlen (u->email)+1;
128                  SetDlgItemText( dlg, item_ctrl_id( c->gpg_cmd ), "" );                      info = new char [n+1];
129                  c->cancel = 1;                      if (!info)
130                  EndDialog( dlg, TRUE );                          BUG (NULL);
131              }                      if (!u->email || strlen (u->email) < 1)
132              break;                          sprintf (info, "%s (%s, 0x%s)", id,
133                                     get_key_pubalgo (r->pubkey_algo), r->keyid+8);
134          case WM_COMMAND:                      else
135              switch( HIWORD( wparam ) ) {                          sprintf (info, "%s <%s> (%s, 0x%s)", id, u->email,
136              case BN_CLICKED:                                   get_key_pubalgo (r->pubkey_algo), r->keyid+8);
137                  if ( LOWORD( wparam ) == IDC_DECRYPT_HIDE                  }
138                      || LOWORD( wparam ) == IDC_DECRYPT_SIGN_HIDE ) {                  else {
139                      HWND hwnd;                      info = new char [32 + strlen (r->keyid)+1 + 4];
140                      int hide = IsDlgButtonChecked (dlg, item_ctrl_id2 (c->gpg_cmd));                      if (!info)
141                      hwnd = GetDlgItem (dlg, item_ctrl_id (c->gpg_cmd));                          BUG (NULL);
142                      SendMessage( hwnd, EM_SETPASSWORDCHAR, hide? '*' : 0, 0 );                      sprintf (info, _("Unknown key ID (%s, 0x%s)"),
143                      SetFocus (hwnd);                               get_key_pubalgo (r->pubkey_algo), r->keyid+8);
144                  }                  }
145              }                  ListBox_AddString_utf8 (GetDlgItem (dlg, IDC_DECRYPT_LIST), info);
146                    free_if_alloc (info);
147              switch (LOWORD (wparam)) {              }
148              case IDOK:            }
149                  /* fixme: the item is even cached when the passphrase is not          else if (c->gpg_cmd == GPG_CMD_DECRYPT)
150                            correct, which means that the user needs to delete all              EnableWindow (GetDlgItem (dlg, IDC_DECRYPT_LIST), FALSE);
151                            cached entries to continue. */          SetDlgItemText (dlg, c->gpg_cmd == GPG_CMD_DECRYPT?
152                  if (c->pwd) {                          IDC_DECRYPT_PWDINFO : IDC_DECRYPT_SIGN_PWDINFO,
153                      delete []c->pwd;                          c->bad_pwd? _("Bad passphrase; Enter passphrase again") :
154                      c->pwd = NULL;                          _("Please enter your passphrase"));
155                  }          if (c->gpg_cmd == GPG_CMD_DECRYPT) {
156                  n = item_get_text_length (dlg, item_ctrl_id (c->gpg_cmd));              SetFocus (GetDlgItem (dlg, IDC_DECRYPT_PWD));
157                  if (!n) {              if (res && !res->recipients) {
158                      c->pwd = new char[2];                  const char *s = _("Symmetric encryption.\n"
159                      strcpy (c->pwd, "");                                    "%s encrypted data.");
160                  }                  const char *alg = get_symkey_algo (c->sym.sym_algo);
161                  else {                  info = new char[strlen (s) + strlen (alg) + 2];
162                      c->pwd = new char[n+2];                  if (!info)
163                      if (!c->pwd)                      BUG (NULL);
164                          BUG (NULL);                  sprintf (info, s, alg);
165                      GetDlgItemText (dlg, item_ctrl_id (c->gpg_cmd), c->pwd, n+1);                  SetDlgItemText (dlg, IDC_DECRYPT_MSG, info);
166                  }                  free_if_alloc (info);
167                  res = gpgme_op_decrypt_result (c->gpg);              }
168                  if (!res)              else
169                      res_sig = gpgme_op_sign_result (c->gpg);                  SetDlgItemText (dlg, IDC_DECRYPT_MSG, c->info);
170                  if (reg_prefs.cache_time > 0 && !c->is_card &&          }
171                      ((res && res->recipients) || (res_sig && res_sig->signatures))) {          else {
172                      if (agent_get_cache (c->keyid, &item))              SetFocus (GetDlgItem (dlg, IDC_DECRYPT_SIGN_PWD));
173                          agent_unlock_cache_entry (&item);              SetDlgItemText (dlg, IDC_DECRYPT_SIGN_MSG, c->info);
174                      else          }
175                          agent_put_cache (c->keyid, c->pwd, reg_prefs.cache_time);          center_window (dlg, NULL);
176                  }          SetForegroundWindow (dlg);
177                  EndDialog (dlg, TRUE);          return FALSE;
178                  return TRUE;  
179                            case WM_SYSCOMMAND:
180              case IDCANCEL:              if (LOWORD (wparam) == SC_CLOSE) {
181                  SetDlgItemText (dlg, item_ctrl_id (c->gpg_cmd), "" );                  SetDlgItemText (dlg, item_ctrl_id (c->gpg_cmd), "");
182                  c->cancel = 1;                  c->cancel = 1;
183                  EndDialog (dlg, FALSE);                  EndDialog (dlg, TRUE);
184                  return FALSE;              }
185              }              break;
186              break;  
187      }          case WM_COMMAND:
188                    switch (HIWORD (wparam)) {
189      return FALSE;              case BN_CLICKED:
190  }                  if  (LOWORD (wparam) == IDC_DECRYPT_HIDE
191                        || LOWORD (wparam) == IDC_DECRYPT_SIGN_HIDE) {
192                        HWND hwnd;
193  /* Extract the main keyid from @pass_info.                      int hide = IsDlgButtonChecked (dlg, item_ctrl_id2 (c->gpg_cmd));
194     Return value: long main keyid or NULL for an error. */                      hwnd = GetDlgItem (dlg, item_ctrl_id (c->gpg_cmd));
195  static const char*                      SendMessage (hwnd, EM_SETPASSWORDCHAR, hide? '*' : 0, 0);
196  parse_gpg_keyid (const char *pass_info)                      SetFocus (hwnd);
197  {                  }
198      static char keyid[16+1];              }
199        
200      if (strlen (pass_info) < 16)              switch (LOWORD (wparam)) {
201          return NULL;              case IDOK:  
202      /* the format of the desc buffer looks like this:                  /* XXX: the item is even cached when the passphrase is not
203         request_keyid[16] main_keyid[16] keytype[1] keylength[4]                          correct, which means that the user needs to delete all
204         we use the main keyid to use only one cache entry. */                          cached entries to continue. */
205      strncpy (keyid, pass_info+17, 16);                  if (c->pwd)
206      keyid[16] = 0;                      burn_passphrase (&c->pwd);
207      return keyid;                  n = item_get_text_length (dlg, item_ctrl_id (c->gpg_cmd));
208  }                  if (!n) {
209                        c->pwd = new char[2];
210                        if (!c->pwd)
211  /* Parse the information in @uid_hint and @pass_info to generate                          BUG (NULL);
212     a input message for the user in @desc. */                      strcpy (c->pwd, "");
213  static void                  }
214  parse_gpg_description (const char *uid_hint, const char *pass_info,                  else {
215                         char *desc, int size)                      c->pwd = new char[n+2];
216  {                      if (!c->pwd)
217      gpgme_pubkey_algo_t algo;                          BUG (NULL);
218      char usedkey[16+1], mainkey[16+1];                      SafeGetDlgItemText (dlg, item_ctrl_id (c->gpg_cmd),
219      char *uid, *p;                                          c->pwd, n+1);
220      int n=0;                  }
221                    res = gpgme_op_decrypt_result (c->gpg);
222      while (p = strsep ((char**)&pass_info, " ")) {                  if (!res)
223          switch (n++) {                      res_sig = gpgme_op_sign_result (c->gpg);            
224          case 0: strncpy (mainkey, p, 16); mainkey[16] = 0; break;                  if (reg_prefs.cache_time > 0 && !c->is_card &&
225          case 1: strncpy (usedkey, p, 16); usedkey[16] = 0; break;                      (res || res_sig)) {
226          case 2: algo = (gpgme_pubkey_algo_t)atol (p); break;                      if (agent_get_cache (c->keyid, &item))                      
227          }                          agent_unlock_cache_entry (&item);
228      }                      else
229      uid_hint += 16; /* skip keyid */                          agent_put_cache (c->keyid, c->pwd, reg_prefs.cache_time);
230      uid_hint += 1;  /* space */                  }
231                    c->cancel = 0;
232      uid = utf8_to_wincp (uid_hint, strlen (uid_hint));                  EndDialog (dlg, TRUE);
233                    return TRUE;
234      if (strcmp (usedkey, mainkey))                  
235          _snprintf (desc, size-1,              case IDCANCEL:
236                     _("You need a passphrase to unlock the secret key for\n"                  SetDlgItemText (dlg, item_ctrl_id (c->gpg_cmd), "");
237                       "user: \"%s\"\n"                  c->cancel = 1;
238                       "%s key, ID %s (main key ID %s)\n"),                  EndDialog (dlg, FALSE);
239                     uid, get_key_pubalgo (algo), usedkey+8, mainkey+8);                  return FALSE;
240      else if (!strcmp (usedkey, mainkey))              }
241          _snprintf (desc, size-1,              break;
242                     _("You need a passphrase to unlock the secret key for\n"      }
243                       "user: \"%s\"\n"      
244                       "%s key, ID %s\n"),      return FALSE;
245                       uid, get_key_pubalgo (algo), usedkey+8);  }
246      free (uid);  
247  }  
248    /* Extract the main keyid from @pass_info.
249       Return value: long main keyid or NULL for an error. */
250  static int inline  static const char*
251  is_hexstring (const char * p)  parse_gpg_keyid (const char *pass_info)
252  {  {
253      size_t i;      static char keyid[16+1];
254        
255      for (i=0; i < strlen (p); i++) {      /* XXX: check for leading alpha-chars? */
256          if (!isxdigit (p[i]))      if (strlen (pass_info) < 16) {
257              return -1;          log_debug ("parse_gpg_keyid: error '%s'\r\n", pass_info);
258      }          return NULL;
259      return 0;      }
260  }      /* the format of the desc buffer looks like this:
261           request_keyid[16] main_keyid[16] keytype[1] keylength[4]
262           we use the main keyid to use only one cache entry. */
263  /* Passphrase callback with the ability to support caching. */      strncpy (keyid, pass_info+17, 16);
264  gpgme_error_t      keyid[16] = 0;
265  passphrase_cb (void *hook, const char *uid_hint,      return keyid;
266                 const char *passphrase_info,  }
267                 int prev_was_bad, int fd)  
268  {  
269      passphrase_cb_s *c = (passphrase_cb_s*)hook;  /* Parse the information in @uid_hint and @pass_info to generate
270      HANDLE hd = (HANDLE)fd;     a input message for the user in @desc. */
271      void *item;  static int
272      const char *keyid, *pass;  parse_gpg_description (const char *uid_hint, const char *pass_info,
273      DWORD n;                         char *desc, int size)
274      int rc;  {
275        gpgme_pubkey_algo_t algo;
276      /* XXX: pubkey_enc cancel does not quit gpg.exe */      char usedkey[16+1];
277      /* XXX: handle prev_was_bad case. */      char mainkey[16+1];
278      if (!c)      char *p, *uid;
279          return gpg_error (GPG_ERR_INV_ARG);      int n=0;
280    
281      if (passphrase_info) {      algo = (gpgme_pubkey_algo_t)0;
282          keyid = parse_gpg_keyid (passphrase_info);      /* Each uid_hint contains a long key-ID so it is at least 16 bytes. */
283          pass = agent_get_cache (keyid+8, &item);      if (strlen (uid_hint) < 17) {
284          if (pass) {          *desc = 0;
285              agent_unlock_cache_entry (&item);          log_debug ("parse_gpg_description: error '%s'\r\n", uid_hint);
286              c->pwd_init = 0;          return -1;
287              if (!WriteFile (hd, pass, strlen (pass), &n, NULL))      }
288                  log_debug ("passphrase_cb: WriteFile() failed ec=%d\n", w32_errno);  
289              if (!WriteFile (hd, "\n", 1, &n, NULL))      while (p = strsep ((char**)&pass_info, " ")) {
290                  log_debug ("passphrase_cb: WriteFile() failed ec=%d\n", w32_errno);          switch (n++) {
291              return 0;          case 0: strncpy (mainkey, p, 16); mainkey[16] = 0; break;
292          }          case 1: strncpy (usedkey, p, 16); usedkey[16] = 0; break;
293      }          case 2: algo = (gpgme_pubkey_algo_t)atol (p); break;
294            }
295      if (c->pwd_init) {      }
296          if (keyid && strlen (keyid) == 16)      uid_hint += 16; /* skip keyid */
297              strcpy (c->keyid, keyid+8);      uid_hint += 1;  /* space */
298    
299          /* if the desc has a length of 32 and only hex digits, we assume a      uid = utf8_to_native (uid_hint);
300             smart card has been used. */      if (strcmp (usedkey, mainkey))
301          if (uid_hint && strlen (uid_hint) == 32 && !is_hexstring (uid_hint)) {          _snprintf (desc, size-1,
302              char buf[16];                     _("You need a passphrase to unlock the secret key for\n"
303              memset (buf, 0, sizeof buf);                       "user: \"%s\"\n"
304              strncpy (buf, uid_hint+20, 8);                       "%s key, ID %s (main key ID %s)\n"),
305              _snprintf (c->info, sizeof c->info-1,                     uid, get_key_pubalgo (algo), usedkey+8, mainkey+8);
306                         _("Please enter the PIN to unlock your secret card key\n"      else if (!strcmp (usedkey, mainkey))
307                           "Card: %s"), buf);          _snprintf (desc, size-1,
308              c->is_card = 1;                     _("You need a passphrase to unlock the secret key for\n"
309          }                       "user: \"%s\"\n"
310          else if (uid_hint)                       "%s key, ID %s\n"),
311              parse_gpg_description (uid_hint, passphrase_info,                       uid, get_key_pubalgo (algo), usedkey+8);
312                                     c->info, sizeof c->info - 1);      safe_free (uid);
313          if (c->gpg_cmd == GPG_CMD_DECRYPT) {      return 0;
314              rc = DialogBoxParam (glob_hinst, (LPCSTR)IDD_WINPT_DECRYPT,  }
315                                   (HWND)c->hwnd, passphrase_callback_proc,  
316                                   (LPARAM)c);  
317          }  /* Extract the serial number from the card ID @id and return it. */
318          else if (c->gpg_cmd == GPG_CMD_SIGN) {  const char*
319              rc = DialogBoxParam (glob_hinst, (LPCSTR)IDD_WINPT_DECRYPT_SIGN,  extract_serial_no (const char *id)
320                                   (HWND)c->hwnd, passphrase_callback_proc,  {
321                                   (LPARAM)c);      static char buf[8];
322          }      char *p;
323          if (rc == -1) {  
324              WriteFile (hd, "\n", 1, &n, NULL);      p = strchr (id, '/');
325              return gpg_error (GPG_ERR_EOF);      if (!p) {
326          }          log_debug ("extract_serial_no: error '%s'\r\n", id);
327          c->pwd_init = 0;          return NULL;
328      }      }
329      if (c->cancel) {      strncpy (buf, id+(p-id)-6, 6);
330          WriteFile (hd, "\n", 1, &n, NULL);      return buf;
331          return gpg_error (GPG_ERR_EOF);  }
332      }  
333    
334      WriteFile (hd, c->pwd, strlen (c->pwd), &n, NULL);  /* Passphrase callback with the ability to support caching. */
335      WriteFile (hd, "\n", 1, &n, NULL);  gpgme_error_t
336      return 0;  passphrase_cb (void *hook, const char *uid_hint,
337  }                 const char *passphrase_info,
338                   int prev_was_bad, int fd)
339    {
340  /* Initialize the given passphrase callback @cb with the      passphrase_cb_s *c = (passphrase_cb_s*)hook;
341     used gpgme context @ctx, the command @cmd and a title      HANDLE hd = (HANDLE)fd;
342     @title for the dialog. */      void *item;
343  void      const char *keyid=NULL, *pass;
344  set_gpg_passphrase_cb (passphrase_cb_s *cb, gpgme_ctx_t ctx,      DWORD n;
345                         int cmd, HWND hwnd, const char *title)      int rc = 0;
346  {  
347      memset (cb, 0, sizeof *cb);      if (!c) {
348      cb->gpg_cmd = cmd;          log_debug ("passphrase_cb: error '!c'\r\n");
349      cb->is_card = 0;          return gpg_error (GPG_ERR_INV_ARG);
350      cb->cancel = 0;      }
351      cb->hwnd = hwnd;      c->bad_pwd = prev_was_bad? 1 : 0;
352      cb->pwd_init = 1;      if (prev_was_bad && !c->cancel) {
353      free_if_alloc (cb->title);          if (c->pwd)
354      cb->title = m_strdup (title);              burn_passphrase (&c->pwd);
355      if (!cb->title)          agent_del_cache (c->keyid);
356          BUG (NULL);          c->pwd_init = 1;
357      gpgme_set_passphrase_cb (ctx, passphrase_cb, cb);      }
358      cb->gpg = ctx;  
359  }      if (passphrase_info) {
360            if (strlen (passphrase_info) < 16 &&
361                !strstr (passphrase_info, "OPENPGP")) {
362  /* Release a passphrase callback @ctx. */              /* assume symetric encryption. */
363  void              int pos=2;
364  release_gpg_passphrase_cb (passphrase_cb_s *ctx)              c->sym.sym_algo = atoi (passphrase_info);
365  {              if (c->sym.sym_algo > 9)
366      if (!ctx)                  pos++;
367          return;              /* XXX: be more strict. */
368      sfree_if_alloc (ctx->pwd);              c->sym.s2k_mode = atoi (passphrase_info+pos);
369      free_if_alloc (ctx->title);              c->sym.s2k_hash = atoi (passphrase_info+pos+2);
370  }          }
371    
372  /* Simple check to measure passphrase (@pass) quality.          keyid = parse_gpg_keyid (passphrase_info);
373     Return value: 0 on success. */          pass = agent_get_cache (keyid+8, &item);        
374  int          if (pass) {
375  check_passwd_quality (const char *pass, int strict)              agent_unlock_cache_entry (&item);
376  {              c->pwd_init = 0;
377      int i, nd=0, nc=0, n;              if (!WriteFile (hd, pass, strlen (pass), &n, NULL))
378                    log_debug ("passphrase_cb: WriteFile() failed ec=%d\n", w32_errno);
379      n = strlen (pass);              if (!WriteFile (hd, "\n", 1, &n, NULL))
380      if (n < 8)                  log_debug ("passphrase_cb: WriteFile() failed ec=%d\n", w32_errno);
381          return -1;              return 0;
382            }
383      for (i=0; i < n; i++) {      }
384          if (isdigit (pass[i])) nd++;  
385          if (isalpha (pass[i])) nc++;      if (c->pwd_init) {
386      }          if (keyid && strlen (keyid) == 16)
387                strcpy (c->keyid, keyid+8);
388      if (nd == n || nc == n)  
389          return -1;          /* if @passphrase_info contains 'OPENPGP' we assume a smart card
390                has been used. */
391      return 0;          if (strstr (passphrase_info, "OPENPGP")) {
392  }              const char *s=passphrase_info;
393                while (s && *s && *s != 'D')
394                    s++;
395                _snprintf (c->info, sizeof c->info-1,
396                           _("Please enter the PIN to unlock your secret card key\n"
397                             "Card: %s"), extract_serial_no (s));
398                c->is_card = 1;
399            }
400            else if (uid_hint)
401                parse_gpg_description (uid_hint, passphrase_info,
402                                       c->info, sizeof (c->info) - 1);
403            if (c->gpg_cmd == GPG_CMD_DECRYPT) {
404                rc = DialogBoxParam (glob_hinst, (LPCSTR)IDD_WINPT_DECRYPT,
405                                     (HWND)c->hwnd, passphrase_callback_proc,
406                                     (LPARAM)c);
407            }
408            else if (c->gpg_cmd == GPG_CMD_SIGN) {
409                rc = DialogBoxParam (glob_hinst, (LPCSTR)IDD_WINPT_DECRYPT_SIGN,
410                                     (HWND)c->hwnd, passphrase_callback_proc,
411                                     (LPARAM)c);
412            }
413            if (rc == -1) {
414                if (!WriteFile (hd, "\n", 1, &n, NULL))
415                    log_debug ("passphrase_cb: WriteFile() failed ec=%d\n", w32_errno);
416                return 0;
417            }
418            c->pwd_init = 0;
419        }
420        if (c->cancel) {
421            if (!WriteFile (hd, "\n", 1, &n, NULL))
422                log_debug ("passphrase_cb: WriteFile() failed ec=%d\n", w32_errno);
423            return 0;
424        }
425    
426        if (!WriteFile (hd, c->pwd, strlen (c->pwd), &n, NULL))
427            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;
431    }
432    
433    
434    /* Initialize the given passphrase callback @cb with the
435       used gpgme context @ctx, the command @cmd and a title
436       @title for the dialog. */
437    void
438    set_gpg_passphrase_cb (passphrase_cb_s *cb, gpgme_ctx_t ctx,
439                           int cmd, HWND hwnd, const char *title)
440    {
441        memset (cb, 0, sizeof *cb);
442        cb->gpg_cmd = cmd;
443        cb->bad_pwd = 0;
444        cb->is_card = 0;
445        cb->cancel = 0;
446        cb->hwnd = hwnd;
447        cb->pwd_init = 1;
448        free_if_alloc (cb->title);
449        cb->title = m_strdup (title);
450        if (!cb->title)
451            BUG (NULL);
452        gpgme_set_passphrase_cb (ctx, passphrase_cb, cb);
453        cb->gpg = ctx;
454    }
455    
456    
457    /* Release a passphrase callback @ctx. */
458    void
459    release_gpg_passphrase_cb (passphrase_cb_s *ctx)
460    {
461        gpgme_recipient_t r, n;
462    
463        if (!ctx)
464            return;
465        sfree_if_alloc (ctx->pwd);
466        free_if_alloc (ctx->title);
467        r = ctx->recipients;
468        while (r) {
469            n = r->next;
470            safe_free (r->keyid);
471            safe_free (r);
472            r = n;
473        }
474    }
475    
476    
477    /* Simple check to measure passphrase (@pass) quality.
478       Return value: 0 on success. */
479    int
480    check_passwd_quality (const char *pass, int strict)
481    {
482        int i, nd=0, nc=0, n;
483    
484        n = strlen (pass);
485        if (n < 8)
486            return -1;
487    
488        for (i=0; i < n; i++) {
489            if (isdigit (pass[i]))
490                nd++;
491            if (isalpha (pass[i]))
492                nc++;
493        }
494    
495        /* check that the passphrase contains letters and numbers. */
496        if (nd == n || nc == n)
497            return -1;
498    
499        return 0;
500    }

Legend:
Removed from v.24  
changed lines
  Added in v.205

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26