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

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26