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

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26