1 |
/* wptClipSignDlg.cpp - WinPT clipboard sign dialog |
2 |
* Copyright (C) 2000-2006, 2009 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 |
|
18 |
#ifdef HAVE_CONFIG_H |
19 |
#include <config.h> |
20 |
#endif |
21 |
|
22 |
#include <windows.h> |
23 |
#include <commctrl.h> |
24 |
|
25 |
#include "resource.h" |
26 |
#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 |
err = gpg_data_utf8_new_from_clipboard (&plain, wraplen, NULL); |
56 |
if (err) |
57 |
return err; |
58 |
err = get_pubkey (keyid, &key); |
59 |
if (err) |
60 |
goto leave; |
61 |
err = gpgme_signers_add (ctx, key); |
62 |
if (err) |
63 |
goto leave; |
64 |
err = gpgme_data_new (&sig); |
65 |
if (err) |
66 |
goto leave; |
67 |
err = gpgme_op_sign (ctx, plain, sig, GPGME_SIG_MODE_CLEAR); |
68 |
if (err) |
69 |
goto leave; |
70 |
|
71 |
// 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 |
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 keylist_ctrl_t |
144 |
on_init_dialog (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
145 |
{ |
146 |
keylist_ctrl_t kl; |
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 |
kl = 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 kl; |
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 keylist_ctrl_t kl = NULL; |
188 |
gpgme_ctx_t ctx; |
189 |
gpgme_error_t err; |
190 |
passphrase_cb_s pwd; |
191 |
int lv_idx = 0; |
192 |
int sel_signer = 0; |
193 |
char *signer = NULL; |
194 |
|
195 |
/* XXX: there seems to be a memory corruption in the code */ |
196 |
|
197 |
switch (msg) { |
198 |
case WM_INITDIALOG: |
199 |
kl = on_init_dialog (dlg, msg, wparam, lparam); |
200 |
if (!kl) |
201 |
EndDialog (dlg, TRUE); |
202 |
return FALSE; |
203 |
|
204 |
case WM_DESTROY: |
205 |
if (kl) { |
206 |
keylist_delete (kl); |
207 |
kl = NULL; |
208 |
} |
209 |
return FALSE; |
210 |
|
211 |
case WM_NOTIFY: |
212 |
/* Wait until the dialog initialization is done */ |
213 |
if (kl == NULL) |
214 |
break; |
215 |
|
216 |
NMHDR *notify; |
217 |
notify = (NMHDR *)lparam; |
218 |
if (notify && notify->code == NM_DBLCLK && |
219 |
notify->idFrom == IDC_SIGN_KEYLIST) |
220 |
PostMessage (dlg, WM_COMMAND, MAKEWPARAM (IDOK, 0), 0); |
221 |
|
222 |
return FALSE; |
223 |
|
224 |
case WM_COMMAND: |
225 |
switch (LOWORD (wparam)) { |
226 |
case IDOK: |
227 |
sel_signer = 1; |
228 |
signer = get_gnupg_default_key (); |
229 |
if (!signer) { |
230 |
msg_box (dlg, _("Could not get default key."), _("Signing"), MB_ERR); |
231 |
return FALSE; |
232 |
} |
233 |
else if ((lv_idx = listview_get_selected_item (kl->lv)) == -1) { |
234 |
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 |
if (rc == IDNO) { |
239 |
free_if_alloc (signer); |
240 |
return FALSE; |
241 |
} |
242 |
sel_signer = 0; |
243 |
} |
244 |
if (sel_signer) { |
245 |
free_if_alloc (signer); |
246 |
signer = new char[32+1]; |
247 |
if (!signer) |
248 |
BUG (NULL); |
249 |
listview_get_item_text (kl->lv, lv_idx, KM_COL_KEYID, signer, 32); |
250 |
} |
251 |
err = gpgme_new (&ctx); |
252 |
if (err) |
253 |
BUG (NULL); |
254 |
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 |
gpgme_release (ctx); |
259 |
if (pwd.cancel && gpgme_err_code (err) == GPG_ERR_BAD_PASSPHRASE) { |
260 |
/* The user hit the cancel button or bad passphrase */ |
261 |
return TRUE; |
262 |
} |
263 |
if (err) { |
264 |
msg_box (dlg, gpgme_strerror (err), _("Signing"), MB_ERR); |
265 |
return TRUE; |
266 |
} |
267 |
else |
268 |
show_msg (dlg, 1500, _("GnuPG Status: Finished")); |
269 |
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 |
} |