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

Legend:
Removed from v.32  
changed lines
  Added in v.271

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26