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

Legend:
Removed from v.23  
changed lines
  Added in v.176

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26