1 |
werner |
36 |
/* wptClipSignDlg.cpp - WinPT clipboard sign dialog |
2 |
twoaday |
328 |
* Copyright (C) 2000-2006, 2009 Timo Schulz |
3 |
werner |
36 |
* 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 |
twoaday |
23 |
|
18 |
werner |
36 |
#ifdef HAVE_CONFIG_H |
19 |
|
|
#include <config.h> |
20 |
|
|
#endif |
21 |
|
|
|
22 |
|
|
#include <windows.h> |
23 |
|
|
#include <commctrl.h> |
24 |
|
|
|
25 |
werner |
47 |
#include "resource.h" |
26 |
werner |
36 |
#include "wptTypes.h" |
27 |
|
|
#include "wptAgent.h" |
28 |
|
|
#include "wptNLS.h" |
29 |
|
|
#include "wptCrypto.h" |
30 |
|
|
#include "wptGPG.h" |
31 |
|
|
#include "wptCommonCtl.h" |
32 |
|
|
#include "wptRegistry.h" |
33 |
|
|
#include "wptKeylist.h" |
34 |
|
|
#include "wptErrors.h" |
35 |
|
|
#include "wptW32API.h" |
36 |
|
|
#include "wptVersion.h" |
37 |
|
|
#include "wptContext.h" /* for passphrase_s */ |
38 |
|
|
|
39 |
|
|
|
40 |
|
|
/* Sign the clipboard contents with the key @keyid and wrap |
41 |
|
|
text lines to @wraplen (0 disable line wrapping). |
42 |
|
|
Return value: 0 on success. */ |
43 |
|
|
gpgme_error_t |
44 |
|
|
gpg_clip_sign (gpgme_ctx_t ctx, const char *keyid, int wraplen) |
45 |
|
|
{ |
46 |
|
|
gpgme_error_t err; |
47 |
|
|
gpgme_data_t plain = NULL; |
48 |
|
|
gpgme_data_t sig = NULL; |
49 |
|
|
gpgme_key_t key = NULL; |
50 |
|
|
|
51 |
|
|
if (!keyid) |
52 |
|
|
return gpg_error (GPG_ERR_INV_ARG); |
53 |
|
|
|
54 |
|
|
gpgme_set_armor (ctx, 1); |
55 |
twoaday |
328 |
err = gpg_data_utf8_new_from_clipboard (&plain, wraplen, NULL); |
56 |
werner |
36 |
if (err) |
57 |
|
|
return err; |
58 |
twoaday |
212 |
err = get_pubkey (keyid, &key); |
59 |
|
|
if (err) |
60 |
werner |
36 |
goto leave; |
61 |
twoaday |
212 |
err = gpgme_signers_add (ctx, key); |
62 |
|
|
if (err) |
63 |
|
|
goto leave; |
64 |
werner |
36 |
err = gpgme_data_new (&sig); |
65 |
|
|
if (err) |
66 |
twoaday |
256 |
goto leave; |
67 |
werner |
36 |
err = gpgme_op_sign (ctx, plain, sig, GPGME_SIG_MODE_CLEAR); |
68 |
|
|
if (err) |
69 |
|
|
goto leave; |
70 |
|
|
|
71 |
twoaday |
462 |
// New code with UTF8 support |
72 |
|
|
gpg_data_release_utf8_to_clipboard(sig); |
73 |
|
|
|
74 |
|
|
//gpg_data_change_version(&sig); |
75 |
|
|
//gpg_data_release_to_clipboard (sig, 1); |
76 |
werner |
36 |
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 |
twoaday |
77 |
it doesn't make sense to offer a dialog for this case. */ |
89 |
werner |
36 |
void |
90 |
|
|
one_key_proc (HWND dlg) |
91 |
twoaday |
256 |
{ |
92 |
werner |
36 |
gpgme_ctx_t ctx; |
93 |
|
|
gpgme_error_t err; |
94 |
|
|
passphrase_cb_s pwd; |
95 |
twoaday |
256 |
char *signer; |
96 |
werner |
36 |
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 |
twoaday |
77 |
err = gpg_clip_sign (ctx, signer, n); |
110 |
twoaday |
175 |
if (pwd.cancel) |
111 |
|
|
goto leave; |
112 |
|
|
|
113 |
werner |
36 |
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 |
twoaday |
175 |
leave: |
120 |
werner |
36 |
gpgme_release (ctx); |
121 |
|
|
free_if_alloc (signer); |
122 |
twoaday |
77 |
release_gpg_passphrase_cb (&pwd); |
123 |
werner |
36 |
} |
124 |
|
|
|
125 |
|
|
|
126 |
twoaday |
175 |
/* 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 |
twoaday |
451 |
DWORD n = 0; |
134 |
twoaday |
175 |
|
135 |
twoaday |
451 |
for (c = kc->item; c; c = c->next) { |
136 |
twoaday |
175 |
if (c->pubpart && key_is_useable (c->pubpart->key)) |
137 |
|
|
n++; |
138 |
|
|
} |
139 |
|
|
return n; |
140 |
|
|
} |
141 |
|
|
|
142 |
|
|
|
143 |
twoaday |
328 |
static keylist_ctrl_t |
144 |
twoaday |
188 |
on_init_dialog (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
145 |
|
|
{ |
146 |
twoaday |
328 |
keylist_ctrl_t kl; |
147 |
twoaday |
188 |
gpg_keycache_t kc, sec_kc; |
148 |
twoaday |
225 |
int nkeys; |
149 |
twoaday |
188 |
|
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 |
twoaday |
328 |
kl = keylist_load (GetDlgItem (dlg, IDC_SIGN_KEYLIST), kc, sec_kc, |
164 |
twoaday |
188 |
KEYLIST_SIGN, KEY_SORT_USERID); |
165 |
|
|
center_window (dlg, NULL); |
166 |
|
|
SetForegroundWindow (dlg); |
167 |
twoaday |
328 |
return kl; |
168 |
twoaday |
188 |
} |
169 |
|
|
|
170 |
|
|
|
171 |
twoaday |
225 |
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 |
werner |
36 |
/* Dialog box procedure for clipboard signing. */ |
184 |
|
|
BOOL CALLBACK |
185 |
|
|
clip_sign_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
186 |
|
|
{ |
187 |
twoaday |
328 |
static keylist_ctrl_t kl = NULL; |
188 |
werner |
36 |
gpgme_ctx_t ctx; |
189 |
|
|
gpgme_error_t err; |
190 |
|
|
passphrase_cb_s pwd; |
191 |
|
|
int lv_idx = 0; |
192 |
twoaday |
451 |
int sel_signer = 0; |
193 |
twoaday |
175 |
char *signer = NULL; |
194 |
werner |
36 |
|
195 |
twoaday |
451 |
/* XXX: there seems to be a memory corruption in the code */ |
196 |
|
|
|
197 |
twoaday |
188 |
switch (msg) { |
198 |
werner |
36 |
case WM_INITDIALOG: |
199 |
twoaday |
328 |
kl = on_init_dialog (dlg, msg, wparam, lparam); |
200 |
|
|
if (!kl) |
201 |
twoaday |
188 |
EndDialog (dlg, TRUE); |
202 |
werner |
36 |
return FALSE; |
203 |
|
|
|
204 |
|
|
case WM_DESTROY: |
205 |
twoaday |
328 |
if (kl) { |
206 |
|
|
keylist_delete (kl); |
207 |
|
|
kl = NULL; |
208 |
werner |
36 |
} |
209 |
|
|
return FALSE; |
210 |
|
|
|
211 |
twoaday |
451 |
case WM_NOTIFY: |
212 |
|
|
/* Wait until the dialog initialization is done */ |
213 |
|
|
if (kl == NULL) |
214 |
|
|
break; |
215 |
|
|
|
216 |
twoaday |
225 |
NMHDR *notify; |
217 |
werner |
36 |
notify = (NMHDR *)lparam; |
218 |
twoaday |
451 |
if (notify && notify->code == NM_DBLCLK && |
219 |
|
|
notify->idFrom == IDC_SIGN_KEYLIST) |
220 |
twoaday |
225 |
PostMessage (dlg, WM_COMMAND, MAKEWPARAM (IDOK, 0), 0); |
221 |
twoaday |
451 |
|
222 |
|
|
return FALSE; |
223 |
werner |
36 |
|
224 |
|
|
case WM_COMMAND: |
225 |
|
|
switch (LOWORD (wparam)) { |
226 |
|
|
case IDOK: |
227 |
twoaday |
225 |
sel_signer = 1; |
228 |
werner |
36 |
signer = get_gnupg_default_key (); |
229 |
|
|
if (!signer) { |
230 |
twoaday |
451 |
msg_box (dlg, _("Could not get default key."), _("Signing"), MB_ERR); |
231 |
werner |
36 |
return FALSE; |
232 |
|
|
} |
233 |
twoaday |
328 |
else if ((lv_idx = listview_get_selected_item (kl->lv)) == -1) { |
234 |
twoaday |
451 |
int rc = log_box (_("Signing"), MB_YESNO, |
235 |
|
|
_("No key was chosen.\n" |
236 |
|
|
"Use the GPG default key '%s'?"), |
237 |
|
|
lookup_key_userid (signer)); |
238 |
werner |
36 |
if (rc == IDNO) { |
239 |
|
|
free_if_alloc (signer); |
240 |
|
|
return FALSE; |
241 |
|
|
} |
242 |
twoaday |
225 |
sel_signer = 0; |
243 |
werner |
36 |
} |
244 |
twoaday |
225 |
if (sel_signer) { |
245 |
werner |
36 |
free_if_alloc (signer); |
246 |
|
|
signer = new char[32+1]; |
247 |
|
|
if (!signer) |
248 |
|
|
BUG (NULL); |
249 |
twoaday |
328 |
listview_get_item_text (kl->lv, lv_idx, KM_COL_KEYID, signer, 32); |
250 |
werner |
36 |
} |
251 |
|
|
err = gpgme_new (&ctx); |
252 |
|
|
if (err) |
253 |
twoaday |
225 |
BUG (NULL); |
254 |
werner |
36 |
set_gpg_passphrase_cb (&pwd, ctx, GPG_CMD_SIGN, dlg, _("Signing")); |
255 |
|
|
err = gpg_clip_sign (ctx, signer, reg_prefs.word_wrap); |
256 |
|
|
free_if_alloc (signer); |
257 |
|
|
release_gpg_passphrase_cb (&pwd); |
258 |
twoaday |
451 |
gpgme_release (ctx); |
259 |
twoaday |
256 |
if (pwd.cancel && gpgme_err_code (err) == GPG_ERR_BAD_PASSPHRASE) { |
260 |
twoaday |
451 |
/* The user hit the cancel button or bad passphrase */ |
261 |
werner |
36 |
return TRUE; |
262 |
|
|
} |
263 |
|
|
if (err) { |
264 |
|
|
msg_box (dlg, gpgme_strerror (err), _("Signing"), MB_ERR); |
265 |
twoaday |
225 |
return TRUE; |
266 |
werner |
36 |
} |
267 |
twoaday |
225 |
else |
268 |
|
|
show_msg (dlg, 1500, _("GnuPG Status: Finished")); |
269 |
werner |
36 |
EndDialog (dlg, TRUE); |
270 |
|
|
return TRUE; |
271 |
|
|
|
272 |
|
|
case IDCANCEL: |
273 |
|
|
EndDialog (dlg, FALSE); |
274 |
|
|
return FALSE; |
275 |
|
|
} |
276 |
|
|
break; |
277 |
|
|
} |
278 |
|
|
|
279 |
|
|
return FALSE; |
280 |
|
|
} |