/[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 34 by twoaday, Wed Oct 26 11:20:09 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 "wptCrypto.h"  #include "wptGPG.h"
28  #include "wptGPG.h"  #include "wptCommonCtl.h"
29  #include "wptCommonCtl.h"  #include "wptKeylist.h"
30  #include "wptKeylist.h"  #include "wptTypes.h"
31  #include "wptTypes.h"  #include "wptNLS.h"
32  #include "wptNLS.h"  #include "wptContext.h" /* for passphrase_s */
33  #include "wptContext.h" /* for passphrase_s */  #include "wptDlgs.h"
34  #include "wptDlgs.h"  #include "wptW32API.h"
35  #include "wptW32API.h"  #include "wptKeylist.h"
36  #include "wptKeylist.h"  #include "wptVersion.h"
37  #include "wptVersion.h"  #include "wptGPG.h"
38  #include "wptGPG.h"  #include "wptRegistry.h"
39  #include "wptRegistry.h"  #include "wptUTF8.h"
40  #include "wptUTF8.h"  
41    
42    /* Encrypt the clipboard data with the recipients from @rset and
43  /* Encrypt the clipboard data with the recipients from @rset and     additionally sign the data before @signer as the keyID.
44     additionally sign the data before @signer as the keyID.     Return value: 0 on success. */
45     Return value: 0 on success. */  gpgme_error_t
46  gpgme_error_t  gpg_clip_sign_encrypt (gpgme_ctx_t ctx, const char *signer,
47  gpg_clip_sign_encrypt (gpgme_ctx_t ctx, const char *signer,                         gpgme_key_t *rset, int opts)
48                         gpgme_key_t *rset, int opts)  {
49  {      gpgme_error_t err;
50      gpgme_error_t err;      gpgme_data_t plain = NULL;
51      gpgme_data_t plain = NULL;      gpgme_data_t ciph = NULL;
52      gpgme_data_t ciph = NULL;      gpgme_key_t key = NULL;
53      gpgme_key_t key = NULL;  
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      gpgme_set_armor (ctx, 1);      gpgme_set_textmode (ctx, 1);
61            
62      err = gpg_data_new_from_clipboard (&plain, 0);      err = gpg_data_utf8_new_from_clipboard (&plain, 0, NULL);
63      if (err)      if (err)
64          goto leave;            goto leave;  
65      err = gpgme_data_new (&ciph);      err = gpgme_data_new (&ciph);
66      if (err)      if (err)
67          goto leave;          goto leave;
68      err = gpgme_signers_add (ctx, key);      err = gpgme_signers_add (ctx, key);
69      if (err)      if (err)
70          goto leave;          goto leave;
71    
72      err = gpgme_op_encrypt_sign (ctx, rset,      err = gpgme_op_encrypt_sign (ctx, rset,
73                                   opts? GPGME_ENCRYPT_ALWAYS_TRUST : (gpgme_encrypt_flags_t)0,                                   opts? GPGME_ENCRYPT_ALWAYS_TRUST : (gpgme_encrypt_flags_t)0,
74                                   plain, ciph);                                   plain, ciph);
75      if (err)      if (err)
76          goto leave;          goto leave;
77    
78      gpg_data_release_and_set_clipboard (ciph, 1);      gpg_data_release_to_clipboard (ciph, 1);
79      ciph = NULL;      ciph = NULL;
80    
81  leave:  leave:
82      if (plain)      if (plain)
83          gpgme_data_release (plain);          gpgme_data_release (plain);
84      if (ciph)      if (ciph)
85          gpgme_data_release (ciph);          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      gpg_keycache_t kc;      static int keys_sortby = 0;
97      gpgme_key_t *rset;      gpg_keycache_t kc;
98      gpgme_error_t err;      gpgme_key_t *rset;
99      gpgme_ctx_t ctx;      gpgme_error_t err;
100      passphrase_cb_s pwd;      gpgme_ctx_t ctx;
101      char *signer = NULL;      passphrase_cb_s pwd;
102      int force_trust = 0, opt = 0;      char *signer = NULL;
103      int kmode = reg_prefs.keylist_mode? KEYLIST_ENCRYPT_MIN : KEYLIST_ENCRYPT;      size_t n;
104      int n;      int force_trust = 0;
105            
106      switch( msg ) {      switch (msg) {
107      case WM_INITDIALOG:      case WM_INITDIALOG:
108          SetWindowText (dlg, _("Sign & Encrypt"));          SetWindowText (dlg, _("Sign & Encrypt"));
109            SetDlgItemText (dlg, IDCANCEL, _("&Cancel"));
110          kc = keycache_get_ctx( KEYCACHE_PUB );          kc = keycache_get_ctx (KEYCACHE_PUB);
111          if( !kc )          kl = keylist_load (GetDlgItem (dlg, IDC_SIGNENC_KEYLIST),
112              BUG( NULL );                             kc, NULL, KEYLIST_ENCRYPT_MIN, KEY_SORT_USERID);
113          lv = keylist_load( GetDlgItem( dlg, IDC_SIGNENC_KEYLIST ),          seclist_init (dlg, IDC_SIGNENC_SECLIST, KEYLIST_FLAG_SHORT, &list);
114                             kc, NULL, kmode, KEY_SORT_USERID);          center_window (dlg, NULL);
115          seclist_init( dlg, IDC_SIGNENC_SECLIST, KEYLIST_FLAG_SHORT, &list );          EnableWindow (GetDlgItem (dlg, IDC_SIGNENC_SECLIST), FALSE);
116          center_window (dlg, NULL);          SetDlgItemText (dlg, IDC_SIGNENC_SELKEY, _("Select key for signing"));
117          set_active_window (dlg);          SetDlgItemText (dlg, IDC_SIGNENC_SECLISTINF, _("Signing key:"));
118          EnableWindow (GetDlgItem (dlg, IDC_SIGNENC_SECLIST), FALSE);          SetForegroundWindow (dlg);
119          SetDlgItemText (dlg, IDC_SIGNENC_SELKEY, _("Select key for signing"));          return TRUE;
120          SetDlgItemText (dlg, IDC_SIGNENC_SECLISTINF, _("Signing key:"));          
121          SetForegroundWindow (dlg);      case WM_DESTROY:
122          return TRUE;          seclist_destroy (&list);
123                    if (kl) {
124      case WM_DESTROY:              keylist_delete (kl);
125          seclist_destroy (&list);              kl = NULL;
126          reset_active_window ();          }
127          if (lv) {          return FALSE;
128              keylist_delete (lv);          
129              lv = NULL;      case WM_NOTIFY:
130          }          int ret;
131          return FALSE;          ret = keylist_listview_notify (dlg, kl->keys,
132                                                   IDC_SIGNENC_KEYLIST, lparam);
133      case WM_NOTIFY:          if (ret != 0) {
134          NMHDR * notify;              SetWindowLong (dlg, DWL_MSGRESULT, ret);
135          notify = (NMHDR *)lparam;              return TRUE;
136          if (notify && notify->code == NM_DBLCLK          }
137              && notify->idFrom == IDC_SIGNENC_KEYLIST)          
138              PostMessage (dlg, WM_COMMAND, MAKEWPARAM (IDOK, 0), NULL);          NMHDR *notify;
139          return TRUE;          notify = (NMHDR *)lparam;
140                    if (notify && notify->code == NM_DBLCLK
141      case WM_SYSCOMMAND:              && notify->idFrom == IDC_SIGNENC_KEYLIST)
142          if( LOWORD (wparam) == SC_CLOSE )              PostMessage (dlg, WM_COMMAND, MAKEWPARAM (IDOK, 0), 0);
143              EndDialog( dlg, TRUE );          
144          return FALSE;          if (notify && notify->code == LVN_COLUMNCLICK &&
145                        notify->idFrom == IDC_SIGNENC_KEYLIST) {
146      case WM_COMMAND:              NMLISTVIEW *p = (LPNMLISTVIEW) lparam;
147          if (HIWORD (wparam) == BN_CLICKED              int sortby = 0;
148              && LOWORD (wparam) == IDC_SIGNENC_SELKEY) {  
149              int enable = IsDlgButtonChecked (dlg, IDC_SIGNENC_SELKEY);              switch (p->iSubItem) {
150              EnableWindow (GetDlgItem (dlg, IDC_SIGNENC_SECLIST), enable? TRUE : FALSE);              case  0: sortby = KEY_SORT_USERID; break;
151          }              case  1: sortby = KEY_SORT_KEYID; break;
152          switch( LOWORD( wparam ) ) {              case  2: sortby = KEY_SORT_LEN; break;
153          case IDOK:              case  4: sortby = KEY_SORT_VALIDITY; break;
154              rset = keylist_get_recipients( lv, &force_trust, &n );              default: sortby = KEY_SORT_USERID; break;
155              if (!n) {              }
156                  msg_box( dlg, _("You must select at least one key."), _("Sign & Encrypt"), MB_ERR );              if ((keys_sortby & ~KEYLIST_SORT_DESC) == sortby)
157                  return FALSE;                  keys_sortby ^= KEYLIST_SORT_DESC;
158              }              else
159              if( IsDlgButtonChecked( dlg, IDC_SIGNENC_SELKEY ) ) {                  keys_sortby = sortby;
160                  gpgme_key_t key;              keylist_sort (kl, keys_sortby);
161                  const char * s;          }
162            return TRUE;
163                  if( seclist_select_key( dlg, IDC_SIGNENC_SECLIST, &key ) ) {          
164                      msg_box( dlg, _("No key was selected."), _("Signing"), MB_ERR );      case WM_COMMAND:
165                      return FALSE;          if (HIWORD (wparam) == BN_CLICKED
166                  }              && LOWORD (wparam) == IDC_SIGNENC_SELKEY) {
167                  s = key->subkeys->keyid;              int enable = IsDlgButtonChecked (dlg, IDC_SIGNENC_SELKEY);
168                  if (s)              EnableWindow (GetDlgItem (dlg, IDC_SIGNENC_SECLIST), enable? TRUE : FALSE);
169                      signer = m_strdup(s+8);          }
170              }          switch (LOWORD (wparam)) {
171              else {          case IDOK:
172                  signer = get_gnupg_default_key ();              rset = keylist_get_recipients (kl, &force_trust, &n);
173                  if (!signer) {              if (!n) {
174                      msg_box (dlg, _("Could not get default key."), _("Signing"), MB_ERR);                  msg_box (dlg, _("You must select at least one key."),
175                      return FALSE;                           _("Sign & Encrypt"), MB_ERR);
176                  }                  return FALSE;
177              }              }
178                if (IsDlgButtonChecked (dlg, IDC_SIGNENC_SELKEY)) {
179              err = gpgme_new (&ctx);                  gpgme_key_t key;
180              if (err)                  const char *s;
181                  BUG (NULL);  
182              set_gpg_passphrase_cb (&pwd, ctx, GPG_CMD_SIGN, dlg, _("Sign & Encrypt"));                  if (seclist_select_key (dlg, IDC_SIGNENC_SECLIST, &key)) {
183              err = gpg_clip_sign_encrypt (ctx, signer, rset, force_trust);                      msg_box (dlg, _("No key was selected."), _("Signing"), MB_ERR);
184              release_gpg_passphrase_cb (&pwd);                      safe_free (rset);
185              free (rset);                      return FALSE;
186              free_if_alloc (signer);                  }
187              gpgme_release (ctx);                  s = key->subkeys->keyid;
188              if (gpgme_err_code (err) ==  GPG_ERR_BAD_PASSPHRASE)                  if (s)
189                  agent_del_cache (pwd.keyid);                      signer = m_strdup(s+8);
190              if (err) {              }
191                  msg_box (dlg, gpgme_strerror (err), _("Sign & Encrypt"), MB_ERR );              else {
192                  return FALSE;                  signer = get_gnupg_default_key ();
193              }                  if (!signer) {
194              show_msg (dlg, 1500, _("GnuPG Status: Finished"));                      msg_box (dlg, _("Could not get default key."), _("Signing"), MB_ERR);
195              EndDialog (dlg, TRUE);                      safe_free (rset);
196              return TRUE;                      return FALSE;
197                                }
198          case IDCANCEL:              }
199              EndDialog( dlg, FALSE );  
200              return FALSE;              err = gpgme_new (&ctx);
201          }              if (err)
202          break;                  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      return FALSE;              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.34  
changed lines
  Added in v.328

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26