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

Legend:
Removed from v.26  
changed lines
  Added in v.187

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26