/[winpt]/trunk/Src/wptClipSignDlg.cpp
ViewVC logotype

Diff of /trunk/Src/wptClipSignDlg.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 48 by werner, Mon Oct 31 21:14:11 2005 UTC
# Line 1  Line 1 
1  /* wptClipSignDlg.cpp - WinPT clipboard sign dialog  /* wptClipSignDlg.cpp - WinPT clipboard sign dialog
2   *      Copyright (C) 2000, 2001, 2002, 2003, 2005 Timo Schulz   *      Copyright (C) 2000, 2001, 2002, 2003, 2005 Timo Schulz
3   *      Copyright (C) 2005 g10 Code GmbH   *      Copyright (C) 2005 g10 Code GmbH
4   *   *
5   * This file is part of WinPT.   * This file is part of WinPT.
6   *   *
7   * WinPT is free software; you can redistribute it and/or modify   * WinPT is free software; you can redistribute it and/or modify
8   * 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
9   * the Free Software Foundation; either version 2 of the License, or   * the Free Software Foundation; either version 2 of the License, or
10   * (at your option) any later version.   * (at your option) any later version.
11   *   *
12   * WinPT is distributed in the hope that it will be useful,   * WinPT is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Public License for more details.   * GNU General Public License for more details.
16   *   *
17   * You should have received a copy of the GNU General Public License   * You should have received a copy of the GNU General Public License
18   * along with WinPT; if not, write to the Free Software Foundation,   * along with WinPT; if not, write to the Free Software Foundation,
19   * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA   * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
20   */   */
21    
22  #include <windows.h>  #ifdef HAVE_CONFIG_H
23  #include <commctrl.h>  #include <config.h>
24    #endif
25  #include "../resource.h"  
26  #include "wptTypes.h"  #include <windows.h>
27  #include "wptAgent.h"  #include <commctrl.h>
28  #include "wptNLS.h"  
29  #include "wptCrypto.h"  #include "resource.h"
30  #include "wptGPG.h"  #include "wptTypes.h"
31  #include "wptCommonCtl.h"  #include "wptAgent.h"
32  #include "wptRegistry.h"  #include "wptNLS.h"
33  #include "wptKeylist.h"  #include "wptCrypto.h"
34  #include "wptErrors.h"  #include "wptGPG.h"
35  #include "wptW32API.h"  #include "wptCommonCtl.h"
36  #include "wptVersion.h"  #include "wptRegistry.h"
37  #include "wptContext.h" /* for passphrase_s */  #include "wptKeylist.h"
38  #include "wptDlgs.h"  #include "wptErrors.h"
39    #include "wptW32API.h"
40    #include "wptVersion.h"
41  /* Sign the clipboard contents with the key @keyid and wrap  #include "wptContext.h" /* for passphrase_s */
42     text lines to @wraplen (0 disable line wrapping).  #include "wptDlgs.h"
43     Return value: 0 on success. */  
44  gpgme_error_t  
45  gpg_clip_sign (gpgme_ctx_t ctx, const char *keyid, int wraplen)  /* Sign the clipboard contents with the key @keyid and wrap
46  {     text lines to @wraplen (0 disable line wrapping).
47      gpgme_error_t err;     Return value: 0 on success. */
48      gpgme_data_t plain = NULL;  gpgme_error_t
49      gpgme_data_t sig = NULL;  gpg_clip_sign (gpgme_ctx_t ctx, const char *keyid, int wraplen)
50      gpgme_key_t key = NULL;  {
51            gpgme_error_t err;
52      if (!keyid)      gpgme_data_t plain = NULL;
53          return gpg_error (GPG_ERR_INV_ARG);      gpgme_data_t sig = NULL;
54            gpgme_key_t key = NULL;
55      gpgme_set_armor (ctx, 1);      
56            if (!keyid)
57      err = gpg_data_new_from_clipboard (&plain, wraplen);          return gpg_error (GPG_ERR_INV_ARG);
58      if (err)      
59          return err;      gpgme_set_armor (ctx, 1);
60        
61      get_pubkey (keyid, &key);      err = gpg_data_new_from_clipboard (&plain, wraplen);
62      if (key)      if (err)
63          err = gpgme_signers_add (ctx, key);          return err;
64      else {  
65          err = gpg_error (GPG_ERR_NO_PUBKEY);      get_pubkey (keyid, &key);
66          goto leave;      if (key)
67      }          err = gpgme_signers_add (ctx, key);
68      err = gpgme_data_new (&sig);      else {
69      if (err)          err = gpg_error (GPG_ERR_NO_PUBKEY);
70          goto leave;              goto leave;
71      err = gpgme_op_sign (ctx, plain, sig, GPGME_SIG_MODE_CLEAR);      }
72      if (err)      err = gpgme_data_new (&sig);
73          goto leave;      if (err)
74                goto leave;    
75      gpg_data_release_and_set_clipboard (sig, 1);      err = gpgme_op_sign (ctx, plain, sig, GPGME_SIG_MODE_CLEAR);
76      sig = NULL;          if (err)
77            goto leave;
78  leave:      
79      if (plain)      gpg_data_release_and_set_clipboard (sig, 1);
80          gpgme_data_release (plain);      sig = NULL;    
81      if (sig)  
82          gpgme_data_release (sig);  leave:
83      return err;      if (plain)
84  }          gpgme_data_release (plain);
85        if (sig)
86            gpgme_data_release (sig);
87  /* This function is used when only one secret key is available.      return err;
88   * it doesn't make sense to offer a dialog for this case.  }
89   */  
90  void  
91  one_key_proc (HWND dlg)  /* This function is used when only one secret key is available.
92  {   * it doesn't make sense to offer a dialog for this case.
93      char * signer;   */
94      gpgme_ctx_t ctx;  void
95      gpgme_error_t err;  one_key_proc (HWND dlg)
96      passphrase_cb_s pwd;  {
97      int rc = 0;      char * signer;
98      int n = reg_prefs.word_wrap;      gpgme_ctx_t ctx;
99            gpgme_error_t err;
100      signer = get_gnupg_default_key ();      passphrase_cb_s pwd;
101      if (!signer) {      int rc = 0;
102          msg_box (dlg, _("Could not get default key."), _("Signing"), MB_ERR);      int n = reg_prefs.word_wrap;
103          return;      
104      }      signer = get_gnupg_default_key ();
105            if (!signer) {
106      err = gpgme_new (&ctx);          msg_box (dlg, _("Could not get default key."), _("Signing"), MB_ERR);
107      if (err)          return;
108          BUG (dlg);      }
109        
110      set_gpg_passphrase_cb (&pwd, ctx, GPG_CMD_SIGN, dlg, _("Signing"));      err = gpgme_new (&ctx);
111      err = gpg_clip_sign (ctx, signer, n );      if (err)
112      wipememory (pwd.pwd, sizeof (pwd.pwd));          BUG (dlg);
113      if (gpgme_err_code (err) == GPG_ERR_BAD_PASSPHRASE)  
114          agent_del_cache (pwd.keyid);      set_gpg_passphrase_cb (&pwd, ctx, GPG_CMD_SIGN, dlg, _("Signing"));
115      if (err)      err = gpg_clip_sign (ctx, signer, n );
116          msg_box (dlg, gpgme_strerror (err), _("Signing"), MB_ERR);      wipememory (pwd.pwd, sizeof (pwd.pwd));
117      else      if (gpgme_err_code (err) == GPG_ERR_BAD_PASSPHRASE)
118          show_msg (dlg, 1500, _("GnuPG Status: Finished"));          agent_del_cache (pwd.keyid);
119      gpgme_release (ctx);      if (err)
120      free_if_alloc (signer);          msg_box (dlg, gpgme_strerror (err), _("Signing"), MB_ERR);
121  }      else
122            show_msg (dlg, 1500, _("GnuPG Status: Finished"));
123        gpgme_release (ctx);
124  /* Dialog box procedure for clipboard signing. */      free_if_alloc (signer);
125  BOOL CALLBACK  }
126  clip_sign_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)  
127  {        
128      static listview_ctrl_t lv = NULL;  /* Dialog box procedure for clipboard signing. */
129      gpg_keycache_t kc, sec_kc;  BOOL CALLBACK
130      gpgme_ctx_t ctx;  clip_sign_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
131      gpgme_error_t err;  {      
132      passphrase_cb_s pwd;      static listview_ctrl_t lv = NULL;
133      int lv_idx = 0;      gpg_keycache_t kc, sec_kc;
134      int rc = 0, no_signer = 0;      gpgme_ctx_t ctx;
135      char * signer = NULL;      gpgme_error_t err;
136            passphrase_cb_s pwd;
137      switch( msg ) {      int lv_idx = 0;
138      case WM_INITDIALOG:      int rc = 0, no_signer = 0;
139          SetWindowText (dlg, _("Signing"));      char * signer = NULL;
140        
141          kc = keycache_get_ctx (KEYCACHE_PUB);      switch( msg ) {
142          if (!kc)      case WM_INITDIALOG:
143              BUG( NULL );          SetWindowText (dlg, _("Signing"));
144          sec_kc = keycache_get_ctx (KEYCACHE_PRV);  
145          if (!sec_kc)          kc = keycache_get_ctx (KEYCACHE_PUB);
146              BUG (dlg);          if (!kc)
147          if (gpg_keycache_get_size (sec_kc) == 1) {              BUG( NULL );
148              one_key_proc (dlg);          sec_kc = keycache_get_ctx (KEYCACHE_PRV);
149              EndDialog (dlg, TRUE);          if (!sec_kc)
150              return FALSE;              BUG (dlg);
151          }          if (gpg_keycache_get_size (sec_kc) == 1) {
152          lv = keylist_load (GetDlgItem (dlg, IDC_SIGN_KEYLIST), kc, sec_kc,              one_key_proc (dlg);
153                             KEYLIST_SIGN, KEY_SORT_USERID);              EndDialog (dlg, TRUE);
154          center_window (dlg, NULL);              return FALSE;
155          SetForegroundWindow (dlg);          }
156          set_active_window (dlg);          lv = keylist_load (GetDlgItem (dlg, IDC_SIGN_KEYLIST), kc, sec_kc,
157          return FALSE;                               KEYLIST_SIGN, KEY_SORT_USERID);
158                    center_window (dlg, NULL);
159      case WM_DESTROY:          SetForegroundWindow (dlg);
160          reset_active_window ();          set_active_window (dlg);
161          if (lv) {          return FALSE;  
162              keylist_delete (lv);          
163              lv = NULL;      case WM_DESTROY:
164          }          reset_active_window ();
165          return FALSE;          if (lv) {
166                        keylist_delete (lv);
167      case WM_NOTIFY:              lv = NULL;
168          NMHDR * notify;          }
169          notify = (NMHDR *)lparam;          return FALSE;
170          if( notify && notify->code == NM_DBLCLK          
171              && notify->idFrom == IDC_SIGN_KEYLIST )      case WM_NOTIFY:
172              PostMessage( dlg, WM_COMMAND, MAKEWPARAM(IDOK, 0), NULL );          NMHDR * notify;
173          return TRUE;          notify = (NMHDR *)lparam;
174                    if( notify && notify->code == NM_DBLCLK
175      case WM_SYSCOMMAND:              && notify->idFrom == IDC_SIGN_KEYLIST )
176          if( LOWORD (wparam) == SC_CLOSE )              PostMessage( dlg, WM_COMMAND, MAKEWPARAM(IDOK, 0), NULL );
177              EndDialog(dlg, TRUE);          return TRUE;
178          return FALSE;          
179                case WM_SYSCOMMAND:
180      case WM_COMMAND:          if( LOWORD (wparam) == SC_CLOSE )
181          switch (LOWORD (wparam)) {              EndDialog(dlg, TRUE);
182          case IDOK:          return FALSE;
183              signer = get_gnupg_default_key ();          
184              if (!signer) {      case WM_COMMAND:
185                  msg_box( dlg, _("Could not get default key."), _("Signing"), MB_ERR );          switch (LOWORD (wparam)) {
186                  return FALSE;          case IDOK:
187              }              signer = get_gnupg_default_key ();
188              if (listview_count_items (lv, 0) == 1) {              if (!signer) {
189                  listview_get_item_text (lv, 0, 1, signer, sizeof signer-1);                  msg_box( dlg, _("Could not get default key."), _("Signing"), MB_ERR );
190                  no_signer = 0;                  return FALSE;
191              }              }
192              else if ((lv_idx = listview_get_curr_pos (lv)) == -1) {              if (listview_count_items (lv, 0) == 1) {
193                  rc = log_box (_("Signing"), MB_YESNO,                  listview_get_item_text (lv, 0, 1, signer, sizeof signer-1);
194                                _("No key was chosen.\nUse the GPG default key '%s'?"),                  no_signer = 0;
195                                signer);              }
196                  if (rc == IDNO) {              else if ((lv_idx = listview_get_curr_pos (lv)) == -1) {
197                      free_if_alloc (signer);                  rc = log_box (_("Signing"), MB_YESNO,
198                      return FALSE;                                _("No key was chosen.\nUse the GPG default key '%s'?"),
199                  }                                signer);
200                  no_signer = 1;                  if (rc == IDNO) {
201              }                      free_if_alloc (signer);
202              if (!no_signer) {                      return FALSE;
203                  free_if_alloc (signer);                  }
204                  signer = new char[32+1];                  no_signer = 1;
205                  if (!signer)              }
206                      BUG (NULL);              if (!no_signer) {
207                  listview_get_item_text (lv, lv_idx, 1, signer, 32);                  free_if_alloc (signer);
208              }                  signer = new char[32+1];
209              err = gpgme_new (&ctx);                  if (!signer)
210              if (err)                      BUG (NULL);
211                  BUG (dlg);                  listview_get_item_text (lv, lv_idx, 1, signer, 32);
212              set_gpg_passphrase_cb (&pwd, ctx, GPG_CMD_SIGN, dlg, _("Signing"));              }
213              err = gpg_clip_sign (ctx, signer, reg_prefs.word_wrap);              err = gpgme_new (&ctx);
214              free_if_alloc (signer);              if (err)
215              release_gpg_passphrase_cb (&pwd);                  BUG (dlg);
216                set_gpg_passphrase_cb (&pwd, ctx, GPG_CMD_SIGN, dlg, _("Signing"));
217              if (pwd.cancel && gpgme_err_code(err) == GPG_ERR_BAD_PASSPHRASE) {              err = gpg_clip_sign (ctx, signer, reg_prefs.word_wrap);
218                  /* The user hit the cancel button or bad passphrase */              free_if_alloc (signer);
219                  gpgme_release (ctx);              release_gpg_passphrase_cb (&pwd);
220                  EndDialog (dlg, TRUE);  
221                  return TRUE;              if (pwd.cancel && gpgme_err_code(err) == GPG_ERR_BAD_PASSPHRASE) {
222              }                  /* The user hit the cancel button or bad passphrase */
223              if (err) {                  gpgme_release (ctx);
224                  msg_box (dlg, gpgme_strerror (err), _("Signing"), MB_ERR);                  EndDialog (dlg, TRUE);
225                  gpgme_release (ctx);                  return TRUE;
226                  return FALSE;              }
227              }              if (err) {
228              else                                      msg_box (dlg, gpgme_strerror (err), _("Signing"), MB_ERR);
229                  show_msg( dlg, 1500, _("GnuPG Status: Finished") );                  gpgme_release (ctx);
230              gpgme_release (ctx);                  return FALSE;
231              EndDialog (dlg, TRUE);              }
232              return TRUE;              else                    
233                                show_msg( dlg, 1500, _("GnuPG Status: Finished") );
234          case IDCANCEL:              gpgme_release (ctx);
235              EndDialog (dlg, FALSE);              EndDialog (dlg, TRUE);
236              return FALSE;              return TRUE;
237          }              
238          break;          case IDCANCEL:
239      }              EndDialog (dlg, FALSE);
240                    return FALSE;
241      return FALSE;          }
242  }          break;
243        }
244        
245        return FALSE;
246    }
247    

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26