1 |
werner |
36 |
/* wptClipEncryptDlg.cpp - Clipboard encrypt 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 |
|
|
#ifdef HAVE_CONFIG_H |
18 |
|
|
#include <config.h> |
19 |
|
|
#endif |
20 |
|
|
|
21 |
|
|
#include <windows.h> |
22 |
|
|
#include <commctrl.h> |
23 |
|
|
|
24 |
werner |
47 |
#include "resource.h" |
25 |
werner |
36 |
#include "wptTypes.h" |
26 |
|
|
#include "wptW32API.h" |
27 |
|
|
#include "wptVersion.h" |
28 |
|
|
#include "wptErrors.h" |
29 |
|
|
#include "wptCommonCtl.h" |
30 |
|
|
#include "wptGPG.h" |
31 |
|
|
#include "wptKeylist.h" |
32 |
|
|
#include "wptNLS.h" |
33 |
|
|
#include "wptContext.h" /* for passphrase_s */ |
34 |
|
|
#include "wptRegistry.h" |
35 |
|
|
#include "wptDlgs.h" |
36 |
twoaday |
197 |
#include "wptUTF8.h" |
37 |
twoaday |
278 |
#include "StringBuffer.h" |
38 |
werner |
36 |
|
39 |
|
|
|
40 |
|
|
/* Encrypt the contents of the clipboard with the keys in @rset. |
41 |
|
|
If @always_trust is set, GPG is forced to trust any recipients. |
42 |
|
|
The context is returned in @r_ctx. |
43 |
|
|
Return value: 0 on success. */ |
44 |
|
|
gpgme_error_t |
45 |
|
|
gpg_clip_encrypt (gpgme_key_t rset[], int always_trust, gpgme_ctx_t *r_ctx) |
46 |
|
|
{ |
47 |
|
|
gpgme_error_t err; |
48 |
twoaday |
256 |
gpgme_encrypt_flags_t flags; |
49 |
werner |
36 |
gpgme_ctx_t ctx = NULL; |
50 |
|
|
gpgme_data_t plain = NULL; |
51 |
|
|
gpgme_data_t ciph = NULL; |
52 |
|
|
|
53 |
|
|
err = gpgme_new (&ctx); |
54 |
|
|
if (err) |
55 |
|
|
return err; |
56 |
twoaday |
273 |
|
57 |
|
|
/* Recently a lot of users got in trouble because they enabled |
58 |
|
|
the textmode even for non-text (binary) data. This leads to |
59 |
|
|
serious trouble. But because our clipboard functions only support |
60 |
|
|
the text format, the returned data is pure text or at least a |
61 |
twoaday |
328 |
conversion of it, so we can safely use the textmode here. */ |
62 |
twoaday |
273 |
gpgme_set_textmode (ctx, 1); |
63 |
werner |
36 |
gpgme_set_armor (ctx, 1); |
64 |
|
|
|
65 |
twoaday |
328 |
err = gpg_data_utf8_new_from_clipboard (&plain, 0, NULL); |
66 |
werner |
36 |
if (err) |
67 |
|
|
goto leave; |
68 |
|
|
err = gpgme_data_new (&ciph); |
69 |
|
|
if (err) |
70 |
twoaday |
197 |
goto leave; |
71 |
twoaday |
328 |
flags = always_trust? GPGME_ENCRYPT_ALWAYS_TRUST : |
72 |
|
|
(gpgme_encrypt_flags_t)0; |
73 |
twoaday |
256 |
err = gpgme_op_encrypt (ctx, rset, flags, plain, ciph); |
74 |
werner |
36 |
*r_ctx = ctx; |
75 |
|
|
if (err) |
76 |
|
|
goto leave; |
77 |
|
|
|
78 |
twoaday |
328 |
gpg_data_release_to_clipboard (ciph, 1); |
79 |
werner |
36 |
ciph = NULL; |
80 |
|
|
|
81 |
|
|
leave: |
82 |
|
|
if (ciph) |
83 |
|
|
gpgme_data_release (ciph); |
84 |
|
|
gpgme_data_release (plain); |
85 |
|
|
return err; |
86 |
|
|
} |
87 |
|
|
|
88 |
|
|
|
89 |
|
|
/* Show all invalid recipients if possible. @dlg is the |
90 |
|
|
handle to the calling dialog and @ctx is the context |
91 |
|
|
used for encryption. |
92 |
|
|
Return value: 0 if invalid recipients exist -1 otherwise. */ |
93 |
|
|
static int |
94 |
|
|
show_invalid_recipients (HWND dlg, gpgme_ctx_t ctx) |
95 |
|
|
{ |
96 |
|
|
gpgme_encrypt_result_t res; |
97 |
|
|
gpgme_invalid_key_t k; |
98 |
|
|
gpgme_key_t key; |
99 |
twoaday |
278 |
StringBuffer p; |
100 |
twoaday |
225 |
const char *keyid; |
101 |
|
|
const char *warn = _("key not found"); |
102 |
twoaday |
278 |
char *uid; |
103 |
werner |
36 |
|
104 |
|
|
if (!ctx) |
105 |
|
|
return -1; |
106 |
|
|
res = gpgme_op_encrypt_result (ctx); |
107 |
|
|
if (!res || !res->invalid_recipients) |
108 |
|
|
return -1; |
109 |
|
|
|
110 |
twoaday |
328 |
p = _("Recipients unusable for encryption:\n"); |
111 |
werner |
36 |
for (k = res->invalid_recipients; k; k = k->next) { |
112 |
twoaday |
225 |
if (!get_pubkey (k->fpr, &key)) { |
113 |
|
|
uid = utf8_to_native (key->uids->name); |
114 |
|
|
keyid = key->subkeys->keyid+8; |
115 |
|
|
} |
116 |
|
|
else { |
117 |
|
|
uid = strdup (warn); |
118 |
|
|
keyid = k->fpr; |
119 |
|
|
} |
120 |
twoaday |
278 |
p = p + keyid + " : " + uid + "\n"; |
121 |
twoaday |
197 |
safe_free (uid); |
122 |
werner |
36 |
} |
123 |
twoaday |
278 |
msg_box (dlg, p.getBuffer (), _("Encryption"), MB_ERR); |
124 |
werner |
36 |
return 0; |
125 |
|
|
} |
126 |
|
|
|
127 |
|
|
|
128 |
twoaday |
328 |
struct dlg_ctx_s { |
129 |
|
|
int keys_sortby; |
130 |
|
|
int enable_redraw; |
131 |
|
|
keylist_ctrl_t kl; |
132 |
|
|
}; |
133 |
|
|
// FIXME: associate the context with the window |
134 |
twoaday |
451 |
static struct dlg_ctx_s state = {0, 0, NULL}; |
135 |
twoaday |
328 |
|
136 |
werner |
36 |
/* Dialog procedure for the clipboard encryption. */ |
137 |
|
|
BOOL CALLBACK |
138 |
|
|
clip_encrypt_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
139 |
twoaday |
328 |
{ |
140 |
werner |
36 |
gpg_keycache_t kc; |
141 |
|
|
gpgme_key_t *rset; |
142 |
|
|
gpgme_ctx_t ctx=NULL; |
143 |
|
|
gpgme_error_t err; |
144 |
twoaday |
256 |
char tmpbuf[64]; |
145 |
twoaday |
328 |
size_t n; |
146 |
werner |
36 |
|
147 |
twoaday |
197 |
switch (msg) { |
148 |
werner |
36 |
case WM_INITDIALOG: |
149 |
twoaday |
105 |
SetWindowText (dlg, _("Encryption")); |
150 |
twoaday |
328 |
SetDlgItemText (dlg, IDC_ENCRYPT_FINDKEY, _("&Find Key:")); |
151 |
twoaday |
105 |
SetDlgItemText (dlg, IDCANCEL, _("&Cancel")); |
152 |
twoaday |
328 |
SetDlgItemText (dlg, IDC_ENCRYPT_FNDCMD, _("&Find")); |
153 |
twoaday |
197 |
kc = keycache_get_ctx (KEYCACHE_PUB); |
154 |
twoaday |
328 |
state.kl = keylist_load (GetDlgItem (dlg, IDC_ENCRYPT_KEYLIST), kc, NULL, |
155 |
|
|
KEYLIST_ENCRYPT_MIN, KEY_SORT_USERID); |
156 |
|
|
|
157 |
twoaday |
197 |
center_window (dlg, NULL); |
158 |
|
|
SetForegroundWindow (dlg); |
159 |
twoaday |
328 |
|
160 |
|
|
state.enable_redraw = 1; |
161 |
|
|
InvalidateRect(GetDlgItem (dlg, IDC_ENCRYPT_KEYLIST), NULL, TRUE); |
162 |
werner |
36 |
return TRUE; |
163 |
|
|
|
164 |
|
|
case WM_DESTROY: |
165 |
twoaday |
328 |
if (state.kl) { |
166 |
|
|
keylist_delete (state.kl); |
167 |
|
|
state.kl = NULL; |
168 |
werner |
36 |
} |
169 |
twoaday |
328 |
break; |
170 |
werner |
36 |
|
171 |
|
|
case WM_NOTIFY: |
172 |
twoaday |
451 |
|
173 |
|
|
/* Wait until the dialog initialization is done */ |
174 |
|
|
if (state.kl == NULL) |
175 |
|
|
break; |
176 |
|
|
|
177 |
|
|
NMHDR *notify; |
178 |
werner |
36 |
notify = (NMHDR *)lparam; |
179 |
twoaday |
328 |
if (!notify) |
180 |
|
|
break; |
181 |
|
|
|
182 |
|
|
if (state.enable_redraw) { |
183 |
|
|
int ret; |
184 |
|
|
ret = keylist_listview_notify (dlg, state.kl->keys, |
185 |
|
|
IDC_ENCRYPT_KEYLIST, lparam); |
186 |
|
|
if (ret != 0) { |
187 |
|
|
SetWindowLong (dlg, DWL_MSGRESULT, ret); |
188 |
|
|
return TRUE; |
189 |
|
|
} |
190 |
twoaday |
451 |
} |
191 |
twoaday |
328 |
|
192 |
werner |
36 |
if (notify && notify->code == NM_DBLCLK && |
193 |
|
|
notify->idFrom == IDC_ENCRYPT_KEYLIST) |
194 |
twoaday |
256 |
PostMessage( dlg, WM_COMMAND, MAKEWPARAM (IDOK, 0), 0); |
195 |
werner |
36 |
if (notify && notify->code == LVN_COLUMNCLICK && |
196 |
twoaday |
236 |
notify->idFrom == IDC_ENCRYPT_KEYLIST) { |
197 |
werner |
36 |
NMLISTVIEW *p = (LPNMLISTVIEW) lparam; |
198 |
|
|
int sortby = 0; |
199 |
|
|
|
200 |
twoaday |
220 |
switch (p->iSubItem) { |
201 |
werner |
36 |
case 0: sortby = KEY_SORT_USERID; break; |
202 |
|
|
case 1: sortby = KEY_SORT_KEYID; break; |
203 |
|
|
case 2: sortby = KEY_SORT_LEN; break; |
204 |
|
|
case 4: sortby = KEY_SORT_VALIDITY; break; |
205 |
|
|
default: sortby = KEY_SORT_USERID; break; |
206 |
|
|
} |
207 |
twoaday |
328 |
if ((state.keys_sortby & ~KEYLIST_SORT_DESC) == sortby) |
208 |
|
|
state.keys_sortby ^= KEYLIST_SORT_DESC; |
209 |
twoaday |
236 |
else |
210 |
twoaday |
328 |
state.keys_sortby = sortby; |
211 |
|
|
keylist_sort (state.kl, state.keys_sortby); |
212 |
twoaday |
451 |
return TRUE; |
213 |
werner |
36 |
} |
214 |
twoaday |
328 |
break; |
215 |
werner |
36 |
|
216 |
|
|
case WM_COMMAND: |
217 |
twoaday |
328 |
state.enable_redraw = 0; |
218 |
|
|
|
219 |
twoaday |
236 |
switch (LOWORD (wparam)) { |
220 |
werner |
36 |
case IDOK: |
221 |
twoaday |
328 |
int force_trust; |
222 |
|
|
rset = keylist_get_recipients (state.kl, &force_trust, &n); |
223 |
werner |
36 |
if (!n) { |
224 |
twoaday |
197 |
msg_box (dlg, _("You must select at least one key."), |
225 |
|
|
_("Encryption"), MB_ERR); |
226 |
|
|
safe_free (rset); |
227 |
werner |
36 |
return FALSE; |
228 |
|
|
} |
229 |
|
|
err = gpg_clip_encrypt (rset, force_trust, &ctx); |
230 |
|
|
if (err) { |
231 |
|
|
if (show_invalid_recipients (dlg, ctx)) |
232 |
|
|
msg_box (dlg, gpgme_strerror (err), _("Encryption"), MB_ERR); |
233 |
|
|
} |
234 |
|
|
else |
235 |
twoaday |
220 |
show_msg (dlg, 1500, _("GnuPG Status: Finished")); |
236 |
|
|
safe_free (rset); |
237 |
|
|
if (ctx) |
238 |
|
|
gpgme_release (ctx); |
239 |
|
|
if (!err) |
240 |
|
|
EndDialog (dlg, TRUE); |
241 |
twoaday |
328 |
break; |
242 |
werner |
36 |
|
243 |
|
|
case IDCANCEL: |
244 |
twoaday |
220 |
EndDialog (dlg, FALSE); |
245 |
twoaday |
328 |
break; |
246 |
werner |
36 |
|
247 |
|
|
case IDC_ENCRYPT_FNDCMD: |
248 |
|
|
n = GetDlgItemText (dlg, IDC_ENCRYPT_FIND, tmpbuf, DIM (tmpbuf)-1); |
249 |
|
|
if (!n) |
250 |
|
|
break; |
251 |
twoaday |
328 |
n = listview_find (state.kl->lv, tmpbuf, 0); |
252 |
twoaday |
256 |
if (n != -1) { |
253 |
twoaday |
328 |
int oldpos = listview_get_curr_pos (state.kl->lv); |
254 |
|
|
listview_select_one (state.kl->lv, n); |
255 |
|
|
listview_scroll (state.kl->lv, oldpos, n); |
256 |
werner |
36 |
} |
257 |
|
|
else |
258 |
twoaday |
256 |
log_box (_("Encryption"), MB_ERR, |
259 |
|
|
_("No recipient found with '%s'"), tmpbuf); |
260 |
werner |
36 |
break; |
261 |
|
|
} |
262 |
|
|
break; |
263 |
|
|
} |
264 |
|
|
|
265 |
|
|
return FALSE; |
266 |
|
|
} |
267 |
|
|
|