1 |
werner |
36 |
/* wptClipSignDlg.cpp - WinPT clipboard sign dialog |
2 |
twoaday |
256 |
* Copyright (C) 2000-2006 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 |
|
|
* 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 |
twoaday |
23 |
|
22 |
werner |
36 |
#ifdef HAVE_CONFIG_H |
23 |
|
|
#include <config.h> |
24 |
|
|
#endif |
25 |
|
|
|
26 |
|
|
#include <windows.h> |
27 |
|
|
#include <commctrl.h> |
28 |
|
|
|
29 |
werner |
47 |
#include "resource.h" |
30 |
werner |
36 |
#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 |
twoaday |
212 |
err = get_pubkey (keyid, &key); |
63 |
|
|
if (err) |
64 |
werner |
36 |
goto leave; |
65 |
twoaday |
212 |
err = gpgme_signers_add (ctx, key); |
66 |
|
|
if (err) |
67 |
|
|
goto leave; |
68 |
werner |
36 |
err = gpgme_data_new (&sig); |
69 |
|
|
if (err) |
70 |
twoaday |
256 |
goto leave; |
71 |
werner |
36 |
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 |
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 |
|
|
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 |
twoaday |
188 |
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 |
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 |
|
|
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 |
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 |
188 |
static listview_ctrl_t lv = 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 |
225 |
int rc = 0, sel_signer = 0; |
193 |
twoaday |
175 |
char *signer = NULL; |
194 |
werner |
36 |
|
195 |
twoaday |
188 |
switch (msg) { |
196 |
werner |
36 |
case WM_INITDIALOG: |
197 |
twoaday |
188 |
lv = on_init_dialog (dlg, msg, wparam, lparam); |
198 |
|
|
if (!lv) |
199 |
|
|
EndDialog (dlg, TRUE); |
200 |
werner |
36 |
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 |
twoaday |
225 |
NMHDR *notify; |
211 |
werner |
36 |
notify = (NMHDR *)lparam; |
212 |
twoaday |
225 |
if (notify && notify->code == NM_DBLCLK |
213 |
|
|
&& notify->idFrom == IDC_SIGN_KEYLIST) |
214 |
|
|
PostMessage (dlg, WM_COMMAND, MAKEWPARAM (IDOK, 0), 0); |
215 |
werner |
36 |
return TRUE; |
216 |
|
|
|
217 |
|
|
case WM_COMMAND: |
218 |
|
|
switch (LOWORD (wparam)) { |
219 |
|
|
case IDOK: |
220 |
twoaday |
225 |
sel_signer = 1; |
221 |
werner |
36 |
signer = get_gnupg_default_key (); |
222 |
|
|
if (!signer) { |
223 |
twoaday |
225 |
msg_box (dlg, _("Could not get default key."), |
224 |
|
|
_("Signing"), MB_ERR); |
225 |
werner |
36 |
return FALSE; |
226 |
|
|
} |
227 |
twoaday |
193 |
else if ((lv_idx = listview_get_selected_item (lv)) == -1) { |
228 |
werner |
36 |
rc = log_box (_("Signing"), MB_YESNO, |
229 |
twoaday |
225 |
_("No key was chosen.\n" |
230 |
|
|
"Use the GPG default key '%s'?"), |
231 |
|
|
lookup_key_userid (signer)); |
232 |
werner |
36 |
if (rc == IDNO) { |
233 |
|
|
free_if_alloc (signer); |
234 |
|
|
return FALSE; |
235 |
|
|
} |
236 |
twoaday |
225 |
sel_signer = 0; |
237 |
werner |
36 |
} |
238 |
twoaday |
225 |
if (sel_signer) { |
239 |
werner |
36 |
free_if_alloc (signer); |
240 |
|
|
signer = new char[32+1]; |
241 |
|
|
if (!signer) |
242 |
|
|
BUG (NULL); |
243 |
twoaday |
225 |
listview_get_item_text (lv, lv_idx, KM_COL_KEYID, signer, 32); |
244 |
werner |
36 |
} |
245 |
|
|
err = gpgme_new (&ctx); |
246 |
|
|
if (err) |
247 |
twoaday |
225 |
BUG (NULL); |
248 |
werner |
36 |
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 |
twoaday |
256 |
if (pwd.cancel && gpgme_err_code (err) == GPG_ERR_BAD_PASSPHRASE) { |
254 |
werner |
36 |
/* 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 |
twoaday |
225 |
return TRUE; |
262 |
werner |
36 |
} |
263 |
twoaday |
225 |
else |
264 |
|
|
show_msg (dlg, 1500, _("GnuPG Status: Finished")); |
265 |
werner |
36 |
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 |
|
|
} |