1 |
/* wptClipSignDlg.cpp - WinPT clipboard sign dialog |
2 |
* Copyright (C) 2000-2006 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 |
|
43 |
|
44 |
/* Sign the clipboard contents with the key @keyid and wrap |
45 |
text lines to @wraplen (0 disable line wrapping). |
46 |
Return value: 0 on success. */ |
47 |
gpgme_error_t |
48 |
gpg_clip_sign (gpgme_ctx_t ctx, const char *keyid, int wraplen) |
49 |
{ |
50 |
gpgme_error_t err; |
51 |
gpgme_data_t plain = NULL; |
52 |
gpgme_data_t sig = NULL; |
53 |
gpgme_key_t key = NULL; |
54 |
|
55 |
if (!keyid) |
56 |
return gpg_error (GPG_ERR_INV_ARG); |
57 |
|
58 |
gpgme_set_armor (ctx, 1); |
59 |
err = gpg_data_new_from_clipboard (&plain, wraplen); |
60 |
if (err) |
61 |
return err; |
62 |
err = get_pubkey (keyid, &key); |
63 |
if (err) |
64 |
goto leave; |
65 |
err = gpgme_signers_add (ctx, key); |
66 |
if (err) |
67 |
goto leave; |
68 |
err = gpgme_data_new (&sig); |
69 |
if (err) |
70 |
goto leave; |
71 |
err = gpgme_op_sign (ctx, plain, sig, GPGME_SIG_MODE_CLEAR); |
72 |
if (err) |
73 |
goto leave; |
74 |
|
75 |
gpg_data_release_and_set_clipboard (sig, 1); |
76 |
sig = NULL; |
77 |
|
78 |
leave: |
79 |
if (plain) |
80 |
gpgme_data_release (plain); |
81 |
if (sig) |
82 |
gpgme_data_release (sig); |
83 |
return err; |
84 |
} |
85 |
|
86 |
|
87 |
/* This function is used when only one secret key is available. |
88 |
it doesn't make sense to offer a dialog for this case. */ |
89 |
void |
90 |
one_key_proc (HWND dlg) |
91 |
{ |
92 |
gpgme_ctx_t ctx; |
93 |
gpgme_error_t err; |
94 |
passphrase_cb_s pwd; |
95 |
char *signer; |
96 |
int n = reg_prefs.word_wrap; |
97 |
|
98 |
signer = get_gnupg_default_key (); |
99 |
if (!signer) { |
100 |
msg_box (dlg, _("Could not get default key."), _("Signing"), MB_ERR); |
101 |
return; |
102 |
} |
103 |
|
104 |
err = gpgme_new (&ctx); |
105 |
if (err) |
106 |
BUG (dlg); |
107 |
|
108 |
set_gpg_passphrase_cb (&pwd, ctx, GPG_CMD_SIGN, dlg, _("Signing")); |
109 |
err = gpg_clip_sign (ctx, signer, n); |
110 |
if (pwd.cancel) |
111 |
goto leave; |
112 |
|
113 |
if (gpgme_err_code (err) == GPG_ERR_BAD_PASSPHRASE) |
114 |
agent_del_cache (pwd.keyid); |
115 |
if (err) |
116 |
msg_box (dlg, gpgme_strerror (err), _("Signing"), MB_ERR); |
117 |
else |
118 |
show_msg (dlg, 1500, _("GnuPG Status: Finished")); |
119 |
leave: |
120 |
gpgme_release (ctx); |
121 |
free_if_alloc (signer); |
122 |
release_gpg_passphrase_cb (&pwd); |
123 |
} |
124 |
|
125 |
|
126 |
/* Count only useable secret keys. |
127 |
Ignore expired, revoked and disabled keys. |
128 |
Return value: amount of keys. */ |
129 |
static DWORD |
130 |
count_useable_seckeys (gpg_keycache_t kc) |
131 |
{ |
132 |
struct keycache_s *c; |
133 |
DWORD n=0; |
134 |
|
135 |
for (c = kc->item; c; c=c->next) { |
136 |
if (c->pubpart && key_is_useable (c->pubpart->key)) |
137 |
n++; |
138 |
} |
139 |
return n; |
140 |
} |
141 |
|
142 |
|
143 |
static listview_ctrl_t |
144 |
on_init_dialog (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
145 |
{ |
146 |
listview_ctrl_t lv; |
147 |
gpg_keycache_t kc, sec_kc; |
148 |
int nkeys; |
149 |
|
150 |
SetWindowText (dlg, _("Signing")); |
151 |
kc = keycache_get_ctx (KEYCACHE_PUB); |
152 |
sec_kc = keycache_get_ctx (KEYCACHE_PRV); |
153 |
|
154 |
nkeys = count_useable_seckeys (sec_kc); |
155 |
if (nkeys < 1) { |
156 |
msg_box (dlg, _("No useable signing key found"), _("Signing"), MB_ERR); |
157 |
return NULL; |
158 |
} |
159 |
else if (nkeys == 1) { |
160 |
one_key_proc (dlg); |
161 |
return NULL; |
162 |
} |
163 |
lv = keylist_load (GetDlgItem (dlg, IDC_SIGN_KEYLIST), kc, sec_kc, |
164 |
KEYLIST_SIGN, KEY_SORT_USERID); |
165 |
center_window (dlg, NULL); |
166 |
SetForegroundWindow (dlg); |
167 |
return lv; |
168 |
} |
169 |
|
170 |
|
171 |
static const char* |
172 |
lookup_key_userid (const char *patt) |
173 |
{ |
174 |
winpt_key_s key; |
175 |
|
176 |
memset (&key, 0, sizeof (key)); |
177 |
if (winpt_get_pubkey (patt, &key)) |
178 |
return patt; |
179 |
return key.ext->uids->uid; |
180 |
} |
181 |
|
182 |
|
183 |
/* Dialog box procedure for clipboard signing. */ |
184 |
BOOL CALLBACK |
185 |
clip_sign_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
186 |
{ |
187 |
static listview_ctrl_t lv = NULL; |
188 |
gpgme_ctx_t ctx; |
189 |
gpgme_error_t err; |
190 |
passphrase_cb_s pwd; |
191 |
int lv_idx = 0; |
192 |
int rc = 0, sel_signer = 0; |
193 |
char *signer = NULL; |
194 |
|
195 |
switch (msg) { |
196 |
case WM_INITDIALOG: |
197 |
lv = on_init_dialog (dlg, msg, wparam, lparam); |
198 |
if (!lv) |
199 |
EndDialog (dlg, TRUE); |
200 |
return FALSE; |
201 |
|
202 |
case WM_DESTROY: |
203 |
if (lv) { |
204 |
keylist_delete (lv); |
205 |
lv = NULL; |
206 |
} |
207 |
return FALSE; |
208 |
|
209 |
case WM_NOTIFY: |
210 |
NMHDR *notify; |
211 |
notify = (NMHDR *)lparam; |
212 |
if (notify && notify->code == NM_DBLCLK |
213 |
&& notify->idFrom == IDC_SIGN_KEYLIST) |
214 |
PostMessage (dlg, WM_COMMAND, MAKEWPARAM (IDOK, 0), 0); |
215 |
return TRUE; |
216 |
|
217 |
case WM_COMMAND: |
218 |
switch (LOWORD (wparam)) { |
219 |
case IDOK: |
220 |
sel_signer = 1; |
221 |
signer = get_gnupg_default_key (); |
222 |
if (!signer) { |
223 |
msg_box (dlg, _("Could not get default key."), |
224 |
_("Signing"), MB_ERR); |
225 |
return FALSE; |
226 |
} |
227 |
else if ((lv_idx = listview_get_selected_item (lv)) == -1) { |
228 |
rc = log_box (_("Signing"), MB_YESNO, |
229 |
_("No key was chosen.\n" |
230 |
"Use the GPG default key '%s'?"), |
231 |
lookup_key_userid (signer)); |
232 |
if (rc == IDNO) { |
233 |
free_if_alloc (signer); |
234 |
return FALSE; |
235 |
} |
236 |
sel_signer = 0; |
237 |
} |
238 |
if (sel_signer) { |
239 |
free_if_alloc (signer); |
240 |
signer = new char[32+1]; |
241 |
if (!signer) |
242 |
BUG (NULL); |
243 |
listview_get_item_text (lv, lv_idx, KM_COL_KEYID, signer, 32); |
244 |
} |
245 |
err = gpgme_new (&ctx); |
246 |
if (err) |
247 |
BUG (NULL); |
248 |
set_gpg_passphrase_cb (&pwd, ctx, GPG_CMD_SIGN, dlg, _("Signing")); |
249 |
err = gpg_clip_sign (ctx, signer, reg_prefs.word_wrap); |
250 |
free_if_alloc (signer); |
251 |
release_gpg_passphrase_cb (&pwd); |
252 |
|
253 |
if (pwd.cancel && gpgme_err_code (err) == GPG_ERR_BAD_PASSPHRASE) { |
254 |
/* The user hit the cancel button or bad passphrase */ |
255 |
gpgme_release (ctx); |
256 |
return TRUE; |
257 |
} |
258 |
if (err) { |
259 |
msg_box (dlg, gpgme_strerror (err), _("Signing"), MB_ERR); |
260 |
gpgme_release (ctx); |
261 |
return TRUE; |
262 |
} |
263 |
else |
264 |
show_msg (dlg, 1500, _("GnuPG Status: Finished")); |
265 |
gpgme_release (ctx); |
266 |
EndDialog (dlg, TRUE); |
267 |
return TRUE; |
268 |
|
269 |
case IDCANCEL: |
270 |
EndDialog (dlg, FALSE); |
271 |
return FALSE; |
272 |
} |
273 |
break; |
274 |
} |
275 |
|
276 |
return FALSE; |
277 |
} |