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