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