/[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 25 by twoaday, Wed Oct 12 10:04:26 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 "wptGPG.h"  #include "resource.h"
30  #include "wptCommonCtl.h"  #include "wptTypes.h"
31  #include "wptRegistry.h"  #include "wptAgent.h"
32  #include "wptKeylist.h"  #include "wptNLS.h"
33  #include "wptErrors.h"  #include "wptCrypto.h"
34  #include "wptW32API.h"  #include "wptGPG.h"
35  #include "wptVersion.h"  #include "wptCommonCtl.h"
36  #include "wptContext.h" /* for passphrase_s */  #include "wptRegistry.h"
37  #include "wptDlgs.h"  #include "wptKeylist.h"
38    #include "wptErrors.h"
39  /* Sign the clipboard contents with the key @keyid and wrap  #include "wptW32API.h"
40     text lines to @wraplen (0 disable line wrapping).  #include "wptVersion.h"
41     Return value: 0 on success. */  #include "wptContext.h" /* for passphrase_s */
42  gpgme_error_t  #include "wptDlgs.h"
43  gpg_clip_sign (gpgme_ctx_t ctx, const char *keyid, int wraplen)  
44  {  
45      gpgme_error_t err;  /* Sign the clipboard contents with the key @keyid and wrap
46      gpgme_data_t plain = NULL;     text lines to @wraplen (0 disable line wrapping).
47      gpgme_data_t sig = NULL;     Return value: 0 on success. */
48      gpgme_key_t key = NULL;  gpgme_error_t
49        gpg_clip_sign (gpgme_ctx_t ctx, const char *keyid, int wraplen)
50      if (!keyid)  {
51          return gpg_error (GPG_ERR_INV_ARG);      gpgme_error_t err;
52            gpgme_data_t plain = NULL;
53      gpgme_set_armor (ctx, 1);      gpgme_data_t sig = NULL;
54            gpgme_key_t key = NULL;
55      err = gpg_data_new_from_clipboard (&plain, wraplen);      
56      if (err)      if (!keyid)
57          return err;          return gpg_error (GPG_ERR_INV_ARG);
58        
59      get_pubkey (keyid, &key);      gpgme_set_armor (ctx, 1);
60      if (key)      
61          err = gpgme_signers_add (ctx, key);      err = gpg_data_new_from_clipboard (&plain, wraplen);
62      else {      if (err)
63          err = gpg_error (GPG_ERR_NO_PUBKEY);          return err;
64          goto leave;  
65      }      get_pubkey (keyid, &key);
66      err = gpgme_data_new (&sig);      if (key)
67      if (err)          err = gpgme_signers_add (ctx, key);
68          goto leave;          else {
69      err = gpgme_op_sign (ctx, plain, sig, GPGME_SIG_MODE_CLEAR);          err = gpg_error (GPG_ERR_NO_PUBKEY);
70      if (err)          goto leave;
71          goto leave;      }
72            err = gpgme_data_new (&sig);
73      gpg_data_release_and_set_clipboard (sig, 1);      if (err)
74      sig = NULL;              goto leave;    
75        err = gpgme_op_sign (ctx, plain, sig, GPGME_SIG_MODE_CLEAR);
76  leave:      if (err)
77      if (plain)          goto leave;
78          gpgme_data_release (plain);      
79      if (sig)      gpg_data_release_and_set_clipboard (sig, 1);
80          gpgme_data_release (sig);      sig = NULL;    
81      return err;  
82  }  leave:
83        if (plain)
84            gpgme_data_release (plain);
85  /* This function is used when only one secret key is available.      if (sig)
86   * it doesn't make sense to offer a dialog for this case.          gpgme_data_release (sig);
87   */      return err;
88  void  }
89  one_key_proc (HWND dlg)  
90  {  
91      char * signer;  /* This function is used when only one secret key is available.
92      gpgme_ctx_t ctx;   * it doesn't make sense to offer a dialog for this case.
93      gpgme_error_t err;   */
94      passphrase_cb_s pwd;  void
95      int rc = 0;  one_key_proc (HWND dlg)
96      int n = reg_prefs.word_wrap;  {
97            char * signer;
98      signer = get_gnupg_default_key ();      gpgme_ctx_t ctx;
99      if (!signer) {      gpgme_error_t err;
100          msg_box (dlg, _("Could not get default key."), _("Signing"), MB_ERR);      passphrase_cb_s pwd;
101          return;      int rc = 0;
102      }      int n = reg_prefs.word_wrap;
103            
104      err = gpgme_new (&ctx);      signer = get_gnupg_default_key ();
105      if (err)      if (!signer) {
106          BUG (dlg);          msg_box (dlg, _("Could not get default key."), _("Signing"), MB_ERR);
107            return;
108      set_gpg_passphrase_cb (&pwd, ctx, GPG_CMD_SIGN, dlg, _("Signing"));      }
109      err = gpg_clip_sign (ctx, signer, n );      
110      memset (pwd.pwd, 0, sizeof pwd.pwd);      err = gpgme_new (&ctx);
111      if (gpgme_err_code (err) == GPG_ERR_BAD_PASSPHRASE)      if (err)
112          agent_del_cache (pwd.keyid);          BUG (dlg);
113      if (err)  
114          msg_box (dlg, gpgme_strerror (err), _("Signing"), MB_ERR);      set_gpg_passphrase_cb (&pwd, ctx, GPG_CMD_SIGN, dlg, _("Signing"));
115      else      err = gpg_clip_sign (ctx, signer, n );
116          show_msg (dlg, 1500, _("GnuPG Status: Finished"));      wipememory (pwd.pwd, sizeof (pwd.pwd));
117      gpgme_release (ctx);      if (gpgme_err_code (err) == GPG_ERR_BAD_PASSPHRASE)
118      free_if_alloc (signer);          agent_del_cache (pwd.keyid);
119  }      if (err)
120            msg_box (dlg, gpgme_strerror (err), _("Signing"), MB_ERR);
121        else
122  /* Dialog box procedure for clipboard signing. */          show_msg (dlg, 1500, _("GnuPG Status: Finished"));
123  BOOL CALLBACK      gpgme_release (ctx);
124  clip_sign_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)      free_if_alloc (signer);
125  {        }
126      static listview_ctrl_t lv = NULL;  
127      gpg_keycache_t kc, sec_kc;  
128      gpgme_ctx_t ctx;  /* Dialog box procedure for clipboard signing. */
129      gpgme_error_t err;  BOOL CALLBACK
130      passphrase_cb_s pwd;  clip_sign_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
131      int lv_idx = 0;  {      
132      int rc = 0, no_signer = 0;      static listview_ctrl_t lv = NULL;
133      char * signer = NULL;      gpg_keycache_t kc, sec_kc;
134            gpgme_ctx_t ctx;
135      switch( msg ) {      gpgme_error_t err;
136      case WM_INITDIALOG:      passphrase_cb_s pwd;
137      #ifndef LANG_DE      int lv_idx = 0;
138          SetWindowText( dlg, _("Signing") );      int rc = 0, no_signer = 0;
139      #endif      char * signer = NULL;
140          kc = keycache_get_ctx (KEYCACHE_PUB);      
141          if (!kc)      switch( msg ) {
142              BUG( NULL );      case WM_INITDIALOG:
143          sec_kc = keycache_get_ctx (KEYCACHE_PRV);          SetWindowText (dlg, _("Signing"));
144          if (!sec_kc)  
145              BUG (dlg);          kc = keycache_get_ctx (KEYCACHE_PUB);
146          if (gpg_keycache_get_size (sec_kc) == 1) {          if (!kc)
147              one_key_proc (dlg);              BUG( NULL );
148              EndDialog (dlg, TRUE);          sec_kc = keycache_get_ctx (KEYCACHE_PRV);
149              return FALSE;          if (!sec_kc)
150          }              BUG (dlg);
151          lv = keylist_load (GetDlgItem (dlg, IDC_SIGN_KEYLIST), kc, sec_kc,          if (gpg_keycache_get_size (sec_kc) == 1) {
152                             KEYLIST_SIGN, KEY_SORT_USERID);              one_key_proc (dlg);
153          center_window (dlg, NULL);              EndDialog (dlg, TRUE);
154          SetForegroundWindow (dlg);              return FALSE;
155          set_active_window (dlg);          }
156          return FALSE;            lv = keylist_load (GetDlgItem (dlg, IDC_SIGN_KEYLIST), kc, sec_kc,
157                                       KEYLIST_SIGN, KEY_SORT_USERID);
158      case WM_DESTROY:          center_window (dlg, NULL);
159          reset_active_window ();          SetForegroundWindow (dlg);
160          if (lv) {          set_active_window (dlg);
161              keylist_delete (lv);          return FALSE;  
162              lv = NULL;          
163          }      case WM_DESTROY:
164          return FALSE;          reset_active_window ();
165                    if (lv) {
166      case WM_NOTIFY:              keylist_delete (lv);
167          NMHDR * notify;              lv = NULL;
168          notify = (NMHDR *)lparam;          }
169          if( notify && notify->code == NM_DBLCLK          return FALSE;
170              && notify->idFrom == IDC_SIGN_KEYLIST )          
171              PostMessage( dlg, WM_COMMAND, MAKEWPARAM(IDOK, 0), NULL );      case WM_NOTIFY:
172          return TRUE;          NMHDR * notify;
173                    notify = (NMHDR *)lparam;
174      case WM_SYSCOMMAND:          if( notify && notify->code == NM_DBLCLK
175          if( LOWORD (wparam) == SC_CLOSE )              && notify->idFrom == IDC_SIGN_KEYLIST )
176              EndDialog(dlg, TRUE);              PostMessage( dlg, WM_COMMAND, MAKEWPARAM(IDOK, 0), NULL );
177          return FALSE;          return TRUE;
178                    
179      case WM_COMMAND:      case WM_SYSCOMMAND:
180          switch (LOWORD (wparam)) {          if( LOWORD (wparam) == SC_CLOSE )
181          case IDOK:              EndDialog(dlg, TRUE);
182              signer = get_gnupg_default_key ();          return FALSE;
183              if (!signer) {          
184                  msg_box( dlg, _("Could not get default key."), _("Signing"), MB_ERR );      case WM_COMMAND:
185                  return FALSE;          switch (LOWORD (wparam)) {
186              }          case IDOK:
187              if (listview_count_items (lv, 0) == 1) {              signer = get_gnupg_default_key ();
188                  listview_get_item_text (lv, 0, 1, signer, sizeof signer-1);              if (!signer) {
189                  no_signer = 0;                  msg_box( dlg, _("Could not get default key."), _("Signing"), MB_ERR );
190              }                  return FALSE;
191              else if ((lv_idx = listview_get_curr_pos (lv)) == -1) {              }
192                  rc = log_box (_("Signing"), MB_YESNO,              if (listview_count_items (lv, 0) == 1) {
193                                _("No key was chosen.\nUse the GPG default key '%s'?"),                  listview_get_item_text (lv, 0, 1, signer, sizeof signer-1);
194                                signer);                  no_signer = 0;
195                  if (rc == IDNO) {              }
196                      free_if_alloc (signer);              else if ((lv_idx = listview_get_curr_pos (lv)) == -1) {
197                      return FALSE;                  rc = log_box (_("Signing"), MB_YESNO,
198                  }                                _("No key was chosen.\nUse the GPG default key '%s'?"),
199                  no_signer = 1;                                signer);
200              }                  if (rc == IDNO) {
201              if (!no_signer) {                      free_if_alloc (signer);
202                  free_if_alloc (signer);                      return FALSE;
203                  signer = new char[32+1];                  }
204                  if (!signer)                  no_signer = 1;
205                      BUG (NULL);              }
206                  listview_get_item_text (lv, lv_idx, 1, signer, 32);              if (!no_signer) {
207              }                  free_if_alloc (signer);
208              err = gpgme_new (&ctx);                  signer = new char[32+1];
209              if (err)                  if (!signer)
210                  BUG (dlg);                      BUG (NULL);
211              set_gpg_passphrase_cb (&pwd, ctx, GPG_CMD_SIGN, dlg, _("Signing"));                  listview_get_item_text (lv, lv_idx, 1, signer, 32);
212              err = gpg_clip_sign (ctx, signer, reg_prefs.word_wrap);              }
213              free_if_alloc (signer);              err = gpgme_new (&ctx);
214              release_gpg_passphrase_cb (&pwd);              if (err)
215                    BUG (dlg);
216              if (pwd.cancel && gpgme_err_code(err) == GPG_ERR_BAD_PASSPHRASE) {              set_gpg_passphrase_cb (&pwd, ctx, GPG_CMD_SIGN, dlg, _("Signing"));
217                  /* The user hit the cancel button or bad passphrase */              err = gpg_clip_sign (ctx, signer, reg_prefs.word_wrap);
218                  gpgme_release (ctx);              free_if_alloc (signer);
219                  EndDialog (dlg, TRUE);              release_gpg_passphrase_cb (&pwd);
220                  return TRUE;  
221              }              if (pwd.cancel && gpgme_err_code(err) == GPG_ERR_BAD_PASSPHRASE) {
222              if (err) {                  /* The user hit the cancel button or bad passphrase */
223                  msg_box (dlg, gpgme_strerror (err), _("Signing"), MB_ERR);                  gpgme_release (ctx);
224                  gpgme_release (ctx);                  EndDialog (dlg, TRUE);
225                  return FALSE;                  return TRUE;
226              }              }
227              else                                  if (err) {
228                  show_msg( dlg, 1500, _("GnuPG Status: Finished") );                  msg_box (dlg, gpgme_strerror (err), _("Signing"), MB_ERR);
229              gpgme_release (ctx);                  gpgme_release (ctx);
230              EndDialog (dlg, TRUE);                  return FALSE;
231              return TRUE;              }
232                            else                    
233          case IDCANCEL:                  show_msg( dlg, 1500, _("GnuPG Status: Finished") );
234              EndDialog (dlg, FALSE);              gpgme_release (ctx);
235              return FALSE;              EndDialog (dlg, TRUE);
236          }              return TRUE;
237          break;              
238      }          case IDCANCEL:
239                    EndDialog (dlg, FALSE);
240      return FALSE;              return FALSE;
241  }          }
242            break;
243        }
244        
245        return FALSE;
246    }
247    

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26