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

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26