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