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

Legend:
Removed from v.34  
changed lines
  Added in v.260

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26