1 |
twoaday |
2 |
/* wptClipEncryptDlg.cpp - Clipboard encrypt dialog |
2 |
twoaday |
22 |
* Copyright (C) 2000-2005 Timo Schulz |
3 |
twoaday |
2 |
* |
4 |
|
|
* This file is part of WinPT. |
5 |
|
|
* |
6 |
|
|
* WinPT is free software; you can redistribute it and/or modify |
7 |
|
|
* it under the terms of the GNU General Public License as published by |
8 |
|
|
* the Free Software Foundation; either version 2 of the License, or |
9 |
|
|
* (at your option) any later version. |
10 |
|
|
* |
11 |
|
|
* WinPT is distributed in the hope that it will be useful, |
12 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
|
|
* GNU General Public License for more details. |
15 |
|
|
* |
16 |
|
|
* You should have received a copy of the GNU General Public License |
17 |
|
|
* along with WinPT; if not, write to the Free Software Foundation, |
18 |
|
|
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
19 |
|
|
*/ |
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 |
|
|
|
37 |
|
|
|
38 |
twoaday |
23 |
/* Encrypt the contents of the clipboard with the keys in @rset. |
39 |
|
|
If @always_trust is set, GPG is forced to trust any recipients. |
40 |
|
|
The context is returned in @r_ctx. |
41 |
|
|
Return value: 0 on success. */ |
42 |
|
|
gpgme_error_t |
43 |
|
|
gpg_clip_encrypt (gpgme_key_t rset[], int always_trust, gpgme_ctx_t *r_ctx) |
44 |
|
|
{ |
45 |
|
|
gpgme_error_t err; |
46 |
|
|
gpgme_ctx_t ctx = NULL; |
47 |
|
|
gpgme_data_t plain = NULL; |
48 |
|
|
gpgme_data_t ciph = NULL; |
49 |
|
|
|
50 |
|
|
err = gpgme_new(&ctx); |
51 |
|
|
if (err) |
52 |
|
|
return err; |
53 |
|
|
|
54 |
|
|
gpgme_set_armor (ctx, 1); |
55 |
|
|
|
56 |
|
|
err = gpg_data_new_from_clipboard (&plain, 0); |
57 |
|
|
if (err) |
58 |
|
|
goto leave; |
59 |
|
|
err = gpgme_data_new (&ciph); |
60 |
|
|
if (err) |
61 |
|
|
goto leave; |
62 |
|
|
err = gpgme_op_encrypt (ctx, rset, |
63 |
|
|
always_trust?GPGME_ENCRYPT_ALWAYS_TRUST : (gpgme_encrypt_flags_t)0, |
64 |
|
|
plain, ciph); |
65 |
|
|
if (err) |
66 |
|
|
goto leave; |
67 |
|
|
|
68 |
|
|
gpg_data_release_and_set_clipboard (ciph, 1); |
69 |
|
|
ciph = NULL; |
70 |
|
|
*r_ctx = ctx; |
71 |
|
|
|
72 |
|
|
leave: |
73 |
|
|
if (err) { |
74 |
|
|
gpgme_release (ctx); |
75 |
|
|
*r_ctx = NULL; |
76 |
|
|
} |
77 |
|
|
if (ciph) |
78 |
|
|
gpgme_data_release (ciph); |
79 |
|
|
gpgme_data_release (plain); |
80 |
|
|
return err; |
81 |
|
|
} |
82 |
|
|
|
83 |
|
|
|
84 |
|
|
/* Show all invalid recipients if possible. @dlg is the |
85 |
|
|
handle to the calling dialog and @ctx is the context |
86 |
|
|
used for encryption. |
87 |
|
|
Return value: 0 if invalid recipients exist -1 otherwise. */ |
88 |
|
|
static int |
89 |
|
|
show_invalid_recipients (HWND dlg, gpgme_ctx_t ctx) |
90 |
|
|
{ |
91 |
|
|
gpgme_encrypt_result_t res; |
92 |
|
|
gpgme_invalid_key_t k; |
93 |
|
|
gpgme_key_t key; |
94 |
|
|
size_t len=0; |
95 |
|
|
char *p; |
96 |
|
|
|
97 |
|
|
res = gpgme_op_encrypt_result (ctx); |
98 |
|
|
if (!res || !res->invalid_recipients) |
99 |
|
|
return -1; |
100 |
|
|
|
101 |
|
|
for (k=res->invalid_recipients; k; k = k->next) { |
102 |
|
|
get_pubkey (k->fpr, &key); |
103 |
|
|
len += (32 + 16 + strlen (key->uids->name) + 2) + 4; |
104 |
|
|
} |
105 |
|
|
|
106 |
|
|
p = (char *)calloc (1, len+64); |
107 |
|
|
if (!p) |
108 |
|
|
BUG (NULL); |
109 |
|
|
sprintf (p, _("Recipients unsuable for encryption:\n")); |
110 |
|
|
for (k = res->invalid_recipients; k; k = k->next) { |
111 |
|
|
get_pubkey (k->fpr, &key); |
112 |
|
|
strcat (p, key->subkeys->keyid); |
113 |
|
|
strcat (p, " : "); |
114 |
|
|
strcat (p, key->uids->name); |
115 |
|
|
strcat (p, "\n"); |
116 |
|
|
} |
117 |
|
|
msg_box (dlg, p, _("Encryption"), MB_ERR); |
118 |
|
|
free (p); |
119 |
|
|
return 0; |
120 |
|
|
} |
121 |
|
|
|
122 |
|
|
|
123 |
|
|
/* Dialog procedure for the clipboard encryption. */ |
124 |
twoaday |
2 |
BOOL CALLBACK |
125 |
twoaday |
23 |
clip_encrypt_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
126 |
twoaday |
2 |
{ |
127 |
|
|
static listview_ctrl_t lv = NULL; |
128 |
|
|
gpgme_keycache_t kc; |
129 |
twoaday |
23 |
gpgme_key_t *rset; |
130 |
twoaday |
2 |
gpgme_ctx_t ctx; |
131 |
|
|
gpgme_error_t err; |
132 |
|
|
refresh_cache_s rcs = {0}; |
133 |
|
|
int force_trust = 0, opt = 0; |
134 |
|
|
int kmode = reg_prefs.keylist_mode? KEYLIST_ENCRYPT_MIN : KEYLIST_ENCRYPT; |
135 |
twoaday |
23 |
int n; |
136 |
twoaday |
2 |
|
137 |
|
|
switch( msg ) { |
138 |
|
|
case WM_INITDIALOG: |
139 |
|
|
SetWindowText( dlg, _("Encryption") ); |
140 |
|
|
kc = keycache_get_ctx( KEYCACHE_PUB ); |
141 |
|
|
if( !kc ) |
142 |
|
|
BUG( NULL ); |
143 |
twoaday |
23 |
lv = keylist_load (GetDlgItem( dlg, IDC_ENCRYPT_KEYLIST ), kc, NULL, |
144 |
|
|
kmode, KEY_SORT_USERID); |
145 |
|
|
center_window( dlg, NULL ); |
146 |
twoaday |
2 |
SetForegroundWindow( dlg ); |
147 |
|
|
set_active_window( dlg ); |
148 |
|
|
return TRUE; |
149 |
|
|
|
150 |
|
|
case WM_DESTROY: |
151 |
|
|
reset_active_window( ); |
152 |
|
|
if( lv ) { |
153 |
|
|
keylist_delete( lv ); |
154 |
|
|
lv = NULL; |
155 |
|
|
} |
156 |
|
|
return FALSE; |
157 |
|
|
|
158 |
|
|
case WM_NOTIFY: |
159 |
|
|
NMHDR *notify; |
160 |
|
|
|
161 |
|
|
notify = (NMHDR *)lparam; |
162 |
twoaday |
23 |
if (notify && notify->code == NM_DBLCLK && |
163 |
|
|
notify->idFrom == IDC_ENCRYPT_KEYLIST) |
164 |
twoaday |
2 |
PostMessage( dlg, WM_COMMAND, MAKEWPARAM( IDOK, 0 ), NULL ); |
165 |
twoaday |
23 |
if (notify && notify->code == LVN_COLUMNCLICK && |
166 |
|
|
notify->idFrom == IDC_ENCRYPT_KEYLIST ) { |
167 |
twoaday |
2 |
NMLISTVIEW *p = (LPNMLISTVIEW) lparam; |
168 |
|
|
int sortby = 0; |
169 |
|
|
|
170 |
|
|
switch( p->iSubItem ) { |
171 |
twoaday |
23 |
case 0: sortby = KEY_SORT_USERID; break; |
172 |
|
|
case 1: sortby = KEY_SORT_KEYID; break; |
173 |
|
|
case 2: sortby = KEY_SORT_LEN; break; |
174 |
|
|
case 4: sortby = KEY_SORT_VALIDITY; break; |
175 |
|
|
default: sortby = KEY_SORT_USERID; break; |
176 |
twoaday |
2 |
} |
177 |
|
|
keylist_sort( lv, sortby ); |
178 |
|
|
} |
179 |
|
|
return TRUE; |
180 |
|
|
|
181 |
|
|
case WM_SYSCOMMAND: |
182 |
|
|
if( LOWORD( wparam ) == SC_CLOSE ) |
183 |
|
|
EndDialog( dlg, TRUE ); |
184 |
|
|
return FALSE; |
185 |
|
|
|
186 |
|
|
case WM_COMMAND: |
187 |
|
|
switch( LOWORD( wparam ) ) { |
188 |
|
|
case IDOK: |
189 |
twoaday |
23 |
rset = keylist_get_recipients (lv, &force_trust, &n); |
190 |
|
|
if (!n) { |
191 |
|
|
msg_box (dlg, _("You must select at least one key."), _("Encryption"), MB_ERR); |
192 |
|
|
free (rset); |
193 |
twoaday |
2 |
return FALSE; |
194 |
|
|
} |
195 |
twoaday |
23 |
err = gpg_clip_encrypt (rset, force_trust, &ctx); |
196 |
|
|
if (err) { |
197 |
|
|
if (show_invalid_recipients (dlg, ctx)) |
198 |
|
|
msg_box (dlg, gpgme_strerror (err), _("Encryption"), MB_ERR); |
199 |
|
|
gpgme_release (ctx); |
200 |
|
|
free (rset); |
201 |
twoaday |
2 |
return FALSE; |
202 |
|
|
} |
203 |
|
|
else |
204 |
|
|
show_msg( dlg, 1500, _("GnuPG Status: Finished") ); |
205 |
twoaday |
23 |
free (rset); |
206 |
|
|
gpgme_release (ctx); |
207 |
|
|
EndDialog (dlg, TRUE); |
208 |
twoaday |
2 |
return TRUE; |
209 |
|
|
|
210 |
|
|
case IDCANCEL: |
211 |
|
|
EndDialog( dlg, FALSE ); |
212 |
|
|
return FALSE; |
213 |
|
|
|
214 |
|
|
case IDC_ENCRYPT_FNDCMD: |
215 |
|
|
{ |
216 |
|
|
char tmpbuf[64]; |
217 |
|
|
int n; |
218 |
|
|
|
219 |
|
|
n = GetDlgItemText (dlg, IDC_ENCRYPT_FIND, tmpbuf, DIM (tmpbuf)-1); |
220 |
|
|
if (!n) |
221 |
|
|
break; |
222 |
|
|
n = listview_find (lv, tmpbuf); |
223 |
twoaday |
22 |
if (n != -1) { |
224 |
|
|
int oldpos = listview_get_curr_pos (lv); |
225 |
twoaday |
2 |
listview_select_one (lv, n); |
226 |
twoaday |
22 |
listview_scroll (lv, oldpos, n); |
227 |
|
|
} |
228 |
twoaday |
2 |
else |
229 |
|
|
log_box (_("Encryption"), MB_ERR, _("No recipient found with '%s'"), tmpbuf); |
230 |
|
|
break; |
231 |
|
|
} |
232 |
|
|
} |
233 |
|
|
break; |
234 |
|
|
} |
235 |
|
|
|
236 |
|
|
return FALSE; |
237 |
twoaday |
23 |
} |
238 |
|
|
|