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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26