/[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 105 by twoaday, Wed Nov 30 10:22:00 2005 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];          if (c->recipients)
96                      if (!info)              recip = c->recipients; /* recipients were already extracted. */
97                          BUG (NULL);          else {
98                      sprintf (info, _("Unknown (key ID 0x%s)"),              /* XXX: not all ENCRYPT_TO entries are listed here. */
99                               r->keyid? r->keyid+8 : "????????");              res = gpgme_op_decrypt_result (c->gpg);
100                      free (info);              if (res && res->recipients)
101                  }                  recip = res->recipients;
102                  listbox_add_string (GetDlgItem( dlg, IDC_DECRYPT_LIST ), info);          }
103              }          if (recip != NULL && c->gpg_cmd == GPG_CMD_DECRYPT) {
104          }              for (r = res->recipients; r; r = r->next) {
105          SetDlgItemText( dlg, c->gpg_cmd == GPG_CMD_DECRYPT?                  get_pubkey (r->keyid, &key);
106                          IDC_DECRYPT_PWDINFO : IDC_DECRYPT_SIGN_PWDINFO,                  if (key) {
107                          _("Please enter your passphrase") );                      char *uid;
108          if( c->gpg_cmd == GPG_CMD_DECRYPT ) {                      id = key->uids->name;
109              SetFocus( GetDlgItem( dlg, IDC_DECRYPT_PWD ) );                      if (!id)
110              SetDlgItemText( dlg, IDC_DECRYPT_MSG, c->info );                          id = _("Invalid User ID");
111          }                      uid = utf8_to_wincp (id, strlen (id));
112          else {                      info = new char [32+strlen (uid)+1 + 4 + strlen (r->keyid)+1
113              SetFocus( GetDlgItem( dlg, IDC_DECRYPT_SIGN_PWD ) );                                       + strlen (key->uids->email)+1];
114              SetDlgItemText( dlg, IDC_DECRYPT_SIGN_MSG, c->info );                      if (!info)
115          }                          BUG (NULL);
116          center_window( dlg, NULL );                      sprintf (info, "%s <%s> (%s, 0x%s)", uid, key->uids->email,
117          SetForegroundWindow( dlg );                               get_key_pubalgo (r->pubkey_algo), r->keyid+8);
118          set_active_window( dlg );                      free (uid);
119          return FALSE;                      
120                    }
121          case WM_SYSCOMMAND:                  else {
122              if( LOWORD( wparam ) == SC_CLOSE ) {                      info = new char [32 + strlen (r->keyid)+1 + 4];
123                  SetDlgItemText( dlg, item_ctrl_id( c->gpg_cmd ), "" );                      if (!info)
124                  c->cancel = 1;                          BUG (NULL);
125                  EndDialog( dlg, TRUE );                      sprintf (info, _("Unknown key ID (%s, 0x%s)"),
126              }                               get_key_pubalgo (r->pubkey_algo), r->keyid+8);
127              return FALSE;                  }
128                    listbox_add_string (GetDlgItem (dlg, IDC_DECRYPT_LIST), info);
129          case WM_COMMAND:                  free_if_alloc (info);
130              switch( HIWORD( wparam ) ) {              }
131              case BN_CLICKED:          }
132                  if ( LOWORD( wparam ) == IDC_DECRYPT_HIDE          else if (c->gpg_cmd == GPG_CMD_DECRYPT)
133                      || LOWORD( wparam ) == IDC_DECRYPT_SIGN_HIDE ) {              EnableWindow (GetDlgItem (dlg, IDC_DECRYPT_LIST), FALSE);
134                      HWND hwnd;          SetDlgItemText (dlg, c->gpg_cmd == GPG_CMD_DECRYPT?
135                      int hide = IsDlgButtonChecked (dlg, item_ctrl_id2 (c->gpg_cmd));                          IDC_DECRYPT_PWDINFO : IDC_DECRYPT_SIGN_PWDINFO,
136                      hwnd = GetDlgItem (dlg, item_ctrl_id (c->gpg_cmd));                          c->bad_pwd? _("Bad passphrase; Enter passphrase again") :
137                      SendMessage( hwnd, EM_SETPASSWORDCHAR, hide? '*' : 0, 0 );                          _("Please enter your passphrase"));
138                      SetFocus (hwnd);          if (c->gpg_cmd == GPG_CMD_DECRYPT) {
139                  }              SetFocus (GetDlgItem (dlg, IDC_DECRYPT_PWD));
140              }              if (res && !res->recipients) {
141                    const char *s = _("Symmetric encryption.\n"
142              switch (LOWORD (wparam)) {                                    "%s encrypted data.");
143              case IDOK:                    const char *alg = get_symkey_algo (c->sym.sym_algo);
144                  /* fixme: the item is even cached when the passphrase is not                  info = new char[strlen (s) + strlen (alg) + 2];
145                            correct, which means that the user needs to delete all                  if (!info)
146                            cached entries to continue. */                      BUG (NULL);
147                  if (c->pwd) {                  sprintf (info, s, alg);
148                      delete []c->pwd;                  SetDlgItemText (dlg, IDC_DECRYPT_MSG, info);
149                      c->pwd = NULL;                  free_if_alloc (info);
150                  }              }
151                  n = item_get_text_length (dlg, item_ctrl_id (c->gpg_cmd));              else
152                  if (!n) {                  SetDlgItemText (dlg, IDC_DECRYPT_MSG, c->info);
153                      c->pwd = new char[2];          }
154                      strcpy (c->pwd, "");          else {
155                  }              SetFocus( GetDlgItem (dlg, IDC_DECRYPT_SIGN_PWD));
156                  else {              SetDlgItemText (dlg, IDC_DECRYPT_SIGN_MSG, c->info);
157                      c->pwd = new char[n+1];          }
158                      if (!c->pwd)          center_window (dlg, NULL);
159                          BUG (NULL);          SetForegroundWindow (dlg);
160                      GetDlgItemText (dlg, item_ctrl_id (c->gpg_cmd), c->pwd, sizeof (c->pwd) -1);          set_active_window (dlg);
161                  }          return FALSE;
162                  if (reg_prefs.cache_time > 0 && !c->is_card && !strstr (c->keyid, "missing")) {  
163                      if (agent_get_cache (c->keyid, &item))          case WM_SYSCOMMAND:
164                          agent_unlock_cache_entry (&item);              if (LOWORD (wparam) == SC_CLOSE) {
165                      else                  SetDlgItemText (dlg, item_ctrl_id (c->gpg_cmd), "");
166                          agent_put_cache (c->keyid, c->pwd, reg_prefs.cache_time);                  c->cancel = 1;
167                  }                  EndDialog (dlg, TRUE);
168                  EndDialog (dlg, TRUE);              }
169                  return TRUE;              break;
170                    
171              case IDCANCEL:          case WM_COMMAND:
172                  SetDlgItemText (dlg, item_ctrl_id (c->gpg_cmd), "" );              switch (HIWORD (wparam)) {
173                  c->cancel = 1;              case BN_CLICKED:
174                  EndDialog (dlg, FALSE);                  if  (LOWORD (wparam) == IDC_DECRYPT_HIDE
175                  return FALSE;                      || LOWORD (wparam) == IDC_DECRYPT_SIGN_HIDE) {
176              }                      HWND hwnd;
177              break;                      int hide = IsDlgButtonChecked (dlg, item_ctrl_id2 (c->gpg_cmd));
178      }                      hwnd = GetDlgItem (dlg, item_ctrl_id (c->gpg_cmd));
179                            SendMessage (hwnd, EM_SETPASSWORDCHAR, hide? '*' : 0, 0);
180      return FALSE;                      SetFocus (hwnd);
181  }                  }
182                }
183    
184  /* Extract the main keyid from @pass_info.              switch (LOWORD (wparam)) {
185     Return value: long main keyid or NULL for an error. */              case IDOK:  
186  static const char*                  /* XXX: the item is even cached when the passphrase is not
187  parse_gpg_keyid (const char *pass_info)                          correct, which means that the user needs to delete all
188  {                          cached entries to continue. */
189      static char keyid[16+1];                  if (c->pwd)
190                            burn_passphrase (&c->pwd);
191      if (strlen (pass_info) < 16)                  n = item_get_text_length (dlg, item_ctrl_id (c->gpg_cmd));
192          return NULL;                  if (!n) {
193      /* the format of the desc buffer looks like this:                      c->pwd = new char[2];
194         request_keyid[16] main_keyid[16] keytype[1] keylength[4]                      if (!c->pwd)
195         we use the main keyid to use only one cache entry. */                          BUG (NULL);
196      strncpy (keyid, pass_info+17, 16);                      strcpy (c->pwd, "");
197      keyid[16] = 0;                  }
198      return keyid;                  else {
199  }                      c->pwd = new char[n+2];
200                        if (!c->pwd)
201                            BUG (NULL);
202  /* Parse the information in @uid_hint and @pass_info to generate                      GetDlgItemText (dlg, item_ctrl_id (c->gpg_cmd), c->pwd, n+1);
203     a input message for the user in @desc. */                  }
204  static void                  res = gpgme_op_decrypt_result (c->gpg);
205  parse_gpg_description (const char *uid_hint, const char *pass_info,                  if (!res)
206                         char *desc, int size)                      res_sig = gpgme_op_sign_result (c->gpg);            
207  {                  if (reg_prefs.cache_time > 0 && !c->is_card &&
208      gpgme_pubkey_algo_t algo;                      (res || res_sig)) {
209      char usedkey[16+1], mainkey[16+1];                      if (agent_get_cache (c->keyid, &item))                      
210      char *uid, *p;                          agent_unlock_cache_entry (&item);
211      int n=0;                      else
212                            agent_put_cache (c->keyid, c->pwd, reg_prefs.cache_time);
213      while (p = strsep ((char**)&pass_info, " ")) {                  }
214          switch (n++) {                  c->cancel = 0;
215          case 0: strncpy (mainkey, p, 16); mainkey[16] = 0; break;                  EndDialog (dlg, TRUE);
216          case 1: strncpy (usedkey, p, 16); usedkey[16] = 0; break;                  return TRUE;
217          case 2: algo = (gpgme_pubkey_algo_t)atol (p); break;                  
218          }              case IDCANCEL:
219      }                  SetDlgItemText (dlg, item_ctrl_id (c->gpg_cmd), "");
220      uid_hint += 16; /* skip keyid */                  c->cancel = 1;
221      uid_hint += 1;  /* space */                  EndDialog (dlg, FALSE);
222                    return FALSE;
223      uid = utf8_to_wincp (uid_hint, strlen (uid_hint));              }
224                break;
225      if (strcmp (usedkey, mainkey))      }
226          _snprintf (desc, size-1,      
227                     _("You need a passphrase to unlock the secret key for\n"      return FALSE;
228                       "user: \"%s\"\n"  }
229                       "%s key, ID %s (main key ID %s)\n"),  
230                     uid, get_key_pubalgo (algo), usedkey+8, mainkey+8);  
231      else if (!strcmp (usedkey, mainkey))  /* Extract the main keyid from @pass_info.
232          _snprintf (desc, size-1,     Return value: long main keyid or NULL for an error. */
233                     _("You need a passphrase to unlock the secret key for\n"  static const char*
234                       "user: \"%s\"\n"  parse_gpg_keyid (const char *pass_info)
235                       "%s key, ID %s\n"),  {
236                       uid, get_key_pubalgo (algo), usedkey+8);      static char keyid[16+1];
237      free (uid);      
238  }      /* XXX: check for leading alpha-chars? */
239        if (strlen (pass_info) < 16) {
240            log_debug ("parse_gpg_keyid: error '%s'\r\n", pass_info);
241  static int inline          return NULL;
242  is_hexstring (const char * p)      }
243  {      /* the format of the desc buffer looks like this:
244      size_t i;         request_keyid[16] main_keyid[16] keytype[1] keylength[4]
245           we use the main keyid to use only one cache entry. */
246      for (i=0; i < strlen (p); i++) {      strncpy (keyid, pass_info+17, 16);
247          if (!isxdigit (p[i]))      keyid[16] = 0;
248              return -1;      return keyid;
249      }  }
250      return 0;  
251  }  
252    /* Parse the information in @uid_hint and @pass_info to generate
253       a input message for the user in @desc. */
254  /* Passphrase callback with the ability to support caching. */  static int
255  gpgme_error_t  parse_gpg_description (const char *uid_hint, const char *pass_info,
256  passphrase_cb (void *hook, const char *uid_hint,                         char *desc, int size)
257                 const char *passphrase_info,  {
258                 int prev_was_bad, int fd)      gpgme_pubkey_algo_t algo;
259  {      char usedkey[16+1];
260      passphrase_cb_s *c = (passphrase_cb_s*)hook;      char mainkey[16+1];
261      void *item;      char *uid, *p;
262      const char *keyid, *pass;      int n=0;
263      int rc;  
264        algo = (gpgme_pubkey_algo_t)0;
265      if (!c)      /* Each uid_hint contains a long key-ID so it is at least 16 bytes. */
266          return gpg_error (GPG_ERR_INV_ARG);      if (strlen (uid_hint) < 17) {
267            *desc = 0;
268      if (passphrase_info) {          log_debug ("parse_gpg_description: error '%s'\r\n", uid_hint);
269          keyid = parse_gpg_keyid (passphrase_info);          return -1;
270          pass = agent_get_cache (keyid, &item);      }
271          if (pass) {  
272              agent_unlock_cache_entry (&item);      while (p = strsep ((char**)&pass_info, " ")) {
273              c->pwd_init = 0;          switch (n++) {
274              write (fd, pass, strlen (pass));          case 0: strncpy (mainkey, p, 16); mainkey[16] = 0; break;
275              write (fd, "\n", 1);          case 1: strncpy (usedkey, p, 16); usedkey[16] = 0; break;
276              return 0;          case 2: algo = (gpgme_pubkey_algo_t)atol (p); break;
277          }          }
278      }      }
279        uid_hint += 16; /* skip keyid */
280      if (c->pwd_init) {      uid_hint += 1;  /* space */
281          if (keyid && strlen (keyid) == 16)  
282              strcpy (c->keyid, keyid+8);      uid = utf8_to_wincp (uid_hint, strlen (uid_hint));
283    
284          /* if the desc has a length of 32 and only hex digits, we assume a      if (strcmp (usedkey, mainkey))
285             smart card has been used. */          _snprintf (desc, size-1,
286          if (uid_hint && strlen (uid_hint) == 32 && !is_hexstring (uid_hint)) {                     _("You need a passphrase to unlock the secret key for\n"
287              char buf[16];                       "user: \"%s\"\n"
288              memset (buf, 0, sizeof buf);                       "%s key, ID %s (main key ID %s)\n"),
289              strncpy (buf, uid_hint+20, 8);                     uid, get_key_pubalgo (algo), usedkey+8, mainkey+8);
290              _snprintf (c->info, sizeof c->info-1,      else if (!strcmp (usedkey, mainkey))
291                         _("Please enter the PIN to unlock your secret card key\n"          _snprintf (desc, size-1,
292                           "Card: %s"), buf);                     _("You need a passphrase to unlock the secret key for\n"
293              c->is_card = 1;                       "user: \"%s\"\n"
294          }                       "%s key, ID %s\n"),
295          else if (uid_hint)                       uid, get_key_pubalgo (algo), usedkey+8);
296              parse_gpg_description (uid_hint, passphrase_info,      free (uid);
297                                     c->info, sizeof c->info - 1);      return 0;
298          if (c->gpg_cmd == GPG_CMD_DECRYPT) {  }
299              rc = DialogBoxParam (glob_hinst, (LPCSTR)IDD_WINPT_DECRYPT,  
300                                   (HWND)c->hwnd, passphrase_callback_proc,  
301                                   (LPARAM)c);  /* Extract the serial number from the card ID @id and return it. */
302          }  const char*
303          else if (c->gpg_cmd == GPG_CMD_SIGN) {  extract_serial_no (const char *id)
304              rc = DialogBoxParam (glob_hinst, (LPCSTR)IDD_WINPT_DECRYPT_SIGN,  {
305                                   (HWND)c->hwnd, passphrase_callback_proc,      static char buf[8];
306                                   (LPARAM)c);      char *p;
307          }  
308          if (rc == -1) {      p = strchr (id, '/');
309              write (fd, "\n", 1);      if (!p) {
310              return gpg_error (GPG_ERR_EOF);          log_debug ("extract_serial_no: error '%s'\r\n", id);
311          }          return NULL;
312          c->pwd_init = 0;      }
313      }      strncpy (buf, id+(p-id)-6, 6);
314      if (c->cancel) {      return buf;
315          write (fd, "\n", 1);  }
316          return gpg_error (GPG_ERR_EOF);  
317      }  
318    /* Passphrase callback with the ability to support caching. */
319      write (fd, c->pwd, strlen (c->pwd));  gpgme_error_t
320      write (fd, "\n", 1);  passphrase_cb (void *hook, const char *uid_hint,
321      return 0;                 const char *passphrase_info,
322  }                 int prev_was_bad, int fd)
323    {
324        passphrase_cb_s *c = (passphrase_cb_s*)hook;
325  /* Initialize the given passphrase callback @cb with the      HANDLE hd = (HANDLE)fd;
326     used gpgme context @ctx, the command @cmd and a title      void *item;
327     @title for the dialog. */      const char *keyid=NULL, *pass;
328  void      DWORD n;
329  set_gpg_passphrase_cb (passphrase_cb_s *cb, gpgme_ctx_t ctx,      int rc = 0;
330                         int cmd, HWND hwnd, const char *title)  
331  {      if (!c) {
332      memset (cb, 0, sizeof *cb);          log_debug ("passphrase_cb: error '!c'\r\n");
333      cb->gpg_cmd = cmd;          return gpg_error (GPG_ERR_INV_ARG);
334      cb->is_card = 0;      }
335      cb->cancel = 0;      c->bad_pwd = prev_was_bad? 1 : 0;
336      cb->hwnd = hwnd;      if (prev_was_bad && !c->cancel) {
337      cb->pwd_init = 1;          if (c->pwd)
338      free_if_alloc (cb->title);              burn_passphrase (&c->pwd);
339      cb->title = m_strdup (title);          agent_del_cache (c->keyid);
340      if (!cb->title)          c->pwd_init = 1;
341          BUG (NULL);      }
342      gpgme_set_passphrase_cb (ctx, passphrase_cb, cb);  
343      cb->gpg = ctx;      if (passphrase_info) {
344  }          if (strlen (passphrase_info) < 16 &&
345                !strstr (passphrase_info, "OPENPGP")) {
346                /* assume symetric encryption. */
347  /* Release a passphrase callback @ctx. */              int pos=2;
348  void              c->sym.sym_algo = atoi (passphrase_info);
349  release_gpg_passphrase_cb (passphrase_cb_s *ctx)              if (c->sym.sym_algo > 9)
350  {                  pos++;
351      if (!ctx)              /* XXX: be more strict. */
352          return;              c->sym.s2k_mode = atoi (passphrase_info+pos);
353      sfree_if_alloc (ctx->pwd);              c->sym.s2k_hash = atoi (passphrase_info+pos+2);
354      free_if_alloc (ctx->title);          }
355  }  
356            keyid = parse_gpg_keyid (passphrase_info);
357  /* Simple check to measure passphrase (@pass) quality.          pass = agent_get_cache (keyid+8, &item);        
358     Return value: 0 on success. */          if (pass) {
359  int              agent_unlock_cache_entry (&item);
360  check_passwd_quality (const char *pass, int strict)              c->pwd_init = 0;
361  {              if (!WriteFile (hd, pass, strlen (pass), &n, NULL))
362      int i, nd=0, nc=0, n;                  log_debug ("passphrase_cb: WriteFile() failed ec=%d\n", w32_errno);
363                if (!WriteFile (hd, "\n", 1, &n, NULL))
364      n = strlen (pass);                  log_debug ("passphrase_cb: WriteFile() failed ec=%d\n", w32_errno);
365      if (n < 8)              return 0;
366          return -1;          }
367        }
368      for (i=0; i < n; i++) {  
369          if (isdigit (pass[i])) nd++;      if (c->pwd_init) {
370          if (isalpha (pass[i])) nc++;          if (keyid && strlen (keyid) == 16)
371      }              strcpy (c->keyid, keyid+8);
372    
373      if (nd == n || nc == n)          /* if @passphrase_info contains 'OPENPGP' we assume a smart card
374          return -1;              has been used. */
375            if (strstr (passphrase_info, "OPENPGP")) {
376      return 0;              const char *s=passphrase_info;
377  }              while (s && *s && *s != 'D')
378                    s++;
379                _snprintf (c->info, sizeof c->info-1,
380                           _("Please enter the PIN to unlock your secret card key\n"
381                             "Card: %s"), extract_serial_no (s));
382                c->is_card = 1;
383            }
384            else if (uid_hint)
385                parse_gpg_description (uid_hint, passphrase_info,
386                                       c->info, sizeof c->info - 1);
387            if (c->gpg_cmd == GPG_CMD_DECRYPT) {
388                rc = DialogBoxParam (glob_hinst, (LPCSTR)IDD_WINPT_DECRYPT,
389                                     (HWND)c->hwnd, passphrase_callback_proc,
390                                     (LPARAM)c);
391            }
392            else if (c->gpg_cmd == GPG_CMD_SIGN) {
393                rc = DialogBoxParam (glob_hinst, (LPCSTR)IDD_WINPT_DECRYPT_SIGN,
394                                     (HWND)c->hwnd, passphrase_callback_proc,
395                                     (LPARAM)c);
396            }
397            if (rc == -1) {
398                if (!WriteFile (hd, "\n", 1, &n, NULL))
399                    log_debug ("passphrase_cb: WriteFile() failed ec=%d\n", w32_errno);
400                return 0;
401            }
402            c->pwd_init = 0;
403        }
404        if (c->cancel) {
405            if (!WriteFile (hd, "\n", 1, &n, NULL))
406                log_debug ("passphrase_cb: WriteFile() failed ec=%d\n", w32_errno);
407            return 0;
408        }
409    
410        if (!WriteFile (hd, c->pwd, strlen (c->pwd), &n, NULL))
411            log_debug ("passphrase_cb: WriteFile() failed ec=%d\n", w32_errno);
412        if (!WriteFile (hd, "\n", 1, &n, NULL))
413            log_debug ("passphrase_cb: WriteFile() failed ec=%d\n", w32_errno);
414        return 0;
415    }
416    
417    
418    /* Initialize the given passphrase callback @cb with the
419       used gpgme context @ctx, the command @cmd and a title
420       @title for the dialog. */
421    void
422    set_gpg_passphrase_cb (passphrase_cb_s *cb, gpgme_ctx_t ctx,
423                           int cmd, HWND hwnd, const char *title)
424    {
425        memset (cb, 0, sizeof *cb);
426        cb->gpg_cmd = cmd;
427        cb->bad_pwd = 0;
428        cb->is_card = 0;
429        cb->cancel = 0;
430        cb->hwnd = hwnd;
431        cb->pwd_init = 1;
432        free_if_alloc (cb->title);
433        cb->title = m_strdup (title);
434        if (!cb->title)
435            BUG (NULL);
436        gpgme_set_passphrase_cb (ctx, passphrase_cb, cb);
437        cb->gpg = ctx;
438    }
439    
440    
441    /* Release a passphrase callback @ctx. */
442    void
443    release_gpg_passphrase_cb (passphrase_cb_s *ctx)
444    {
445        gpgme_recipient_t r, n;
446    
447        if (!ctx)
448            return;
449        sfree_if_alloc (ctx->pwd);
450        free_if_alloc (ctx->title);
451        r = ctx->recipients;
452        while (r) {
453            n = r->next;
454            safe_free (r->keyid);
455            safe_free (r);
456            r = n;
457        }
458    }
459    
460    
461    /* Simple check to measure passphrase (@pass) quality.
462       Return value: 0 on success. */
463    int
464    check_passwd_quality (const char *pass, int strict)
465    {
466        int i, nd=0, nc=0, n;
467    
468        n = strlen (pass);
469        if (n < 8)
470            return -1;
471    
472        for (i=0; i < n; i++) {
473            if (isdigit (pass[i]))
474                nd++;
475            if (isalpha (pass[i]))
476                nc++;
477        }
478    
479        /* check that the passphrase contains letters and numbers. */
480        if (nd == n || nc == n)
481            return -1;
482    
483        return 0;
484    }

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26