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

Legend:
Removed from v.23  
changed lines
  Added in v.179

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26