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

Diff of /trunk/Src/wptClipSignEncDlg.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 328 by twoaday, Fri Sep 25 16:07:38 2009 UTC
# Line 1  Line 1 
1  /* wptSignEncDlg.cpp - Sign & encrypt dialog  /* wptSignEncDlg.cpp - Sign & encrypt dialog
2   *      Copyright (C) 2000-2004 Timo Schulz   *      Copyright (C) 2000-2006, 2009 Timo Schulz
3   *   *
4   * This file is part of WinPT.   * This file is part of WinPT.
5   *   *
6   * WinPT is free software; you can redistribute it and/or modify   * WinPT is free software; you can redistribute it and/or modify
7   * it under the terms of the GNU General Public License as published by   * it under the terms of the GNU General Public License as published by
8   * the Free Software Foundation; either version 2 of the License, or   * the Free Software Foundation; either version 2 of the License, or
9   * (at your option) any later version.   * (at your option) any later version.
10   *   *
11   * WinPT is distributed in the hope that it will be useful,   * WinPT is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.   * GNU General Public License for more details.
15   *   */
16   * You should have received a copy of the GNU General Public License  #ifdef HAVE_CONFIG_H
17   * along with WinPT; if not, write to the Free Software Foundation,  #include <config.h>
18   * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA  #endif
19   */  
20    #include <windows.h>
21  #include <windows.h>  #include <commctrl.h>
22  #include <commctrl.h>  
23    #include "resource.h"
24  #include "../resource.h"  #include "wptErrors.h"
25  #include "wptErrors.h"  #include "wptAgent.h"
26  #include "wptAgent.h"  #include "wptCrypto.h"
27  #include "wptGPG.h"  #include "wptGPG.h"
28  #include "wptCommonCtl.h"  #include "wptCommonCtl.h"
29  #include "wptKeylist.h"  #include "wptKeylist.h"
30  #include "wptTypes.h"  #include "wptTypes.h"
31  #include "wptNLS.h"  #include "wptNLS.h"
32  #include "wptContext.h" /* for passphrase_s */  #include "wptContext.h" /* for passphrase_s */
33  #include "wptDlgs.h"  #include "wptDlgs.h"
34  #include "wptW32API.h"  #include "wptW32API.h"
35  #include "wptKeylist.h"  #include "wptKeylist.h"
36  #include "wptVersion.h"  #include "wptVersion.h"
37  #include "wptGPG.h"  #include "wptGPG.h"
38  #include "wptRegistry.h"  #include "wptRegistry.h"
39  #include "wptUTF8.h"  #include "wptUTF8.h"
40    
41    
42  BOOL CALLBACK  /* Encrypt the clipboard data with the recipients from @rset and
43  clip_signenc_dlg_proc( HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam )     additionally sign the data before @signer as the keyID.
44  {     Return value: 0 on success. */
45      static listview_ctrl_t lv = NULL;  gpgme_error_t
46      static keylist_t list = NULL;  gpg_clip_sign_encrypt (gpgme_ctx_t ctx, const char *signer,
47      static int enable = 0;                         gpgme_key_t *rset, int opts)
48      gpgme_ctx_t ctx;  {
49      gpgme_keycache_t kc;      gpgme_error_t err;
50      gpgme_recipients_t rset;      gpgme_data_t plain = NULL;
51      gpgme_error_t err;      gpgme_data_t ciph = NULL;
52      passphrase_cb_s pwd;      gpgme_key_t key = NULL;
53      char * signer = NULL;  
54      int force_trust = 0, opt = 0;      if (!signer)
55      int kmode = reg_prefs.keylist_mode? KEYLIST_ENCRYPT_MIN : KEYLIST_ENCRYPT;          return gpg_error (GPG_ERR_INV_ARG);
56            if (get_pubkey (signer, &key))
57      switch( msg ) {          return gpg_error (GPG_ERR_NO_PUBKEY);
58      case WM_INITDIALOG:  
59  #ifndef LANG_DE      gpgme_set_armor (ctx, 1);
60          SetWindowText( dlg, _("Sign & Encrypt") );      gpgme_set_textmode (ctx, 1);
61  #endif      
62          kc = keycache_get_ctx( KEYCACHE_PUB );      err = gpg_data_utf8_new_from_clipboard (&plain, 0, NULL);
63          if( !kc )      if (err)
64              BUG( NULL );          goto leave;  
65          lv = keylist_load( GetDlgItem( dlg, IDC_SIGNENC_KEYLIST ),      err = gpgme_data_new (&ciph);
66                             kc, NULL, kmode, GPGME_ATTR_USERID );      if (err)
67          seclist_init( dlg, IDC_SIGNENC_SECLIST, KEYLIST_FLAG_SHORT, &list );          goto leave;
68          center_window( dlg );      err = gpgme_signers_add (ctx, key);
69          set_active_window( dlg );      if (err)
70          EnableWindow( GetDlgItem( dlg, IDC_SIGNENC_SECLIST ), FALSE );          goto leave;
71          SetDlgItemText (dlg, IDC_SIGNENC_SELKEY, _("Select key for signing"));  
72          SetDlgItemText (dlg, IDC_SIGNENC_SECLISTINF, _("Signing key:"));      err = gpgme_op_encrypt_sign (ctx, rset,
73          SetForegroundWindow( dlg );                                   opts? GPGME_ENCRYPT_ALWAYS_TRUST : (gpgme_encrypt_flags_t)0,
74          return TRUE;                                   plain, ciph);
75                if (err)
76      case WM_DESTROY:          goto leave;
77          seclist_destroy( &list );  
78          reset_active_window( );      gpg_data_release_to_clipboard (ciph, 1);
79          if( lv ) {      ciph = NULL;
80              keylist_delete( lv );  
81              lv = NULL;  leave:
82          }      if (plain)
83          memset( pwd.pwd, 0, sizeof pwd.pwd );          gpgme_data_release (plain);
84          return FALSE;      if (ciph)
85                    gpgme_data_release (ciph);
86      case WM_NOTIFY:      return err;
87          NMHDR * notify;  }
88          notify = (NMHDR *)lparam;  
89          if( notify && notify->code == NM_DBLCLK  
90              && notify->idFrom == IDC_SIGNENC_KEYLIST )  /* Dialog procedure for clipboard sign + encrypt. */
91              PostMessage( dlg, WM_COMMAND, MAKEWPARAM(IDOK, 0), NULL );  BOOL CALLBACK
92          return TRUE;  clip_signenc_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
93            {
94      case WM_SYSCOMMAND:      static keylist_ctrl_t kl = NULL;
95          if( LOWORD (wparam) == SC_CLOSE )      static keylist_t list = NULL;
96              EndDialog( dlg, TRUE );      static int keys_sortby = 0;
97          return FALSE;      gpg_keycache_t kc;
98                gpgme_key_t *rset;
99      case WM_COMMAND:      gpgme_error_t err;
100          /* fixme: the enable seems to have a sync problem */      gpgme_ctx_t ctx;
101          if( HIWORD( wparam ) == BN_CLICKED      passphrase_cb_s pwd;
102              && LOWORD( wparam ) == IDC_SIGNENC_SELKEY ) {      char *signer = NULL;
103              enable ^= 1;      size_t n;
104              EnableWindow( GetDlgItem( dlg, IDC_SIGNENC_SECLIST ), enable? TRUE : FALSE );      int force_trust = 0;
105          }      
106          switch( LOWORD( wparam ) ) {      switch (msg) {
107          case IDOK:      case WM_INITDIALOG:
108              rset = keylist_get_recipients( lv, &force_trust, NULL );          SetWindowText (dlg, _("Sign & Encrypt"));
109              if( !gpgme_recipients_count( rset ) ) {          SetDlgItemText (dlg, IDCANCEL, _("&Cancel"));
110                  msg_box( dlg, _("You must select at least one key."), _("Sign & Encrypt"), MB_ERR );          kc = keycache_get_ctx (KEYCACHE_PUB);
111                  gpgme_recipients_release( rset );          kl = keylist_load (GetDlgItem (dlg, IDC_SIGNENC_KEYLIST),
112                  return FALSE;                             kc, NULL, KEYLIST_ENCRYPT_MIN, KEY_SORT_USERID);
113              }          seclist_init (dlg, IDC_SIGNENC_SECLIST, KEYLIST_FLAG_SHORT, &list);
114              if( IsDlgButtonChecked( dlg, IDC_SIGNENC_SELKEY ) ) {          center_window (dlg, NULL);
115                  gpgme_key_t key;          EnableWindow (GetDlgItem (dlg, IDC_SIGNENC_SECLIST), FALSE);
116                  const char * s;          SetDlgItemText (dlg, IDC_SIGNENC_SELKEY, _("Select key for signing"));
117            SetDlgItemText (dlg, IDC_SIGNENC_SECLISTINF, _("Signing key:"));
118                  if( seclist_select_key( dlg, IDC_SIGNENC_SECLIST, &key ) ) {          SetForegroundWindow (dlg);
119                      msg_box( dlg, _("No key was selected."), _("Signing"), MB_ERR );          return TRUE;
120                      return FALSE;          
121                  }      case WM_DESTROY:
122                  s = gpgme_key_get_string_attr( key, GPGME_ATTR_KEYID, NULL, 0 );          seclist_destroy (&list);
123                  if( s )          if (kl) {
124                      signer = m_strdup( s+8 );              keylist_delete (kl);
125              }              kl = NULL;
126              else {          }
127                  signer = get_gnupg_default_key( );          return FALSE;
128                  if( !signer ) {          
129                      msg_box( dlg, _("Could not get default key."), _("Signing"), MB_ERR );      case WM_NOTIFY:
130                      return FALSE;          int ret;
131                  }          ret = keylist_listview_notify (dlg, kl->keys,
132              }                                         IDC_SIGNENC_KEYLIST, lparam);
133              memset( &pwd, 0, sizeof pwd );          if (ret != 0) {
134              err = gpgme_new( &ctx );              SetWindowLong (dlg, DWL_MSGRESULT, ret);
135              if( err )              return TRUE;
136                  BUG( dlg );          }
137              gpgme_enable_logging( ctx );          
138              set_gpg_passphrase_cb( ctx, &pwd, GPG_CMD_SIGN, dlg, _("Sign & Encrypt") );          NMHDR *notify;
139              if( force_trust )          notify = (NMHDR *)lparam;
140                  opt |= GPGME_CTRL_FORCETRUST;          if (notify && notify->code == NM_DBLCLK
141              err = gpgme_op_clip_sign_encrypt( ctx, rset, signer, opt );              && notify->idFrom == IDC_SIGNENC_KEYLIST)
142              memset( pwd.pwd, 0, sizeof pwd.pwd );              PostMessage (dlg, WM_COMMAND, MAKEWPARAM (IDOK, 0), 0);
143              gpgme_recipients_release( rset );          
144              free_if_alloc( signer );          if (notify && notify->code == LVN_COLUMNCLICK &&
145              if( err == GPGME_Bad_Passphrase )                  notify->idFrom == IDC_SIGNENC_KEYLIST) {
146                  agent_del_cache( pwd.keyid );              NMLISTVIEW *p = (LPNMLISTVIEW) lparam;
147              if( err ) {              int sortby = 0;
148                  gpgme_show_error( dlg, err, ctx, _("Sign & Encrypt"), MB_ERR );  
149                  gpgme_release( ctx );              switch (p->iSubItem) {
150                  return FALSE;              case  0: sortby = KEY_SORT_USERID; break;
151              }              case  1: sortby = KEY_SORT_KEYID; break;
152              gpgme_release( ctx );              case  2: sortby = KEY_SORT_LEN; break;
153              show_msg( dlg, 1500, _("GnuPG Status: Finished") );              case  4: sortby = KEY_SORT_VALIDITY; break;
154              EndDialog( dlg, TRUE );              default: sortby = KEY_SORT_USERID; break;
155              return TRUE;              }
156                            if ((keys_sortby & ~KEYLIST_SORT_DESC) == sortby)
157          case IDCANCEL:                  keys_sortby ^= KEYLIST_SORT_DESC;
158              EndDialog( dlg, FALSE );              else
159              return FALSE;                  keys_sortby = sortby;
160          }              keylist_sort (kl, keys_sortby);
161          break;          }
162      }          return TRUE;
163                
164      return FALSE;      case WM_COMMAND:
165  } /* clip_signenc_dlg_proc */          if (HIWORD (wparam) == BN_CLICKED
166                && LOWORD (wparam) == IDC_SIGNENC_SELKEY) {
167                int enable = IsDlgButtonChecked (dlg, IDC_SIGNENC_SELKEY);
168                EnableWindow (GetDlgItem (dlg, IDC_SIGNENC_SECLIST), enable? TRUE : FALSE);
169            }
170            switch (LOWORD (wparam)) {
171            case IDOK:
172                rset = keylist_get_recipients (kl, &force_trust, &n);
173                if (!n) {
174                    msg_box (dlg, _("You must select at least one key."),
175                             _("Sign & Encrypt"), MB_ERR);
176                    return FALSE;
177                }
178                if (IsDlgButtonChecked (dlg, IDC_SIGNENC_SELKEY)) {
179                    gpgme_key_t key;
180                    const char *s;
181    
182                    if (seclist_select_key (dlg, IDC_SIGNENC_SECLIST, &key)) {
183                        msg_box (dlg, _("No key was selected."), _("Signing"), MB_ERR);
184                        safe_free (rset);
185                        return FALSE;
186                    }
187                    s = key->subkeys->keyid;
188                    if (s)
189                        signer = m_strdup(s+8);
190                }
191                else {
192                    signer = get_gnupg_default_key ();
193                    if (!signer) {
194                        msg_box (dlg, _("Could not get default key."), _("Signing"), MB_ERR);
195                        safe_free (rset);
196                        return FALSE;
197                    }
198                }
199    
200                err = gpgme_new (&ctx);
201                if (err)
202                    BUG (NULL);
203                set_gpg_passphrase_cb (&pwd, ctx, GPG_CMD_SIGN, dlg, _("Sign & Encrypt"));
204                err = gpg_clip_sign_encrypt (ctx, signer, rset, force_trust);
205                release_gpg_passphrase_cb (&pwd);
206                safe_free (rset);
207                free_if_alloc (signer);
208                gpgme_release (ctx);
209                if (gpgme_err_code (err) ==  GPG_ERR_BAD_PASSPHRASE)
210                    agent_del_cache (pwd.keyid);
211                if (err)
212                    msg_box (dlg, gpgme_strerror (err), _("Sign & Encrypt"), MB_ERR);
213                else {
214                    show_msg (dlg, 1500, _("GnuPG Status: Finished"));
215                    EndDialog (dlg, TRUE);
216                }
217                return TRUE;
218                
219            case IDCANCEL:
220                EndDialog (dlg, FALSE);
221                return FALSE;
222            }
223            break;
224        }
225        
226        return FALSE;
227    }

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26