/[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 451 by twoaday, Sat May 5 14:06:10 2012 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 );          if (!kc)
112                  return FALSE;              BUG (NULL);
113              }          
114              if( IsDlgButtonChecked( dlg, IDC_SIGNENC_SELKEY ) ) {          kl = keylist_load (GetDlgItem (dlg, IDC_SIGNENC_KEYLIST),
115                  gpgme_key_t key;                             kc, NULL, KEYLIST_ENCRYPT_MIN, KEY_SORT_USERID);
116                  const char * s;          seclist_init (dlg, IDC_SIGNENC_SECLIST, KEYLIST_FLAG_SHORT, &list);
117            center_window (dlg, NULL);
118                  if( seclist_select_key( dlg, IDC_SIGNENC_SECLIST, &key ) ) {          EnableWindow (GetDlgItem (dlg, IDC_SIGNENC_SECLIST), FALSE);
119                      msg_box( dlg, _("No key was selected."), _("Signing"), MB_ERR );          SetDlgItemText (dlg, IDC_SIGNENC_SELKEY, _("Select key for signing"));
120                      return FALSE;          SetDlgItemText (dlg, IDC_SIGNENC_SECLISTINF, _("Signing key:"));
121                  }          SetForegroundWindow (dlg);
122                  s = gpgme_key_get_string_attr( key, GPGME_ATTR_KEYID, NULL, 0 );          return TRUE;
123                  if( s )          
124                      signer = m_strdup( s+8 );      case WM_DESTROY:
125              }          seclist_destroy (&list);
126              else {          if (kl) {
127                  signer = get_gnupg_default_key( );              keylist_delete (kl);
128                  if( !signer ) {              kl = NULL;
129                      msg_box( dlg, _("Could not get default key."), _("Signing"), MB_ERR );          }
130                      return FALSE;          return FALSE;
131                  }          
132              }      case WM_NOTIFY:
133              memset( &pwd, 0, sizeof pwd );          /* Wait until the dialog initialization is done */
134              err = gpgme_new( &ctx );          if (kl == NULL)
135              if( err )              break;
136                  BUG( dlg );          
137              gpgme_enable_logging( ctx );          int ret;
138              set_gpg_passphrase_cb( ctx, &pwd, GPG_CMD_SIGN, dlg, _("Sign & Encrypt") );          ret = keylist_listview_notify (dlg, kl->keys,
139              if( force_trust )                                         IDC_SIGNENC_KEYLIST, lparam);
140                  opt |= GPGME_CTRL_FORCETRUST;          if (ret != 0) {
141              err = gpgme_op_clip_sign_encrypt( ctx, rset, signer, opt );              SetWindowLong (dlg, DWL_MSGRESULT, ret);
142              memset( pwd.pwd, 0, sizeof pwd.pwd );              return TRUE;
143              gpgme_recipients_release( rset );          }
144              free_if_alloc( signer );          
145              if( err == GPGME_Bad_Passphrase )              NMHDR *notify;
146                  agent_del_cache( pwd.keyid );          notify = (NMHDR *)lparam;
147              if( err ) {          if (notify && notify->code == NM_DBLCLK &&
148                  gpgme_show_error( dlg, err, ctx, _("Sign & Encrypt"), MB_ERR );              notify->idFrom == IDC_SIGNENC_KEYLIST)
149                  gpgme_release( ctx );              PostMessage (dlg, WM_COMMAND, MAKEWPARAM (IDOK, 0), 0);
150                  return FALSE;          
151              }          if (notify && notify->code == LVN_COLUMNCLICK &&
152              gpgme_release( ctx );              notify->idFrom == IDC_SIGNENC_KEYLIST) {
153              show_msg( dlg, 1500, _("GnuPG Status: Finished") );              NMLISTVIEW *p = (LPNMLISTVIEW) lparam;
154              EndDialog( dlg, TRUE );              int sortby = 0;
155              return TRUE;  
156                            switch (p->iSubItem) {
157          case IDCANCEL:              case  0: sortby = KEY_SORT_USERID; break;
158              EndDialog( dlg, FALSE );              case  1: sortby = KEY_SORT_KEYID; break;
159              return FALSE;              case  2: sortby = KEY_SORT_LEN; break;
160          }              case  4: sortby = KEY_SORT_VALIDITY; break;
161          break;              default: sortby = KEY_SORT_USERID; break;
162      }              }
163                    if ((keys_sortby & ~KEYLIST_SORT_DESC) == sortby)
164      return FALSE;                  keys_sortby ^= KEYLIST_SORT_DESC;
165  } /* clip_signenc_dlg_proc */              else
166                    keys_sortby = sortby;
167                keylist_sort (kl, keys_sortby);
168                return TRUE;
169            }        
170            break;
171            
172        case WM_COMMAND:
173            if (HIWORD (wparam) == BN_CLICKED &&
174                LOWORD (wparam) == IDC_SIGNENC_SELKEY) {
175                int enable = IsDlgButtonChecked (dlg, IDC_SIGNENC_SELKEY);
176                EnableWindow (GetDlgItem (dlg, IDC_SIGNENC_SECLIST), enable? TRUE : FALSE);
177                break;
178            }
179            
180            switch (LOWORD (wparam)) {
181            case IDOK:
182                rset = keylist_get_recipients (kl, &force_trust, &n);
183                if (!n) {
184                    msg_box (dlg, _("You must select at least one key."),
185                             _("Sign & Encrypt"), MB_ERR);
186                    return FALSE;
187                }
188                if (IsDlgButtonChecked (dlg, IDC_SIGNENC_SELKEY)) {
189                    gpgme_key_t key;
190                    const char *s;
191    
192                    if (seclist_select_key (dlg, IDC_SIGNENC_SECLIST, &key)) {
193                        msg_box (dlg, _("No key was selected."), _("Signing"), MB_ERR);
194                        safe_free (rset);
195                        return FALSE;
196                    }
197                    s = key->subkeys->keyid;
198                    if (s)
199                        signer = m_strdup(s+8);
200                }
201                else {
202                    signer = get_gnupg_default_key ();
203                    if (!signer) {
204                        msg_box (dlg, _("Could not get default key."), _("Signing"), MB_ERR);
205                        safe_free (rset);
206                        return FALSE;
207                    }
208                }
209    
210                err = gpgme_new (&ctx);
211                if (err)
212                    BUG (NULL);
213                set_gpg_passphrase_cb (&pwd, ctx, GPG_CMD_SIGN, dlg, _("Sign & Encrypt"));
214                err = gpg_clip_sign_encrypt (ctx, signer, rset, force_trust);
215                release_gpg_passphrase_cb (&pwd);
216                safe_free (rset);
217                free_if_alloc (signer);
218                gpgme_release (ctx);
219                if (gpgme_err_code (err) ==  GPG_ERR_BAD_PASSPHRASE)
220                    agent_del_cache (pwd.keyid);
221                if (err)
222                    msg_box (dlg, gpgme_strerror (err), _("Sign & Encrypt"), MB_ERR);
223                else {
224                    show_msg (dlg, 1500, _("GnuPG Status: Finished"));
225                    EndDialog (dlg, TRUE);
226                }
227                return TRUE;
228                
229            case IDCANCEL:
230                EndDialog (dlg, FALSE);
231                return FALSE;
232            }
233            break;
234        }
235        
236        return FALSE;
237    }

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26