/[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 24 by twoaday, Sat Oct 8 10:43:08 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-2005 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  /* Encrypt the clipboard data with the recipients from @rset and  /* Encrypt the clipboard data with the recipients from @rset and
43     additionally sign the data before @signer as the keyID.     additionally sign the data before @signer as the keyID.
44     Return value: 0 on success. */     Return value: 0 on success. */
45  gpgme_error_t  gpgme_error_t
46  gpg_clip_sign_encrypt (gpgme_ctx_t ctx, const char *signer,  gpg_clip_sign_encrypt (gpgme_ctx_t ctx, const char *signer,
47                         gpgme_key_t *rset, int opts)                         gpgme_key_t *rset, int opts)
48  {  {
49      gpgme_error_t err;      gpgme_error_t err;
50      gpgme_data_t plain = NULL;      gpgme_data_t plain = NULL;
51      gpgme_data_t ciph = NULL;      gpgme_data_t ciph = NULL;
52      gpgme_key_t key = NULL;      gpgme_key_t key = NULL;
53    
54      if (!signer)      if (!signer)
55          return gpg_error (GPG_ERR_INV_ARG);          return gpg_error (GPG_ERR_INV_ARG);
56      if (get_pubkey (signer, &key))      if (get_pubkey (signer, &key))
57          return gpg_error (GPG_ERR_NO_PUBKEY);          return gpg_error (GPG_ERR_NO_PUBKEY);
58    
59      gpgme_set_armor (ctx, 1);      gpgme_set_armor (ctx, 1);
60            gpgme_set_textmode (ctx, 1);
61      err = gpg_data_new_from_clipboard (&plain, 0);      
62      if (err)      err = gpg_data_utf8_new_from_clipboard (&plain, 0, NULL);
63          goto leave;        if (err)
64      err = gpgme_data_new (&ciph);          goto leave;  
65      if (err)      err = gpgme_data_new (&ciph);
66          goto leave;      if (err)
67      err = gpgme_signers_add (ctx, key);          goto leave;
68      if (err)      err = gpgme_signers_add (ctx, key);
69          goto leave;      if (err)
70            goto leave;
71      err = gpgme_op_encrypt_sign (ctx, rset,  
72                                   opts? GPGME_ENCRYPT_ALWAYS_TRUST : (gpgme_encrypt_flags_t)0,      err = gpgme_op_encrypt_sign (ctx, rset,
73                                   plain, ciph);                                   opts? GPGME_ENCRYPT_ALWAYS_TRUST : (gpgme_encrypt_flags_t)0,
74      if (err)                                   plain, ciph);
75          goto leave;      if (err)
76            goto leave;
77      gpg_data_release_and_set_clipboard (ciph, 1);  
78      ciph = NULL;      gpg_data_release_to_clipboard (ciph, 1);
79        ciph = NULL;
80  leave:  
81      if (plain)  leave:
82          gpgme_data_release (plain);      if (plain)
83      if (ciph)          gpgme_data_release (plain);
84          gpgme_data_release (ciph);      if (ciph)
85      gpgme_key_release (key);          gpgme_data_release (ciph);
86      return err;      return err;
87  }  }
88    
89    
90  /* Dialog procedure for clipboard sign + encrypt. */  /* Dialog procedure for clipboard sign + encrypt. */
91  BOOL CALLBACK  BOOL CALLBACK
92  clip_signenc_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)  clip_signenc_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
93  {  {
94      static listview_ctrl_t lv = NULL;      static keylist_ctrl_t kl = NULL;
95      static keylist_t list = NULL;      static keylist_t list = NULL;
96      static int enable = 0;      static int keys_sortby = 0;
97      gpgme_keycache_t kc;      gpg_keycache_t kc;
98      gpgme_key_t *rset;      gpgme_key_t *rset;
99      gpgme_error_t err;      gpgme_error_t err;
100      gpgme_ctx_t ctx;      gpgme_ctx_t ctx;
101      passphrase_cb_s pwd;      passphrase_cb_s pwd;
102      char *signer = NULL;      char *signer = NULL;
103      int force_trust = 0, opt = 0;      size_t n;
104      int kmode = reg_prefs.keylist_mode? KEYLIST_ENCRYPT_MIN : KEYLIST_ENCRYPT;      int force_trust = 0;
105      int n;      
106            switch (msg) {
107      switch( msg ) {      case WM_INITDIALOG:
108      case WM_INITDIALOG:          SetWindowText (dlg, _("Sign & Encrypt"));
109          enable = 0;          SetDlgItemText (dlg, IDCANCEL, _("&Cancel"));
110      #ifndef LANG_DE          kc = keycache_get_ctx (KEYCACHE_PUB);
111          SetWindowText( dlg, _("Sign & Encrypt") );          if (!kc)
112      #endif              BUG (NULL);
113          kc = keycache_get_ctx( KEYCACHE_PUB );          
114          if( !kc )          kl = keylist_load (GetDlgItem (dlg, IDC_SIGNENC_KEYLIST),
115              BUG( NULL );                             kc, NULL, KEYLIST_ENCRYPT_MIN, KEY_SORT_USERID);
116          lv = keylist_load( GetDlgItem( dlg, IDC_SIGNENC_KEYLIST ),          seclist_init (dlg, IDC_SIGNENC_SECLIST, KEYLIST_FLAG_SHORT, &list);
117                             kc, NULL, kmode, KEY_SORT_USERID);          center_window (dlg, NULL);
118          seclist_init( dlg, IDC_SIGNENC_SECLIST, KEYLIST_FLAG_SHORT, &list );          EnableWindow (GetDlgItem (dlg, IDC_SIGNENC_SECLIST), FALSE);
119          center_window (dlg, NULL);          SetDlgItemText (dlg, IDC_SIGNENC_SELKEY, _("Select key for signing"));
120          set_active_window (dlg);          SetDlgItemText (dlg, IDC_SIGNENC_SECLISTINF, _("Signing key:"));
121          EnableWindow (GetDlgItem (dlg, IDC_SIGNENC_SECLIST), FALSE);          SetForegroundWindow (dlg);
122          SetDlgItemText (dlg, IDC_SIGNENC_SELKEY, _("Select key for signing"));          return TRUE;
123          SetDlgItemText (dlg, IDC_SIGNENC_SECLISTINF, _("Signing key:"));          
124          SetForegroundWindow (dlg);      case WM_DESTROY:
125          return TRUE;          seclist_destroy (&list);
126                    if (kl) {
127      case WM_DESTROY:              keylist_delete (kl);
128          seclist_destroy (&list);              kl = NULL;
129          reset_active_window ();          }
130          if (lv) {          return FALSE;
131              keylist_delete( lv );          
132              lv = NULL;      case WM_NOTIFY:
133          }          /* Wait until the dialog initialization is done */
134          return FALSE;          if (kl == NULL)
135                        break;
136      case WM_NOTIFY:          
137          NMHDR * notify;          int ret;
138          notify = (NMHDR *)lparam;          ret = keylist_listview_notify (dlg, kl->keys,
139          if( notify && notify->code == NM_DBLCLK                                         IDC_SIGNENC_KEYLIST, lparam);
140              && notify->idFrom == IDC_SIGNENC_KEYLIST )          if (ret != 0) {
141              PostMessage( dlg, WM_COMMAND, MAKEWPARAM(IDOK, 0), NULL );              SetWindowLong (dlg, DWL_MSGRESULT, ret);
142          return TRUE;              return TRUE;
143                    }
144      case WM_SYSCOMMAND:          
145          if( LOWORD (wparam) == SC_CLOSE )          NMHDR *notify;
146              EndDialog( dlg, TRUE );          notify = (NMHDR *)lparam;
147          return FALSE;          if (notify && notify->code == NM_DBLCLK &&
148                        notify->idFrom == IDC_SIGNENC_KEYLIST)
149      case WM_COMMAND:              PostMessage (dlg, WM_COMMAND, MAKEWPARAM (IDOK, 0), 0);
150          /* fixme: the enable seems to have a sync problem */          
151          if( HIWORD( wparam ) == BN_CLICKED          if (notify && notify->code == LVN_COLUMNCLICK &&
152              && LOWORD( wparam ) == IDC_SIGNENC_SELKEY ) {              notify->idFrom == IDC_SIGNENC_KEYLIST) {
153              enable ^= 1;              NMLISTVIEW *p = (LPNMLISTVIEW) lparam;
154              EnableWindow( GetDlgItem( dlg, IDC_SIGNENC_SECLIST ), enable? TRUE : FALSE );              int sortby = 0;
155          }  
156          switch( LOWORD( wparam ) ) {              switch (p->iSubItem) {
157          case IDOK:              case  0: sortby = KEY_SORT_USERID; break;
158              rset = keylist_get_recipients( lv, &force_trust, &n );              case  1: sortby = KEY_SORT_KEYID; break;
159              if (!n) {              case  2: sortby = KEY_SORT_LEN; break;
160                  msg_box( dlg, _("You must select at least one key."), _("Sign & Encrypt"), MB_ERR );              case  4: sortby = KEY_SORT_VALIDITY; break;
161                  return FALSE;              default: sortby = KEY_SORT_USERID; break;
162              }              }
163              if( IsDlgButtonChecked( dlg, IDC_SIGNENC_SELKEY ) ) {              if ((keys_sortby & ~KEYLIST_SORT_DESC) == sortby)
164                  gpgme_key_t key;                  keys_sortby ^= KEYLIST_SORT_DESC;
165                  const char * s;              else
166                    keys_sortby = sortby;
167                  if( seclist_select_key( dlg, IDC_SIGNENC_SECLIST, &key ) ) {              keylist_sort (kl, keys_sortby);
168                      msg_box( dlg, _("No key was selected."), _("Signing"), MB_ERR );              return TRUE;
169                      return FALSE;          }        
170                  }          break;
171                  s = key->subkeys->keyid;          
172                  if (s)      case WM_COMMAND:
173                      signer = m_strdup(s+8);          if (HIWORD (wparam) == BN_CLICKED &&
174              }              LOWORD (wparam) == IDC_SIGNENC_SELKEY) {
175              else {              int enable = IsDlgButtonChecked (dlg, IDC_SIGNENC_SELKEY);
176                  signer = get_gnupg_default_key ();              EnableWindow (GetDlgItem (dlg, IDC_SIGNENC_SECLIST), enable? TRUE : FALSE);
177                  if (!signer) {              break;
178                      msg_box (dlg, _("Could not get default key."), _("Signing"), MB_ERR);          }
179                      return FALSE;          
180                  }          switch (LOWORD (wparam)) {
181              }          case IDOK:
182                rset = keylist_get_recipients (kl, &force_trust, &n);
183              err = gpgme_new (&ctx);              if (!n) {
184              if (err)                  msg_box (dlg, _("You must select at least one key."),
185                  BUG (NULL);                           _("Sign & Encrypt"), MB_ERR);
186              set_gpg_passphrase_cb (&pwd, ctx, GPG_CMD_SIGN, dlg, _("Sign & Encrypt"));                  return FALSE;
187              err = gpg_clip_sign_encrypt (ctx, signer, rset, force_trust);              }
188              release_gpg_passphrase_cb (&pwd);              if (IsDlgButtonChecked (dlg, IDC_SIGNENC_SELKEY)) {
189              free (rset);                  gpgme_key_t key;
190              free_if_alloc (signer);                  const char *s;
191              gpgme_release (ctx);  
192              if (gpgme_err_code (err) ==  GPG_ERR_BAD_PASSPHRASE)                  if (seclist_select_key (dlg, IDC_SIGNENC_SECLIST, &key)) {
193                  agent_del_cache (pwd.keyid);                      msg_box (dlg, _("No key was selected."), _("Signing"), MB_ERR);
194              if (err) {                      safe_free (rset);
195                  msg_box (dlg, gpgme_strerror (err), _("Sign & Encrypt"), MB_ERR );                      return FALSE;
196                  return FALSE;                  }
197              }                  s = key->subkeys->keyid;
198              show_msg (dlg, 1500, _("GnuPG Status: Finished"));                  if (s)
199              EndDialog (dlg, TRUE);                      signer = m_strdup(s+8);
200              return TRUE;              }
201                            else {
202          case IDCANCEL:                  signer = get_gnupg_default_key ();
203              EndDialog( dlg, FALSE );                  if (!signer) {
204              return FALSE;                      msg_box (dlg, _("Could not get default key."), _("Signing"), MB_ERR);
205          }                      safe_free (rset);
206          break;                      return FALSE;
207      }                  }
208                    }
209      return FALSE;  
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.24  
changed lines
  Added in v.451

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26