1 |
werner |
36 |
/* wptClipEncryptDlg.cpp - Clipboard encrypt dialog |
2 |
twoaday |
220 |
* 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 |
197 |
|
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 "wptW32API.h" |
32 |
|
|
#include "wptVersion.h" |
33 |
|
|
#include "wptErrors.h" |
34 |
|
|
#include "wptCommonCtl.h" |
35 |
|
|
#include "wptGPG.h" |
36 |
|
|
#include "wptKeylist.h" |
37 |
|
|
#include "wptNLS.h" |
38 |
|
|
#include "wptContext.h" /* for passphrase_s */ |
39 |
|
|
#include "wptRegistry.h" |
40 |
|
|
#include "wptDlgs.h" |
41 |
twoaday |
197 |
#include "wptUTF8.h" |
42 |
werner |
36 |
|
43 |
|
|
|
44 |
|
|
/* Encrypt the contents of the clipboard with the keys in @rset. |
45 |
|
|
If @always_trust is set, GPG is forced to trust any recipients. |
46 |
|
|
The context is returned in @r_ctx. |
47 |
|
|
Return value: 0 on success. */ |
48 |
|
|
gpgme_error_t |
49 |
|
|
gpg_clip_encrypt (gpgme_key_t rset[], int always_trust, gpgme_ctx_t *r_ctx) |
50 |
|
|
{ |
51 |
|
|
gpgme_error_t err; |
52 |
|
|
gpgme_ctx_t ctx = NULL; |
53 |
|
|
gpgme_data_t plain = NULL; |
54 |
|
|
gpgme_data_t ciph = NULL; |
55 |
|
|
|
56 |
|
|
err = gpgme_new (&ctx); |
57 |
|
|
if (err) |
58 |
|
|
return err; |
59 |
|
|
|
60 |
|
|
gpgme_set_armor (ctx, 1); |
61 |
twoaday |
175 |
gpgme_set_textmode (ctx, 1); |
62 |
werner |
36 |
|
63 |
|
|
err = gpg_data_new_from_clipboard (&plain, 0); |
64 |
|
|
if (err) |
65 |
|
|
goto leave; |
66 |
|
|
err = gpgme_data_new (&ciph); |
67 |
|
|
if (err) |
68 |
twoaday |
197 |
goto leave; |
69 |
werner |
36 |
err = gpgme_op_encrypt (ctx, rset, |
70 |
|
|
always_trust?GPGME_ENCRYPT_ALWAYS_TRUST : (gpgme_encrypt_flags_t)0, |
71 |
|
|
plain, ciph); |
72 |
|
|
*r_ctx = ctx; |
73 |
|
|
if (err) |
74 |
|
|
goto leave; |
75 |
|
|
|
76 |
|
|
gpg_data_release_and_set_clipboard (ciph, 1); |
77 |
|
|
ciph = NULL; |
78 |
|
|
|
79 |
|
|
leave: |
80 |
|
|
if (ciph) |
81 |
|
|
gpgme_data_release (ciph); |
82 |
|
|
gpgme_data_release (plain); |
83 |
|
|
return err; |
84 |
|
|
} |
85 |
|
|
|
86 |
|
|
|
87 |
|
|
/* Show all invalid recipients if possible. @dlg is the |
88 |
|
|
handle to the calling dialog and @ctx is the context |
89 |
|
|
used for encryption. |
90 |
|
|
Return value: 0 if invalid recipients exist -1 otherwise. */ |
91 |
|
|
static int |
92 |
|
|
show_invalid_recipients (HWND dlg, gpgme_ctx_t ctx) |
93 |
|
|
{ |
94 |
|
|
gpgme_encrypt_result_t res; |
95 |
|
|
gpgme_invalid_key_t k; |
96 |
|
|
gpgme_key_t key; |
97 |
|
|
size_t len=0; |
98 |
twoaday |
197 |
char *uid, *p; |
99 |
werner |
36 |
|
100 |
|
|
if (!ctx) |
101 |
|
|
return -1; |
102 |
|
|
res = gpgme_op_encrypt_result (ctx); |
103 |
|
|
if (!res || !res->invalid_recipients) |
104 |
|
|
return -1; |
105 |
|
|
|
106 |
|
|
for (k=res->invalid_recipients; k; k = k->next) { |
107 |
twoaday |
197 |
if (get_pubkey (k->fpr, &key)) |
108 |
|
|
continue; |
109 |
werner |
36 |
len += (32 + 16 + strlen (key->uids->name) + 2) + 4; |
110 |
|
|
} |
111 |
|
|
|
112 |
|
|
p = (char *)calloc (1, len+64); |
113 |
|
|
if (!p) |
114 |
|
|
BUG (NULL); |
115 |
twoaday |
68 |
strcpy (p, _("Recipients unsuable for encryption:\n")); |
116 |
werner |
36 |
for (k = res->invalid_recipients; k; k = k->next) { |
117 |
twoaday |
197 |
if (get_pubkey (k->fpr, &key)) |
118 |
|
|
continue; |
119 |
|
|
uid = utf8_to_native (key->uids->name); |
120 |
werner |
36 |
strcat (p, key->subkeys->keyid+8); |
121 |
|
|
strcat (p, " : "); |
122 |
twoaday |
197 |
strcat (p, uid); |
123 |
werner |
36 |
strcat (p, "\n"); |
124 |
twoaday |
197 |
safe_free (uid); |
125 |
werner |
36 |
} |
126 |
|
|
msg_box (dlg, p, _("Encryption"), MB_ERR); |
127 |
twoaday |
220 |
safe_free (p); |
128 |
werner |
36 |
return 0; |
129 |
|
|
} |
130 |
|
|
|
131 |
|
|
|
132 |
|
|
/* Dialog procedure for the clipboard encryption. */ |
133 |
|
|
BOOL CALLBACK |
134 |
|
|
clip_encrypt_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
135 |
|
|
{ |
136 |
|
|
static listview_ctrl_t lv = NULL; |
137 |
|
|
gpg_keycache_t kc; |
138 |
|
|
gpgme_key_t *rset; |
139 |
|
|
gpgme_ctx_t ctx=NULL; |
140 |
|
|
gpgme_error_t err; |
141 |
twoaday |
68 |
int force_trust = 0; |
142 |
werner |
36 |
int n; |
143 |
|
|
|
144 |
twoaday |
197 |
switch (msg) { |
145 |
werner |
36 |
case WM_INITDIALOG: |
146 |
twoaday |
105 |
SetWindowText (dlg, _("Encryption")); |
147 |
|
|
SetDlgItemText (dlg, IDC_ENCRYPT_FNDCMD, _("&Find")); |
148 |
|
|
SetDlgItemText (dlg, IDCANCEL, _("&Cancel")); |
149 |
twoaday |
197 |
kc = keycache_get_ctx (KEYCACHE_PUB); |
150 |
|
|
lv = keylist_load (GetDlgItem (dlg, IDC_ENCRYPT_KEYLIST), kc, NULL, |
151 |
twoaday |
176 |
KEYLIST_ENCRYPT_MIN, KEY_SORT_USERID); |
152 |
twoaday |
197 |
center_window (dlg, NULL); |
153 |
|
|
SetForegroundWindow (dlg); |
154 |
werner |
36 |
return TRUE; |
155 |
|
|
|
156 |
|
|
case WM_DESTROY: |
157 |
twoaday |
220 |
if (lv) { |
158 |
|
|
keylist_delete (lv); |
159 |
werner |
36 |
lv = NULL; |
160 |
|
|
} |
161 |
|
|
return FALSE; |
162 |
|
|
|
163 |
|
|
case WM_NOTIFY: |
164 |
|
|
NMHDR *notify; |
165 |
|
|
|
166 |
|
|
notify = (NMHDR *)lparam; |
167 |
|
|
if (notify && notify->code == NM_DBLCLK && |
168 |
|
|
notify->idFrom == IDC_ENCRYPT_KEYLIST) |
169 |
twoaday |
105 |
PostMessage( dlg, WM_COMMAND, MAKEWPARAM( IDOK, 0 ), 0 ); |
170 |
werner |
36 |
if (notify && notify->code == LVN_COLUMNCLICK && |
171 |
|
|
notify->idFrom == IDC_ENCRYPT_KEYLIST ) { |
172 |
|
|
NMLISTVIEW *p = (LPNMLISTVIEW) lparam; |
173 |
|
|
int sortby = 0; |
174 |
|
|
|
175 |
twoaday |
220 |
switch (p->iSubItem) { |
176 |
werner |
36 |
case 0: sortby = KEY_SORT_USERID; break; |
177 |
|
|
case 1: sortby = KEY_SORT_KEYID; break; |
178 |
|
|
case 2: sortby = KEY_SORT_LEN; break; |
179 |
|
|
case 4: sortby = KEY_SORT_VALIDITY; break; |
180 |
|
|
default: sortby = KEY_SORT_USERID; break; |
181 |
|
|
} |
182 |
twoaday |
220 |
keylist_sort (lv, sortby); |
183 |
werner |
36 |
} |
184 |
|
|
return TRUE; |
185 |
|
|
|
186 |
|
|
case WM_COMMAND: |
187 |
|
|
switch( LOWORD( wparam ) ) { |
188 |
|
|
case IDOK: |
189 |
|
|
rset = keylist_get_recipients (lv, &force_trust, &n); |
190 |
|
|
if (!n) { |
191 |
twoaday |
197 |
msg_box (dlg, _("You must select at least one key."), |
192 |
|
|
_("Encryption"), MB_ERR); |
193 |
|
|
safe_free (rset); |
194 |
werner |
36 |
return FALSE; |
195 |
|
|
} |
196 |
|
|
err = gpg_clip_encrypt (rset, force_trust, &ctx); |
197 |
|
|
if (err) { |
198 |
|
|
if (show_invalid_recipients (dlg, ctx)) |
199 |
|
|
msg_box (dlg, gpgme_strerror (err), _("Encryption"), MB_ERR); |
200 |
|
|
} |
201 |
|
|
else |
202 |
twoaday |
220 |
show_msg (dlg, 1500, _("GnuPG Status: Finished")); |
203 |
|
|
safe_free (rset); |
204 |
|
|
if (ctx) |
205 |
|
|
gpgme_release (ctx); |
206 |
|
|
if (!err) |
207 |
|
|
EndDialog (dlg, TRUE); |
208 |
werner |
36 |
return TRUE; |
209 |
|
|
|
210 |
|
|
case IDCANCEL: |
211 |
twoaday |
220 |
EndDialog (dlg, FALSE); |
212 |
werner |
36 |
return FALSE; |
213 |
|
|
|
214 |
|
|
case IDC_ENCRYPT_FNDCMD: |
215 |
|
|
{ |
216 |
|
|
char tmpbuf[64]; |
217 |
|
|
|
218 |
|
|
n = GetDlgItemText (dlg, IDC_ENCRYPT_FIND, tmpbuf, DIM (tmpbuf)-1); |
219 |
|
|
if (!n) |
220 |
|
|
break; |
221 |
|
|
n = listview_find (lv, tmpbuf); |
222 |
|
|
if (n != -1) { |
223 |
|
|
int oldpos = listview_get_curr_pos (lv); |
224 |
|
|
listview_select_one (lv, n); |
225 |
|
|
listview_scroll (lv, oldpos, n); |
226 |
|
|
} |
227 |
|
|
else |
228 |
|
|
log_box (_("Encryption"), MB_ERR, _("No recipient found with '%s'"), tmpbuf); |
229 |
|
|
break; |
230 |
|
|
} |
231 |
|
|
} |
232 |
|
|
break; |
233 |
|
|
} |
234 |
|
|
|
235 |
|
|
return FALSE; |
236 |
|
|
} |
237 |
|
|
|