/[winpt]/trunk/Src/wptPassphraseCB.cpp
ViewVC logotype

Diff of /trunk/Src/wptPassphraseCB.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 23 by twoaday, Fri Sep 30 10:10:16 2005 UTC revision 25 by twoaday, Wed Oct 12 10:04:26 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
4   *   *
5   * This file is part of WinPT.   * This file is part of WinPT.
6   *   *
# Line 20  Line 21 
21    
22  #include <windows.h>  #include <windows.h>
23  #include <ctype.h>  #include <ctype.h>
 #include <io.h>  
24    
25  #include "../resource.h"  #include "../resource.h"
26  #include "wptNLS.h"  #include "wptNLS.h"
# Line 37  Line 37 
37  #include "wptAgent.h"  #include "wptAgent.h"
38  #include "wptRegistry.h"  #include "wptRegistry.h"
39    
40    const char* get_symkey_algo (int algo);
41    
42  #define item_ctrl_id( cmd ) \  #define item_ctrl_id( cmd ) \
43      ((cmd) == GPG_CMD_DECRYPT? IDC_DECRYPT_PWD : IDC_DECRYPT_SIGN_PWD)      ((cmd) == GPG_CMD_DECRYPT? IDC_DECRYPT_PWD : IDC_DECRYPT_SIGN_PWD)
# Line 50  static BOOL CALLBACK Line 51  static BOOL CALLBACK
51  passphrase_callback_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)  passphrase_callback_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
52  {      {    
53      static passphrase_cb_s * c;      static passphrase_cb_s * c;
54        gpgme_decrypt_result_t res;
55        gpgme_sign_result_t res_sig;
56      gpgme_key_t key;      gpgme_key_t key;
57        void *ctx = NULL, *item;
58      const char *id;      const char *id;
59      char *info;      char *info;
     void *ctx = NULL, *item;  
60      int n;      int n;
61    
62      switch( msg )  {      switch (msg) {
63      case WM_INITDIALOG:      case WM_INITDIALOG:
64          c = (passphrase_cb_s *)lparam;          c = (passphrase_cb_s *)lparam;
65          if (!c)          if (!c)
# Line 67  passphrase_callback_proc (HWND dlg, UINT Line 70  passphrase_callback_proc (HWND dlg, UINT
70                  _("Encrypted with the following public key(s)") );                  _("Encrypted with the following public key(s)") );
71              CheckDlgButton( dlg, IDC_DECRYPT_HIDE, BST_CHECKED );              CheckDlgButton( dlg, IDC_DECRYPT_HIDE, BST_CHECKED );
72          }          }
73          else if( c->gpg_cmd == GPG_CMD_SIGN )          else if (c->gpg_cmd == GPG_CMD_SIGN)
74              CheckDlgButton (dlg, IDC_DECRYPT_SIGN_HIDE, BST_CHECKED);              CheckDlgButton (dlg, IDC_DECRYPT_SIGN_HIDE, BST_CHECKED);
75          if (gpgme_op_decrypt_result (c->gpg) != NULL &&          res = gpgme_op_decrypt_result (c->gpg);
76              c->gpg_cmd == GPG_CMD_DECRYPT) {          if (res != NULL && c->gpg_cmd == GPG_CMD_DECRYPT) {
77              gpgme_recipient_t r;              gpgme_recipient_t r;
             gpgme_decrypt_result_t res;  
78    
79              res = gpgme_op_decrypt_result (c->gpg);              /* XXX: not all ENCRYPT_TO entries are listed here. */
80              for (r = res->recipients; r; r = r->next) {              for (r = res->recipients; r; r = r->next) {
81                  get_pubkey (r->keyid, &key);                  get_pubkey (r->keyid, &key);
82                  if (key) {                  if (key) {
83                      char *uid;                      char *uid;
84                      id = key->uids->uid;                      id = key->uids->name;
85                      if (!id)                      if (!id)
86                          id = _("Invalid User ID");                          id = _("Invalid User ID");
87                      uid = utf8_to_wincp (id, strlen (id));                      uid = utf8_to_wincp (id, strlen (id));
88                      info = new char [8+strlen (uid) + 4 + strlen (r->keyid) + 2];                      info = new char [32+strlen (uid)+1 + 4 + strlen (r->keyid)+1
89                                         + strlen (key->uids->email)+1];
90                      if (!info)                      if (!info)
91                          BUG (NULL);                          BUG (NULL);
92                      sprintf (info, "%s (%s, 0x%s)", uid,                      sprintf (info, "%s <%s> (%s, 0x%s)", uid, key->uids->email,
93                               get_key_pubalgo (r->pubkey_algo), r->keyid+8);                               get_key_pubalgo (r->pubkey_algo), r->keyid+8);
94                      free (uid);                      free (uid);
95                      free (info);                      
96                  }                  }
97                  else {                  else {
98                      info = new char [32 + strlen (r->keyid) + 2];                      info = new char [32 + strlen (r->keyid)+1 + 4];
99                      if (!info)                      if (!info)
100                          BUG (NULL);                          BUG (NULL);
101                      sprintf (info, _("Unknown (key ID 0x%s)"),                      sprintf (info, _("Unknown key ID (%s, 0x%s)"),
102                               r->keyid? r->keyid+8 : "????????");                               get_key_pubalgo (r->pubkey_algo), r->keyid+8);
                     free (info);  
103                  }                  }
104                  listbox_add_string (GetDlgItem( dlg, IDC_DECRYPT_LIST ), info);                  listbox_add_string (GetDlgItem (dlg, IDC_DECRYPT_LIST), info);
105                    free_if_alloc (info);
106              }              }
107          }          }
108          SetDlgItemText( dlg, c->gpg_cmd == GPG_CMD_DECRYPT?          else if (c->gpg_cmd == GPG_CMD_DECRYPT)
109                EnableWindow (GetDlgItem (dlg, IDC_DECRYPT_LIST), FALSE);
110            SetDlgItemText (dlg, c->gpg_cmd == GPG_CMD_DECRYPT?
111                          IDC_DECRYPT_PWDINFO : IDC_DECRYPT_SIGN_PWDINFO,                          IDC_DECRYPT_PWDINFO : IDC_DECRYPT_SIGN_PWDINFO,
112                          _("Please enter your passphrase") );                          _("Please enter your passphrase"));
113          if( c->gpg_cmd == GPG_CMD_DECRYPT ) {          if (c->gpg_cmd == GPG_CMD_DECRYPT) {
114              SetFocus( GetDlgItem( dlg, IDC_DECRYPT_PWD ) );              SetFocus (GetDlgItem (dlg, IDC_DECRYPT_PWD));
115              SetDlgItemText( dlg, IDC_DECRYPT_MSG, c->info );              if (res && !res->recipients) {
116                    const char *s = _("Symmetric encryption.\n"
117                                      "%s encrypted data.");
118                    const char *alg = get_symkey_algo (c->sym.sym_algo);
119                    info = new char[strlen (s) + strlen (alg) + 2];
120                    if (!info)
121                        BUG (NULL);
122                    sprintf (info, s, alg);
123                    SetDlgItemText (dlg, IDC_DECRYPT_MSG, info);
124                    free_if_alloc (info);
125                }
126                else
127                    SetDlgItemText (dlg, IDC_DECRYPT_MSG, c->info);
128          }          }
129          else {          else {
130              SetFocus( GetDlgItem( dlg, IDC_DECRYPT_SIGN_PWD ) );              SetFocus( GetDlgItem (dlg, IDC_DECRYPT_SIGN_PWD));
131              SetDlgItemText( dlg, IDC_DECRYPT_SIGN_MSG, c->info );              SetDlgItemText (dlg, IDC_DECRYPT_SIGN_MSG, c->info);
132          }          }
133          center_window( dlg, NULL );          center_window (dlg, NULL);
134          SetForegroundWindow( dlg );          SetForegroundWindow (dlg);
135          set_active_window( dlg );          set_active_window (dlg);
136          return FALSE;          return FALSE;
137    
138          case WM_SYSCOMMAND:          case WM_SYSCOMMAND:
# Line 124  passphrase_callback_proc (HWND dlg, UINT Line 141  passphrase_callback_proc (HWND dlg, UINT
141                  c->cancel = 1;                  c->cancel = 1;
142                  EndDialog( dlg, TRUE );                  EndDialog( dlg, TRUE );
143              }              }
144              return FALSE;              break;
145    
146          case WM_COMMAND:          case WM_COMMAND:
147              switch( HIWORD( wparam ) ) {              switch( HIWORD( wparam ) ) {
# Line 134  passphrase_callback_proc (HWND dlg, UINT Line 151  passphrase_callback_proc (HWND dlg, UINT
151                      HWND hwnd;                      HWND hwnd;
152                      int hide = IsDlgButtonChecked (dlg, item_ctrl_id2 (c->gpg_cmd));                      int hide = IsDlgButtonChecked (dlg, item_ctrl_id2 (c->gpg_cmd));
153                      hwnd = GetDlgItem (dlg, item_ctrl_id (c->gpg_cmd));                      hwnd = GetDlgItem (dlg, item_ctrl_id (c->gpg_cmd));
154                      SendMessage( hwnd, EM_SETPASSWORDCHAR, hide? '*' : 0, 0 );                      SendMessage (hwnd, EM_SETPASSWORDCHAR, hide? '*' : 0, 0);
155                      SetFocus (hwnd);                      SetFocus (hwnd);
156                  }                  }
157              }              }
# Line 154  passphrase_callback_proc (HWND dlg, UINT Line 171  passphrase_callback_proc (HWND dlg, UINT
171                      strcpy (c->pwd, "");                      strcpy (c->pwd, "");
172                  }                  }
173                  else {                  else {
174                      c->pwd = new char[n+1];                      c->pwd = new char[n+2];
175                      if (!c->pwd)                      if (!c->pwd)
176                          BUG (NULL);                          BUG (NULL);
177                      GetDlgItemText (dlg, item_ctrl_id (c->gpg_cmd), c->pwd, sizeof (c->pwd) -1);                      GetDlgItemText (dlg, item_ctrl_id (c->gpg_cmd), c->pwd, n+1);
178                  }                  }
179                  if (reg_prefs.cache_time > 0 && !c->is_card && !strstr (c->keyid, "missing")) {                  res = gpgme_op_decrypt_result (c->gpg);
180                    if (!res)
181                        res_sig = gpgme_op_sign_result (c->gpg);
182                    if (reg_prefs.cache_time > 0 && !c->is_card &&
183                        ((res && res->recipients) || (res_sig && res_sig->signatures))) {
184                      if (agent_get_cache (c->keyid, &item))                      if (agent_get_cache (c->keyid, &item))
185                          agent_unlock_cache_entry (&item);                          agent_unlock_cache_entry (&item);
186                      else                      else
# Line 258  passphrase_cb (void *hook, const char *u Line 279  passphrase_cb (void *hook, const char *u
279                 int prev_was_bad, int fd)                 int prev_was_bad, int fd)
280  {  {
281      passphrase_cb_s *c = (passphrase_cb_s*)hook;      passphrase_cb_s *c = (passphrase_cb_s*)hook;
282        HANDLE hd = (HANDLE)fd;
283      void *item;      void *item;
284      const char *keyid, *pass;      const char *keyid, *pass;
285        DWORD n;
286      int rc;      int rc;
287    
288        /* XXX: pubkey_enc cancel does not quit gpg.exe */
289        /* XXX: handle prev_was_bad case. */
290      if (!c)      if (!c)
291          return gpg_error (GPG_ERR_INV_ARG);          return gpg_error (GPG_ERR_INV_ARG);
292    
293      if (passphrase_info) {      if (passphrase_info) {
294            if (strlen (passphrase_info) < 16) {/* assume symetric encryption. */
295                int n=2;
296                c->sym.sym_algo = atoi (passphrase_info);
297                if (c->sym.sym_algo > 9)
298                    n++;
299                /* XXX: be more strict. */
300                c->sym.s2k_mode = atoi (passphrase_info+n);
301                c->sym.s2k_hash = atoi (passphrase_info+n+2);
302            }
303    
304          keyid = parse_gpg_keyid (passphrase_info);          keyid = parse_gpg_keyid (passphrase_info);
305          pass = agent_get_cache (keyid, &item);          pass = agent_get_cache (keyid+8, &item);
306          if (pass) {          if (pass) {
307              agent_unlock_cache_entry (&item);              agent_unlock_cache_entry (&item);
308              c->pwd_init = 0;              c->pwd_init = 0;
309              write (fd, pass, strlen (pass));              if (!WriteFile (hd, pass, strlen (pass), &n, NULL))
310              write (fd, "\n", 1);                  log_debug ("passphrase_cb: WriteFile() failed ec=%d\n", w32_errno);
311                if (!WriteFile (hd, "\n", 1, &n, NULL))
312                    log_debug ("passphrase_cb: WriteFile() failed ec=%d\n", w32_errno);
313              return 0;              return 0;
314          }          }
315      }      }
# Line 306  passphrase_cb (void *hook, const char *u Line 343  passphrase_cb (void *hook, const char *u
343                                   (LPARAM)c);                                   (LPARAM)c);
344          }          }
345          if (rc == -1) {          if (rc == -1) {
346              write (fd, "\n", 1);              WriteFile (hd, "\n", 1, &n, NULL);
347              return gpg_error (GPG_ERR_EOF);              return gpg_error (GPG_ERR_EOF);
348          }          }
349          c->pwd_init = 0;          c->pwd_init = 0;
350      }      }
351      if (c->cancel) {      if (c->cancel) {
352          write (fd, "\n", 1);          WriteFile (hd, "\n", 1, &n, NULL);
353          return gpg_error (GPG_ERR_EOF);          return gpg_error (GPG_ERR_EOF);
354      }      }
355    
356      write (fd, c->pwd, strlen (c->pwd));      WriteFile (hd, c->pwd, strlen (c->pwd), &n, NULL);
357      write (fd, "\n", 1);      WriteFile (hd, "\n", 1, &n, NULL);
358      return 0;      return 0;
359  }  }
360    

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26