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