/[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 32 by twoaday, Mon Oct 24 08:03:48 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 "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 n = reg_prefs.word_wrap;
102          msg_box (dlg, _("Could not get default key."), _("Signing"), MB_ERR);      
103          return;      signer = get_gnupg_default_key ();
104      }      if (!signer) {
105                msg_box (dlg, _("Could not get default key."), _("Signing"), MB_ERR);
106      err = gpgme_new (&ctx);          return;
107      if (err)      }
108          BUG (dlg);      
109        err = gpgme_new (&ctx);
110      set_gpg_passphrase_cb (&pwd, ctx, GPG_CMD_SIGN, dlg, _("Signing"));      if (err)
111      err = gpg_clip_sign (ctx, signer, n );          BUG (dlg);
112      wipememory (pwd.pwd, sizeof (pwd.pwd));  
113      if (gpgme_err_code (err) == GPG_ERR_BAD_PASSPHRASE)      set_gpg_passphrase_cb (&pwd, ctx, GPG_CMD_SIGN, dlg, _("Signing"));
114          agent_del_cache (pwd.keyid);      err = gpg_clip_sign (ctx, signer, n );
115      if (err)      wipememory (pwd.pwd, sizeof (pwd.pwd));
116          msg_box (dlg, gpgme_strerror (err), _("Signing"), MB_ERR);      if (gpgme_err_code (err) == GPG_ERR_BAD_PASSPHRASE)
117      else          agent_del_cache (pwd.keyid);
118          show_msg (dlg, 1500, _("GnuPG Status: Finished"));      if (err)
119      gpgme_release (ctx);          msg_box (dlg, gpgme_strerror (err), _("Signing"), MB_ERR);
120      free_if_alloc (signer);      else
121  }          show_msg (dlg, 1500, _("GnuPG Status: Finished"));
122        gpgme_release (ctx);
123        free_if_alloc (signer);
124  /* Dialog box procedure for clipboard signing. */  }
125  BOOL CALLBACK  
126  clip_sign_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)  
127  {        /* Dialog box procedure for clipboard signing. */
128      static listview_ctrl_t lv = NULL;  BOOL CALLBACK
129      gpg_keycache_t kc, sec_kc;  clip_sign_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
130      gpgme_ctx_t ctx;  {      
131      gpgme_error_t err;      static listview_ctrl_t lv = NULL;
132      passphrase_cb_s pwd;      gpg_keycache_t kc, sec_kc;
133      int lv_idx = 0;      gpgme_ctx_t ctx;
134      int rc = 0, no_signer = 0;      gpgme_error_t err;
135      char * signer = NULL;      passphrase_cb_s pwd;
136            int lv_idx = 0;
137      switch( msg ) {      int rc = 0, no_signer = 0;
138      case WM_INITDIALOG:      char * signer = NULL;
139      #ifndef LANG_DE      
140          SetWindowText( dlg, _("Signing") );      switch( msg ) {
141      #endif      case WM_INITDIALOG:
142          kc = keycache_get_ctx (KEYCACHE_PUB);          SetWindowText (dlg, _("Signing"));
143          if (!kc)  
144              BUG( NULL );          kc = keycache_get_ctx (KEYCACHE_PUB);
145          sec_kc = keycache_get_ctx (KEYCACHE_PRV);          if (!kc)
146          if (!sec_kc)              BUG( NULL );
147              BUG (dlg);          sec_kc = keycache_get_ctx (KEYCACHE_PRV);
148          if (gpg_keycache_get_size (sec_kc) == 1) {          if (!sec_kc)
149              one_key_proc (dlg);              BUG (dlg);
150              EndDialog (dlg, TRUE);          if (gpg_keycache_get_size (sec_kc) == 1) {
151              return FALSE;              one_key_proc (dlg);
152          }              EndDialog (dlg, TRUE);
153          lv = keylist_load (GetDlgItem (dlg, IDC_SIGN_KEYLIST), kc, sec_kc,              return FALSE;
154                             KEYLIST_SIGN, KEY_SORT_USERID);          }
155          center_window (dlg, NULL);          lv = keylist_load (GetDlgItem (dlg, IDC_SIGN_KEYLIST), kc, sec_kc,
156          SetForegroundWindow (dlg);                             KEYLIST_SIGN, KEY_SORT_USERID);
157          set_active_window (dlg);          center_window (dlg, NULL);
158          return FALSE;            SetForegroundWindow (dlg);
159                    set_active_window (dlg);
160      case WM_DESTROY:          return FALSE;  
161          reset_active_window ();          
162          if (lv) {      case WM_DESTROY:
163              keylist_delete (lv);          reset_active_window ();
164              lv = NULL;          if (lv) {
165          }              keylist_delete (lv);
166          return FALSE;              lv = NULL;
167                    }
168      case WM_NOTIFY:          return FALSE;
169          NMHDR * notify;          
170          notify = (NMHDR *)lparam;      case WM_NOTIFY:
171          if( notify && notify->code == NM_DBLCLK          NMHDR * notify;
172              && notify->idFrom == IDC_SIGN_KEYLIST )          notify = (NMHDR *)lparam;
173              PostMessage( dlg, WM_COMMAND, MAKEWPARAM(IDOK, 0), NULL );          if( notify && notify->code == NM_DBLCLK
174          return TRUE;              && notify->idFrom == IDC_SIGN_KEYLIST )
175                        PostMessage (dlg, WM_COMMAND, MAKEWPARAM(IDOK, 0), 0);
176      case WM_SYSCOMMAND:          return TRUE;
177          if( LOWORD (wparam) == SC_CLOSE )          
178              EndDialog(dlg, TRUE);      case WM_SYSCOMMAND:
179          return FALSE;          if( LOWORD (wparam) == SC_CLOSE )
180                        EndDialog(dlg, TRUE);
181      case WM_COMMAND:          return FALSE;
182          switch (LOWORD (wparam)) {          
183          case IDOK:      case WM_COMMAND:
184              signer = get_gnupg_default_key ();          switch (LOWORD (wparam)) {
185              if (!signer) {          case IDOK:
186                  msg_box( dlg, _("Could not get default key."), _("Signing"), MB_ERR );              signer = get_gnupg_default_key ();
187                  return FALSE;              if (!signer) {
188              }                  msg_box( dlg, _("Could not get default key."), _("Signing"), MB_ERR );
189              if (listview_count_items (lv, 0) == 1) {                  return FALSE;
190                  listview_get_item_text (lv, 0, 1, signer, sizeof signer-1);              }
191                  no_signer = 0;              if (listview_count_items (lv, 0) == 1) {
192              }                  listview_get_item_text (lv, 0, 1, signer, sizeof signer-1);
193              else if ((lv_idx = listview_get_curr_pos (lv)) == -1) {                  no_signer = 0;
194                  rc = log_box (_("Signing"), MB_YESNO,              }
195                                _("No key was chosen.\nUse the GPG default key '%s'?"),              else if ((lv_idx = listview_get_curr_pos (lv)) == -1) {
196                                signer);                  rc = log_box (_("Signing"), MB_YESNO,
197                  if (rc == IDNO) {                                _("No key was chosen.\nUse the GPG default key '%s'?"),
198                      free_if_alloc (signer);                                signer);
199                      return FALSE;                  if (rc == IDNO) {
200                  }                      free_if_alloc (signer);
201                  no_signer = 1;                      return FALSE;
202              }                  }
203              if (!no_signer) {                  no_signer = 1;
204                  free_if_alloc (signer);              }
205                  signer = new char[32+1];              if (!no_signer) {
206                  if (!signer)                  free_if_alloc (signer);
207                      BUG (NULL);                  signer = new char[32+1];
208                  listview_get_item_text (lv, lv_idx, 1, signer, 32);                  if (!signer)
209              }                      BUG (NULL);
210              err = gpgme_new (&ctx);                  listview_get_item_text (lv, lv_idx, 1, signer, 32);
211              if (err)              }
212                  BUG (dlg);              err = gpgme_new (&ctx);
213              set_gpg_passphrase_cb (&pwd, ctx, GPG_CMD_SIGN, dlg, _("Signing"));              if (err)
214              err = gpg_clip_sign (ctx, signer, reg_prefs.word_wrap);                  BUG (dlg);
215              free_if_alloc (signer);              set_gpg_passphrase_cb (&pwd, ctx, GPG_CMD_SIGN, dlg, _("Signing"));
216              release_gpg_passphrase_cb (&pwd);              err = gpg_clip_sign (ctx, signer, reg_prefs.word_wrap);
217                free_if_alloc (signer);
218              if (pwd.cancel && gpgme_err_code(err) == GPG_ERR_BAD_PASSPHRASE) {              release_gpg_passphrase_cb (&pwd);
219                  /* The user hit the cancel button or bad passphrase */  
220                  gpgme_release (ctx);              if (pwd.cancel && gpgme_err_code(err) == GPG_ERR_BAD_PASSPHRASE) {
221                  EndDialog (dlg, TRUE);                  /* The user hit the cancel button or bad passphrase */
222                  return TRUE;                  gpgme_release (ctx);
223              }                  EndDialog (dlg, TRUE);
224              if (err) {                  return TRUE;
225                  msg_box (dlg, gpgme_strerror (err), _("Signing"), MB_ERR);              }
226                  gpgme_release (ctx);              if (err) {
227                  return FALSE;                  msg_box (dlg, gpgme_strerror (err), _("Signing"), MB_ERR);
228              }                  gpgme_release (ctx);
229              else                                      return FALSE;
230                  show_msg( dlg, 1500, _("GnuPG Status: Finished") );              }
231              gpgme_release (ctx);              else                    
232              EndDialog (dlg, TRUE);                  show_msg( dlg, 1500, _("GnuPG Status: Finished") );
233              return TRUE;              gpgme_release (ctx);
234                            EndDialog (dlg, TRUE);
235          case IDCANCEL:              return TRUE;
236              EndDialog (dlg, FALSE);              
237              return FALSE;          case IDCANCEL:
238          }              EndDialog (dlg, FALSE);
239          break;              return FALSE;
240      }          }
241                break;
242      return FALSE;      }
243  }      
244        return FALSE;
245    }
246    

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26