1 |
/* wptClipSignDlg.cpp - WinPT clipboard sign dialog |
/* wptClipSignDlg.cpp - WinPT clipboard sign dialog |
2 |
* Copyright (C) 2000, 2001, 2002, 2003 Timo Schulz |
* Copyright (C) 2000, 2001, 2002, 2003, 2005 Timo Schulz |
3 |
* |
* Copyright (C) 2005 g10 Code GmbH |
4 |
* This file is part of WinPT. |
* |
5 |
* |
* This file is part of WinPT. |
6 |
* WinPT is free software; you can redistribute it and/or modify |
* |
7 |
* it under the terms of the GNU General Public License as published by |
* WinPT is free software; you can redistribute it and/or modify |
8 |
* the Free Software Foundation; either version 2 of the License, or |
* it under the terms of the GNU General Public License as published by |
9 |
* (at your option) any later version. |
* the Free Software Foundation; either version 2 of the License, or |
10 |
* |
* (at your option) any later version. |
11 |
* WinPT is distributed in the hope that it will be useful, |
* |
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
* WinPT is distributed in the hope that it will be useful, |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
* GNU General Public License for more details. |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
* |
* GNU General Public License for more details. |
16 |
* You should have received a copy of the GNU General Public License |
* |
17 |
* along with WinPT; if not, write to the Free Software Foundation, |
* You should have received a copy of the GNU General Public License |
18 |
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
* along with WinPT; if not, write to the Free Software Foundation, |
19 |
*/ |
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
20 |
|
*/ |
21 |
#include <windows.h> |
|
22 |
#include <commctrl.h> |
#ifdef HAVE_CONFIG_H |
23 |
|
#include <config.h> |
24 |
#include "../resource.h" |
#endif |
25 |
#include "wptTypes.h" |
|
26 |
#include "wptAgent.h" |
#include <windows.h> |
27 |
#include "wptNLS.h" |
#include <commctrl.h> |
28 |
#include "wptGPG.h" |
|
29 |
#include "wptCommonCtl.h" |
#include "resource.h" |
30 |
#include "wptRegistry.h" |
#include "wptTypes.h" |
31 |
#include "wptKeylist.h" |
#include "wptAgent.h" |
32 |
#include "wptErrors.h" |
#include "wptNLS.h" |
33 |
#include "wptW32API.h" |
#include "wptCrypto.h" |
34 |
#include "wptVersion.h" |
#include "wptGPG.h" |
35 |
#include "wptContext.h" /* for passphrase_s */ |
#include "wptCommonCtl.h" |
36 |
#include "wptDlgs.h" |
#include "wptRegistry.h" |
37 |
|
#include "wptKeylist.h" |
38 |
|
#include "wptErrors.h" |
39 |
/* This function is used when only one secret key is available. |
#include "wptW32API.h" |
40 |
* it doesn't make sense to offer a dialog for this case. |
#include "wptVersion.h" |
41 |
*/ |
#include "wptContext.h" /* for passphrase_s */ |
42 |
void |
#include "wptDlgs.h" |
43 |
one_key_proc (HWND dlg) |
|
44 |
{ |
|
45 |
char * signer; |
/* Sign the clipboard contents with the key @keyid and wrap |
46 |
gpgme_ctx_t ctx; |
text lines to @wraplen (0 disable line wrapping). |
47 |
gpgme_error_t err; |
Return value: 0 on success. */ |
48 |
passphrase_cb_s pwd; |
gpgme_error_t |
49 |
int rc = 0, n = reg_prefs.word_wrap; |
gpg_clip_sign (gpgme_ctx_t ctx, const char *keyid, int wraplen) |
50 |
|
{ |
51 |
signer = get_gnupg_default_key (); |
gpgme_error_t err; |
52 |
if( !signer ) |
gpgme_data_t plain = NULL; |
53 |
{ |
gpgme_data_t sig = NULL; |
54 |
msg_box( dlg, _("Could not get default key."), _("Signing"), MB_ERR ); |
gpgme_key_t key = NULL; |
55 |
return; |
|
56 |
} |
if (!keyid) |
57 |
|
return gpg_error (GPG_ERR_INV_ARG); |
58 |
err = gpgme_new( &ctx ); |
|
59 |
if( err ) |
gpgme_set_armor (ctx, 1); |
60 |
BUG( dlg ); |
|
61 |
gpgme_enable_logging( ctx ); |
err = gpg_data_new_from_clipboard (&plain, wraplen); |
62 |
set_gpg_passphrase_cb( ctx, &pwd, GPG_CMD_SIGN, dlg, _("Signing") ); |
if (err) |
63 |
err = gpgme_op_clip_sign( ctx, GPGME_SIG_MODE_CLEAR, signer, n ); |
return err; |
64 |
memset( pwd.pwd, 0, sizeof pwd.pwd ); |
|
65 |
if( err == GPGME_Bad_Passphrase ) |
get_pubkey (keyid, &key); |
66 |
agent_del_cache( pwd.keyid ); |
if (key) |
67 |
|
err = gpgme_signers_add (ctx, key); |
68 |
if( err ) |
else { |
69 |
gpgme_show_error( dlg, err, ctx, _("Signing"), MB_ERR ); |
err = gpg_error (GPG_ERR_NO_PUBKEY); |
70 |
else |
goto leave; |
71 |
show_msg( dlg, 1500, _("GnuPG Status: Finished") ); |
} |
72 |
gpgme_release( ctx ); |
err = gpgme_data_new (&sig); |
73 |
free_if_alloc( signer ); |
if (err) |
74 |
} /* one_key_proc */ |
goto leave; |
75 |
|
err = gpgme_op_sign (ctx, plain, sig, GPGME_SIG_MODE_CLEAR); |
76 |
|
if (err) |
77 |
BOOL CALLBACK |
goto leave; |
78 |
clip_sign_dlg_proc( HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam ) |
|
79 |
{ |
gpg_data_release_and_set_clipboard (sig, 1); |
80 |
static listview_ctrl_t lv = NULL; |
sig = NULL; |
81 |
gpgme_keycache_t kc, sec_kc; |
|
82 |
gpgme_ctx_t ctx; |
leave: |
83 |
gpgme_error_t err; |
if (plain) |
84 |
passphrase_cb_s pwd; |
gpgme_data_release (plain); |
85 |
int lv_idx = 0; |
if (sig) |
86 |
int rc = 0, no_signer = 0; |
gpgme_data_release (sig); |
87 |
char * signer = NULL; |
return err; |
88 |
|
} |
89 |
switch( msg ) { |
|
90 |
case WM_INITDIALOG: |
|
91 |
#ifndef LANG_DE |
/* This function is used when only one secret key is available. |
92 |
SetWindowText( dlg, _("Signing") ); |
it doesn't make sense to offer a dialog for this case. */ |
93 |
#endif |
void |
94 |
kc = keycache_get_ctx( KEYCACHE_PUB ); |
one_key_proc (HWND dlg) |
95 |
if( !kc ) |
{ |
96 |
BUG( NULL ); |
char *signer; |
97 |
sec_kc = keycache_get_ctx( KEYCACHE_PRV ); |
gpgme_ctx_t ctx; |
98 |
if( !sec_kc ) |
gpgme_error_t err; |
99 |
BUG( dlg ); |
passphrase_cb_s pwd; |
100 |
if( gpgme_keycache_count( sec_kc ) == 1 ) { |
int n = reg_prefs.word_wrap; |
101 |
one_key_proc( dlg ); |
|
102 |
EndDialog( dlg, TRUE ); |
signer = get_gnupg_default_key (); |
103 |
return FALSE; |
if (!signer) { |
104 |
} |
msg_box (dlg, _("Could not get default key."), _("Signing"), MB_ERR); |
105 |
lv = keylist_load( GetDlgItem( dlg, IDC_SIGN_KEYLIST ), kc, sec_kc, |
return; |
106 |
KEYLIST_SIGN, GPGME_ATTR_USERID ); |
} |
107 |
center_window( dlg ); |
|
108 |
SetForegroundWindow( dlg ); |
err = gpgme_new (&ctx); |
109 |
set_active_window( dlg ); |
if (err) |
110 |
return FALSE; |
BUG (dlg); |
111 |
|
|
112 |
case WM_DESTROY: |
set_gpg_passphrase_cb (&pwd, ctx, GPG_CMD_SIGN, dlg, _("Signing")); |
113 |
reset_active_window(); |
err = gpg_clip_sign (ctx, signer, n); |
114 |
if( lv ) { |
if (pwd.cancel) |
115 |
keylist_delete( lv ); |
goto leave; |
116 |
lv = NULL; |
|
117 |
} |
if (gpgme_err_code (err) == GPG_ERR_BAD_PASSPHRASE) |
118 |
memset( pwd.pwd, 0, sizeof pwd.pwd ); |
agent_del_cache (pwd.keyid); |
119 |
return FALSE; |
if (err) |
120 |
|
msg_box (dlg, gpgme_strerror (err), _("Signing"), MB_ERR); |
121 |
case WM_NOTIFY: |
else |
122 |
NMHDR * notify; |
show_msg (dlg, 1500, _("GnuPG Status: Finished")); |
123 |
notify = (NMHDR *)lparam; |
leave: |
124 |
if( notify && notify->code == NM_DBLCLK |
gpgme_release (ctx); |
125 |
&& notify->idFrom == IDC_SIGN_KEYLIST ) |
free_if_alloc (signer); |
126 |
PostMessage( dlg, WM_COMMAND, MAKEWPARAM(IDOK, 0), NULL ); |
release_gpg_passphrase_cb (&pwd); |
127 |
return TRUE; |
} |
128 |
|
|
129 |
case WM_SYSCOMMAND: |
|
130 |
if( LOWORD (wparam) == SC_CLOSE ) |
/* Count only useable secret keys. |
131 |
EndDialog(dlg, TRUE); |
Ignore expired, revoked and disabled keys. |
132 |
return FALSE; |
Return value: amount of keys. */ |
133 |
|
static DWORD |
134 |
case WM_COMMAND: |
count_useable_seckeys (gpg_keycache_t kc) |
135 |
switch ( LOWORD( wparam ) ) |
{ |
136 |
{ |
struct keycache_s *c; |
137 |
case IDOK: |
DWORD n=0; |
138 |
signer = get_gnupg_default_key (); |
|
139 |
if( !signer ) |
for (c = kc->item; c; c=c->next) { |
140 |
{ |
if (c->pubpart && key_is_useable (c->pubpart->key)) |
141 |
msg_box( dlg, _("Could not get default key."), _("Signing"), MB_ERR ); |
n++; |
142 |
return FALSE; |
} |
143 |
} |
return n; |
144 |
if( listview_count_items( lv, 0 ) == 1 ) |
} |
145 |
{ |
|
146 |
listview_get_item_text( lv, 0, 1, signer, sizeof signer-1 ); |
|
147 |
no_signer = 0; |
/* Return -1 if no key is selected, the index otherwise. */ |
148 |
} |
static int |
149 |
else if( (lv_idx = listview_get_curr_pos( lv )) == -1 ) |
get_selected_key (listview_ctrl_t lv) |
150 |
{ |
{ |
151 |
rc = log_box( _("Signing"), MB_YESNO, |
int i; |
152 |
_("No key was chosen.\nUse the GPG default key '%s'?"), |
for (i=0; i < listview_count_items (lv, 0); i++) { |
153 |
signer ); |
if (listview_get_item_state (lv, i)) |
154 |
if( rc == IDNO ) |
return i; |
155 |
{ |
} |
156 |
free_if_alloc (signer); |
return -1; |
157 |
return FALSE; |
} |
158 |
} |
|
159 |
no_signer = 1; |
|
160 |
} |
/* Dialog box procedure for clipboard signing. */ |
161 |
if (!no_signer) |
BOOL CALLBACK |
162 |
{ |
clip_sign_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
163 |
free_if_alloc (signer); |
{ |
164 |
signer = new char[32+1]; |
static listview_ctrl_t lv = NULL; |
165 |
if (!signer) |
gpg_keycache_t kc, sec_kc; |
166 |
BUG (NULL); |
gpgme_ctx_t ctx; |
167 |
listview_get_item_text (lv, lv_idx, 1, signer, 32); |
gpgme_error_t err; |
168 |
} |
passphrase_cb_s pwd; |
169 |
err = gpgme_new (&ctx); |
int lv_idx = 0; |
170 |
if( err ) |
int rc = 0, no_signer = 0; |
171 |
BUG( dlg ); |
char *signer = NULL; |
172 |
gpgme_enable_logging( ctx ); |
|
173 |
set_gpg_passphrase_cb( ctx, &pwd, GPG_CMD_SIGN, dlg, _("Signing") ); |
switch( msg ) { |
174 |
err = gpgme_op_clip_sign( ctx, GPGME_SIG_MODE_CLEAR, signer, |
case WM_INITDIALOG: |
175 |
reg_prefs.word_wrap ); |
SetWindowText (dlg, _("Signing")); |
176 |
free_if_alloc( signer ); |
|
177 |
memset( pwd.pwd, 0, sizeof pwd.pwd ); |
kc = keycache_get_ctx (KEYCACHE_PUB); |
178 |
if( pwd.cancel && err == GPGME_Bad_Passphrase ) { /* The user hit the cancel button */ |
if (!kc) |
179 |
gpgme_release( ctx ); |
BUG( NULL ); |
180 |
EndDialog( dlg, TRUE ); |
sec_kc = keycache_get_ctx (KEYCACHE_PRV); |
181 |
return TRUE; |
if (!sec_kc) |
182 |
} |
BUG (dlg); |
183 |
if( err ) { |
if (count_useable_seckeys (sec_kc) == 1) { |
184 |
gpgme_show_error( dlg, err, ctx, _("Signing"), MB_ERR ); |
one_key_proc (dlg); |
185 |
gpgme_release( ctx ); |
EndDialog (dlg, TRUE); |
186 |
return FALSE; |
return FALSE; |
187 |
} |
} |
188 |
else |
lv = keylist_load (GetDlgItem (dlg, IDC_SIGN_KEYLIST), kc, sec_kc, |
189 |
show_msg( dlg, 1500, _("GnuPG Status: Finished") ); |
KEYLIST_SIGN, KEY_SORT_USERID); |
190 |
gpgme_release( ctx ); |
center_window (dlg, NULL); |
191 |
EndDialog( dlg, TRUE ); |
SetForegroundWindow (dlg); |
192 |
return TRUE; |
return FALSE; |
193 |
|
|
194 |
case IDCANCEL: |
case WM_DESTROY: |
195 |
EndDialog( dlg, FALSE ); |
if (lv) { |
196 |
return FALSE; |
keylist_delete (lv); |
197 |
} |
lv = NULL; |
198 |
break; |
} |
199 |
} |
return FALSE; |
200 |
|
|
201 |
return FALSE; |
case WM_NOTIFY: |
202 |
} /* clip_sign_dlg_proc */ |
NMHDR * notify; |
203 |
|
notify = (NMHDR *)lparam; |
204 |
|
if( notify && notify->code == NM_DBLCLK |
205 |
|
&& notify->idFrom == IDC_SIGN_KEYLIST ) |
206 |
|
PostMessage (dlg, WM_COMMAND, MAKEWPARAM(IDOK, 0), 0); |
207 |
|
return TRUE; |
208 |
|
|
209 |
|
case WM_SYSCOMMAND: |
210 |
|
if( LOWORD (wparam) == SC_CLOSE ) |
211 |
|
EndDialog(dlg, TRUE); |
212 |
|
return FALSE; |
213 |
|
|
214 |
|
case WM_COMMAND: |
215 |
|
switch (LOWORD (wparam)) { |
216 |
|
case IDOK: |
217 |
|
signer = get_gnupg_default_key (); |
218 |
|
if (!signer) { |
219 |
|
msg_box( dlg, _("Could not get default key."), _("Signing"), MB_ERR ); |
220 |
|
return FALSE; |
221 |
|
} |
222 |
|
if (listview_count_items (lv, 0) == 1) { |
223 |
|
listview_get_item_text (lv, 0, 1, signer, sizeof signer-1); |
224 |
|
no_signer = 0; |
225 |
|
} |
226 |
|
else if ((lv_idx = get_selected_key (lv)) == -1) { |
227 |
|
rc = log_box (_("Signing"), MB_YESNO, |
228 |
|
_("No key was chosen.\nUse the GPG default key '%s'?"), |
229 |
|
signer); |
230 |
|
if (rc == IDNO) { |
231 |
|
free_if_alloc (signer); |
232 |
|
return FALSE; |
233 |
|
} |
234 |
|
no_signer = 1; |
235 |
|
} |
236 |
|
if (!no_signer) { |
237 |
|
free_if_alloc (signer); |
238 |
|
signer = new char[32+1]; |
239 |
|
if (!signer) |
240 |
|
BUG (NULL); |
241 |
|
listview_get_item_text (lv, lv_idx, 1, signer, 32); |
242 |
|
} |
243 |
|
err = gpgme_new (&ctx); |
244 |
|
if (err) |
245 |
|
BUG (dlg); |
246 |
|
set_gpg_passphrase_cb (&pwd, ctx, GPG_CMD_SIGN, dlg, _("Signing")); |
247 |
|
err = gpg_clip_sign (ctx, signer, reg_prefs.word_wrap); |
248 |
|
free_if_alloc (signer); |
249 |
|
release_gpg_passphrase_cb (&pwd); |
250 |
|
|
251 |
|
if (pwd.cancel && gpgme_err_code(err) == GPG_ERR_BAD_PASSPHRASE) { |
252 |
|
/* The user hit the cancel button or bad passphrase */ |
253 |
|
gpgme_release (ctx); |
254 |
|
return TRUE; |
255 |
|
} |
256 |
|
if (err) { |
257 |
|
msg_box (dlg, gpgme_strerror (err), _("Signing"), MB_ERR); |
258 |
|
gpgme_release (ctx); |
259 |
|
return FALSE; |
260 |
|
} |
261 |
|
else |
262 |
|
show_msg( dlg, 1500, _("GnuPG Status: Finished") ); |
263 |
|
gpgme_release (ctx); |
264 |
|
EndDialog (dlg, TRUE); |
265 |
|
return TRUE; |
266 |
|
|
267 |
|
case IDCANCEL: |
268 |
|
EndDialog (dlg, FALSE); |
269 |
|
return FALSE; |
270 |
|
} |
271 |
|
break; |
272 |
|
} |
273 |
|
|
274 |
|
return FALSE; |
275 |
|
} |