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

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26