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

Legend:
Removed from v.25  
changed lines
  Added in v.47

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26