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

Legend:
Removed from v.2  
changed lines
  Added in v.260

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26