/[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 23 by twoaday, Fri Sep 30 10:10:16 2005 UTC revision 36 by werner, Thu Oct 27 15:25:13 2005 UTC
# Line 1  Line 1 
1  /* wptSignEncDlg.cpp - Sign & encrypt dialog  /* wptSignEncDlg.cpp - Sign & encrypt dialog
2   *      Copyright (C) 2000-2005 Timo Schulz   *      Copyright (C) 2000-2005 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   * You should have received a copy of the GNU General Public License
17   * along with WinPT; if not, write to the Free Software Foundation,   * along with WinPT; if not, write to the Free Software Foundation,
18   * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA   * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19   */   */
20    
21  #include <windows.h>  #ifdef HAVE_CONFIG_H
22  #include <commctrl.h>  #include <config.h>
23    #endif
24  #include "../resource.h"  
25  #include "wptErrors.h"  #include <windows.h>
26  #include "wptAgent.h"  #include <windows.h>
27  #include "wptGPG.h"  #include <commctrl.h>
28  #include "wptCommonCtl.h"  
29  #include "wptKeylist.h"  #include "../resource.h"
30  #include "wptTypes.h"  #include "wptErrors.h"
31  #include "wptNLS.h"  #include "wptAgent.h"
32  #include "wptContext.h" /* for passphrase_s */  #include "wptCrypto.h"
33  #include "wptDlgs.h"  #include "wptGPG.h"
34  #include "wptW32API.h"  #include "wptCommonCtl.h"
35  #include "wptKeylist.h"  #include "wptKeylist.h"
36  #include "wptVersion.h"  #include "wptTypes.h"
37  #include "wptGPG.h"  #include "wptNLS.h"
38  #include "wptRegistry.h"  #include "wptContext.h" /* for passphrase_s */
39  #include "wptUTF8.h"  #include "wptDlgs.h"
40    #include "wptW32API.h"
41    #include "wptKeylist.h"
42  /* Encrypt the clipboard data with the recipients from @rset and  #include "wptVersion.h"
43     additionally sign the data before @signer as the keyID.  #include "wptGPG.h"
44     Return value: 0 on success. */  #include "wptRegistry.h"
45  gpgme_error_t  #include "wptUTF8.h"
46  gpg_clip_sign_encrypt (struct passphrase_cb_s *cb, const char *signer,  
47                         gpgme_key_t *rset, int opts)  
48  {  /* Encrypt the clipboard data with the recipients from @rset and
49      gpgme_error_t err;     additionally sign the data before @signer as the keyID.
50      gpgme_data_t plain = NULL;     Return value: 0 on success. */
51      gpgme_data_t ciph = NULL;  gpgme_error_t
52      gpgme_key_t key = NULL;  gpg_clip_sign_encrypt (gpgme_ctx_t ctx, const char *signer,
53      gpgme_ctx_t ctx;                         gpgme_key_t *rset, int opts)
54    {
55      if (!signer)      gpgme_error_t err;
56          return gpg_error (GPG_ERR_INV_ARG);      gpgme_data_t plain = NULL;
57      if (get_pubkey (signer, &key))      gpgme_data_t ciph = NULL;
58          return gpg_error (GPG_ERR_NO_PUBKEY);      gpgme_key_t key = NULL;
59        
60      err = gpgme_new (&ctx);      if (!signer)
61      if (err)          return gpg_error (GPG_ERR_INV_ARG);
62          return err;      if (get_pubkey (signer, &key))
63            return gpg_error (GPG_ERR_NO_PUBKEY);
64      /* Update the gpgme context manually. */  
65      cb->gpg = ctx;      gpgme_set_armor (ctx, 1);
66        
67      gpgme_set_armor (ctx, 1);      err = gpg_data_new_from_clipboard (&plain, 0);
68            if (err)
69      err = gpg_data_new_from_clipboard (&plain, 0);          goto leave;  
70      if (err)      err = gpgme_data_new (&ciph);
71          goto leave;        if (err)
72      err = gpgme_data_new (&ciph);          goto leave;
73      if (err)      err = gpgme_signers_add (ctx, key);
74          goto leave;      if (err)
75      err = gpgme_signers_add (ctx, key);          goto leave;
76      if (err)  
77          goto leave;      err = gpgme_op_encrypt_sign (ctx, rset,
78                                     opts? GPGME_ENCRYPT_ALWAYS_TRUST : (gpgme_encrypt_flags_t)0,
79      err = gpgme_op_encrypt_sign (ctx, rset,                                   plain, ciph);
80                                   opts? GPGME_ENCRYPT_ALWAYS_TRUST : (gpgme_encrypt_flags_t)0,      if (err)
81                                   plain, ciph);          goto leave;
82      if (err)  
83          goto leave;      gpg_data_release_and_set_clipboard (ciph, 1);
84        ciph = NULL;
85      gpg_data_release_and_set_clipboard (ciph, 1);  
86      ciph = NULL;  leave:
87        if (plain)
88  leave:          gpgme_data_release (plain);
89      if (plain)      if (ciph)
90          gpgme_data_release (plain);          gpgme_data_release (ciph);
91      if (ciph)      return err;
92          gpgme_data_release (ciph);  }
93      gpgme_key_release (key);  
94      gpgme_release (ctx);  
95      return err;  /* Dialog procedure for clipboard sign + encrypt. */
96  }  BOOL CALLBACK
97    clip_signenc_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
98    {
99  /* Dialog procedure for clipboard sign + encrypt. */      static listview_ctrl_t lv = NULL;
100  BOOL CALLBACK      static keylist_t list = NULL;
101  clip_signenc_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)      gpg_keycache_t kc;
102  {      gpgme_key_t *rset;
103      static listview_ctrl_t lv = NULL;      gpgme_error_t err;
104      static keylist_t list = NULL;      gpgme_ctx_t ctx;
105      static int enable = 0;      passphrase_cb_s pwd;
106      gpgme_keycache_t kc;      char *signer = NULL;
107      gpgme_key_t *rset;      int force_trust = 0, opt = 0;
108      gpgme_error_t err;      int kmode = reg_prefs.keylist_mode? KEYLIST_ENCRYPT_MIN : KEYLIST_ENCRYPT;
109      passphrase_cb_s pwd;      int n;
110      char * signer = NULL;      
111      int force_trust = 0, opt = 0;      switch( msg ) {
112      int kmode = reg_prefs.keylist_mode? KEYLIST_ENCRYPT_MIN : KEYLIST_ENCRYPT;      case WM_INITDIALOG:
113      int n;          SetWindowText (dlg, _("Sign & Encrypt"));
114        
115      switch( msg ) {          kc = keycache_get_ctx( KEYCACHE_PUB );
116      case WM_INITDIALOG:          if( !kc )
117          enable = 0;              BUG( NULL );
118      #ifndef LANG_DE          lv = keylist_load( GetDlgItem( dlg, IDC_SIGNENC_KEYLIST ),
119          SetWindowText( dlg, _("Sign & Encrypt") );                             kc, NULL, kmode, KEY_SORT_USERID);
120      #endif          seclist_init( dlg, IDC_SIGNENC_SECLIST, KEYLIST_FLAG_SHORT, &list );
121          kc = keycache_get_ctx( KEYCACHE_PUB );          center_window (dlg, NULL);
122          if( !kc )          set_active_window (dlg);
123              BUG( NULL );          EnableWindow (GetDlgItem (dlg, IDC_SIGNENC_SECLIST), FALSE);
124          lv = keylist_load( GetDlgItem( dlg, IDC_SIGNENC_KEYLIST ),          SetDlgItemText (dlg, IDC_SIGNENC_SELKEY, _("Select key for signing"));
125                             kc, NULL, kmode, KEY_SORT_USERID);          SetDlgItemText (dlg, IDC_SIGNENC_SECLISTINF, _("Signing key:"));
126          seclist_init( dlg, IDC_SIGNENC_SECLIST, KEYLIST_FLAG_SHORT, &list );          SetForegroundWindow (dlg);
127          center_window (dlg, NULL);          return TRUE;
128          set_active_window (dlg);          
129          EnableWindow (GetDlgItem (dlg, IDC_SIGNENC_SECLIST), FALSE);      case WM_DESTROY:
130          SetDlgItemText (dlg, IDC_SIGNENC_SELKEY, _("Select key for signing"));          seclist_destroy (&list);
131          SetDlgItemText (dlg, IDC_SIGNENC_SECLISTINF, _("Signing key:"));          reset_active_window ();
132          SetForegroundWindow (dlg);          if (lv) {
133          return TRUE;              keylist_delete (lv);
134                        lv = NULL;
135      case WM_DESTROY:          }
136          seclist_destroy (&list);          return FALSE;
137          reset_active_window ();          
138          if (lv) {      case WM_NOTIFY:
139              keylist_delete( lv );          NMHDR * notify;
140              lv = NULL;          notify = (NMHDR *)lparam;
141          }          if (notify && notify->code == NM_DBLCLK
142          return FALSE;              && notify->idFrom == IDC_SIGNENC_KEYLIST)
143                        PostMessage (dlg, WM_COMMAND, MAKEWPARAM (IDOK, 0), NULL);
144      case WM_NOTIFY:          return TRUE;
145          NMHDR * notify;          
146          notify = (NMHDR *)lparam;      case WM_SYSCOMMAND:
147          if( notify && notify->code == NM_DBLCLK          if( LOWORD (wparam) == SC_CLOSE )
148              && notify->idFrom == IDC_SIGNENC_KEYLIST )              EndDialog( dlg, TRUE );
149              PostMessage( dlg, WM_COMMAND, MAKEWPARAM(IDOK, 0), NULL );          return FALSE;
150          return TRUE;          
151                case WM_COMMAND:
152      case WM_SYSCOMMAND:          if (HIWORD (wparam) == BN_CLICKED
153          if( LOWORD (wparam) == SC_CLOSE )              && LOWORD (wparam) == IDC_SIGNENC_SELKEY) {
154              EndDialog( dlg, TRUE );              int enable = IsDlgButtonChecked (dlg, IDC_SIGNENC_SELKEY);
155          return FALSE;              EnableWindow (GetDlgItem (dlg, IDC_SIGNENC_SECLIST), enable? TRUE : FALSE);
156                    }
157      case WM_COMMAND:          switch( LOWORD( wparam ) ) {
158          /* fixme: the enable seems to have a sync problem */          case IDOK:
159          if( HIWORD( wparam ) == BN_CLICKED              rset = keylist_get_recipients( lv, &force_trust, &n );
160              && LOWORD( wparam ) == IDC_SIGNENC_SELKEY ) {              if (!n) {
161              enable ^= 1;                  msg_box( dlg, _("You must select at least one key."), _("Sign & Encrypt"), MB_ERR );
162              EnableWindow( GetDlgItem( dlg, IDC_SIGNENC_SECLIST ), enable? TRUE : FALSE );                  return FALSE;
163          }              }
164          switch( LOWORD( wparam ) ) {              if( IsDlgButtonChecked( dlg, IDC_SIGNENC_SELKEY ) ) {
165          case IDOK:                  gpgme_key_t key;
166              rset = keylist_get_recipients( lv, &force_trust, &n );                  const char * s;
167              if (!n) {  
168                  msg_box( dlg, _("You must select at least one key."), _("Sign & Encrypt"), MB_ERR );                  if( seclist_select_key( dlg, IDC_SIGNENC_SECLIST, &key ) ) {
169                  return FALSE;                      msg_box( dlg, _("No key was selected."), _("Signing"), MB_ERR );
170              }                      return FALSE;
171              if( IsDlgButtonChecked( dlg, IDC_SIGNENC_SELKEY ) ) {                  }
172                  gpgme_key_t key;                  s = key->subkeys->keyid;
173                  const char * s;                  if (s)
174                        signer = m_strdup(s+8);
175                  if( seclist_select_key( dlg, IDC_SIGNENC_SECLIST, &key ) ) {              }
176                      msg_box( dlg, _("No key was selected."), _("Signing"), MB_ERR );              else {
177                      return FALSE;                  signer = get_gnupg_default_key ();
178                  }                  if (!signer) {
179                  s = key->subkeys->keyid;                      msg_box (dlg, _("Could not get default key."), _("Signing"), MB_ERR);
180                  if (s)                      return FALSE;
181                      signer = m_strdup(s+8);                  }
182              }              }
183              else {  
184                  signer = get_gnupg_default_key ();              err = gpgme_new (&ctx);
185                  if (!signer) {              if (err)
186                      msg_box (dlg, _("Could not get default key."), _("Signing"), MB_ERR);                  BUG (NULL);
187                      return FALSE;              set_gpg_passphrase_cb (&pwd, ctx, GPG_CMD_SIGN, dlg, _("Sign & Encrypt"));
188                  }              err = gpg_clip_sign_encrypt (ctx, signer, rset, force_trust);
189              }              release_gpg_passphrase_cb (&pwd);
190                                free (rset);
191              set_gpg_passphrase_cb (&pwd, NULL, GPG_CMD_SIGN, dlg, _("Sign & Encrypt"));              free_if_alloc (signer);
192              err = gpg_clip_sign_encrypt (&pwd, signer, rset, force_trust);              gpgme_release (ctx);
193              release_gpg_passphrase_cb (&pwd);              if (gpgme_err_code (err) ==  GPG_ERR_BAD_PASSPHRASE)
194              free (rset);                  agent_del_cache (pwd.keyid);
195              free_if_alloc (signer);              if (err) {
196              if (err == gpg_error (GPG_ERR_BAD_PASSPHRASE))                  msg_box (dlg, gpgme_strerror (err), _("Sign & Encrypt"), MB_ERR );
197                  agent_del_cache (pwd.keyid);                  return FALSE;
198              if (err) {              }
199                  msg_box (dlg, gpgme_strerror (err), _("Sign & Encrypt"), MB_ERR );              show_msg (dlg, 1500, _("GnuPG Status: Finished"));
200                  return FALSE;              EndDialog (dlg, TRUE);
201              }              return TRUE;
202              show_msg (dlg, 1500, _("GnuPG Status: Finished"));              
203              EndDialog (dlg, TRUE);          case IDCANCEL:
204              return TRUE;              EndDialog( dlg, FALSE );
205                            return FALSE;
206          case IDCANCEL:          }
207              EndDialog( dlg, FALSE );          break;
208              return FALSE;      }
209          }      
210          break;      return FALSE;
211      }  }
       
     return FALSE;  
 }  

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26