1 |
/* wptClipEncryptDlg.cpp - Clipboard encrypt 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 |
#ifdef HAVE_CONFIG_H |
18 |
#include <config.h> |
19 |
#endif |
20 |
|
21 |
#include <windows.h> |
22 |
#include <commctrl.h> |
23 |
|
24 |
#include "resource.h" |
25 |
#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 |
#include "wptUTF8.h" |
37 |
#include "StringBuffer.h" |
38 |
|
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 |
gpgme_encrypt_flags_t flags; |
49 |
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 |
|
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 |
conversion of it, so we can safely use the textmode here. */ |
62 |
gpgme_set_textmode (ctx, 1); |
63 |
gpgme_set_armor (ctx, 1); |
64 |
|
65 |
err = gpg_data_utf8_new_from_clipboard (&plain, 0, NULL); |
66 |
if (err) |
67 |
goto leave; |
68 |
err = gpgme_data_new (&ciph); |
69 |
if (err) |
70 |
goto leave; |
71 |
flags = always_trust? GPGME_ENCRYPT_ALWAYS_TRUST : |
72 |
(gpgme_encrypt_flags_t)0; |
73 |
err = gpgme_op_encrypt (ctx, rset, flags, plain, ciph); |
74 |
*r_ctx = ctx; |
75 |
if (err) |
76 |
goto leave; |
77 |
|
78 |
gpg_data_release_to_clipboard (ciph, 1); |
79 |
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 |
StringBuffer p; |
100 |
const char *keyid; |
101 |
const char *warn = _("key not found"); |
102 |
char *uid; |
103 |
|
104 |
if (!ctx) |
105 |
return -1; |
106 |
res = gpgme_op_encrypt_result (ctx); |
107 |
if (!res || !res->invalid_recipients) |
108 |
return -1; |
109 |
|
110 |
p = _("Recipients unusable for encryption:\n"); |
111 |
for (k = res->invalid_recipients; k; k = k->next) { |
112 |
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 |
p = p + keyid + " : " + uid + "\n"; |
121 |
safe_free (uid); |
122 |
} |
123 |
msg_box (dlg, p.getBuffer (), _("Encryption"), MB_ERR); |
124 |
return 0; |
125 |
} |
126 |
|
127 |
|
128 |
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 |
static struct dlg_ctx_s state = {0, 0, NULL}; |
135 |
|
136 |
/* Dialog procedure for the clipboard encryption. */ |
137 |
BOOL CALLBACK |
138 |
clip_encrypt_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
139 |
{ |
140 |
gpg_keycache_t kc; |
141 |
gpgme_key_t *rset; |
142 |
gpgme_ctx_t ctx=NULL; |
143 |
gpgme_error_t err; |
144 |
char tmpbuf[64]; |
145 |
size_t n; |
146 |
|
147 |
switch (msg) { |
148 |
case WM_INITDIALOG: |
149 |
SetWindowText (dlg, _("Encryption")); |
150 |
SetDlgItemText (dlg, IDC_ENCRYPT_FINDKEY, _("&Find Key:")); |
151 |
SetDlgItemText (dlg, IDCANCEL, _("&Cancel")); |
152 |
SetDlgItemText (dlg, IDC_ENCRYPT_FNDCMD, _("&Find")); |
153 |
kc = keycache_get_ctx (KEYCACHE_PUB); |
154 |
state.kl = keylist_load (GetDlgItem (dlg, IDC_ENCRYPT_KEYLIST), kc, NULL, |
155 |
KEYLIST_ENCRYPT_MIN, KEY_SORT_USERID); |
156 |
|
157 |
center_window (dlg, NULL); |
158 |
SetForegroundWindow (dlg); |
159 |
|
160 |
state.enable_redraw = 1; |
161 |
InvalidateRect(GetDlgItem (dlg, IDC_ENCRYPT_KEYLIST), NULL, TRUE); |
162 |
return TRUE; |
163 |
|
164 |
case WM_DESTROY: |
165 |
if (state.kl) { |
166 |
keylist_delete (state.kl); |
167 |
state.kl = NULL; |
168 |
} |
169 |
break; |
170 |
|
171 |
case WM_NOTIFY: |
172 |
|
173 |
/* Wait until the dialog initialization is done */ |
174 |
if (state.kl == NULL) |
175 |
break; |
176 |
|
177 |
NMHDR *notify; |
178 |
notify = (NMHDR *)lparam; |
179 |
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 |
} |
191 |
|
192 |
if (notify && notify->code == NM_DBLCLK && |
193 |
notify->idFrom == IDC_ENCRYPT_KEYLIST) |
194 |
PostMessage( dlg, WM_COMMAND, MAKEWPARAM (IDOK, 0), 0); |
195 |
if (notify && notify->code == LVN_COLUMNCLICK && |
196 |
notify->idFrom == IDC_ENCRYPT_KEYLIST) { |
197 |
NMLISTVIEW *p = (LPNMLISTVIEW) lparam; |
198 |
int sortby = 0; |
199 |
|
200 |
switch (p->iSubItem) { |
201 |
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 |
if ((state.keys_sortby & ~KEYLIST_SORT_DESC) == sortby) |
208 |
state.keys_sortby ^= KEYLIST_SORT_DESC; |
209 |
else |
210 |
state.keys_sortby = sortby; |
211 |
keylist_sort (state.kl, state.keys_sortby); |
212 |
return TRUE; |
213 |
} |
214 |
break; |
215 |
|
216 |
case WM_COMMAND: |
217 |
state.enable_redraw = 0; |
218 |
|
219 |
switch (LOWORD (wparam)) { |
220 |
case IDOK: |
221 |
int force_trust; |
222 |
rset = keylist_get_recipients (state.kl, &force_trust, &n); |
223 |
if (!n) { |
224 |
msg_box (dlg, _("You must select at least one key."), |
225 |
_("Encryption"), MB_ERR); |
226 |
safe_free (rset); |
227 |
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 |
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 |
break; |
242 |
|
243 |
case IDCANCEL: |
244 |
EndDialog (dlg, FALSE); |
245 |
break; |
246 |
|
247 |
case IDC_ENCRYPT_FNDCMD: |
248 |
n = GetDlgItemText (dlg, IDC_ENCRYPT_FIND, tmpbuf, DIM (tmpbuf)-1); |
249 |
if (!n) |
250 |
break; |
251 |
n = listview_find (state.kl->lv, tmpbuf, 0); |
252 |
if (n != -1) { |
253 |
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 |
} |
257 |
else |
258 |
log_box (_("Encryption"), MB_ERR, |
259 |
_("No recipient found with '%s'"), tmpbuf); |
260 |
break; |
261 |
} |
262 |
break; |
263 |
} |
264 |
|
265 |
return FALSE; |
266 |
} |
267 |
|