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

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26