1 |
werner |
36 |
/* wptClipVerifyDlg.cpp - WinPT verify dialog |
2 |
|
|
* Copyright (C) 2001-2005 Timo Schulz |
3 |
|
|
* |
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 |
werner |
42 |
#ifdef HAVE_CONFIG_H |
21 |
|
|
#include <config.h> |
22 |
|
|
#endif |
23 |
|
|
|
24 |
werner |
36 |
#include <windows.h> |
25 |
twoaday |
41 |
#include <time.h> |
26 |
werner |
36 |
|
27 |
werner |
47 |
#include "resource.h" |
28 |
werner |
36 |
#include "wptGPG.h" |
29 |
|
|
#include "wptTypes.h" |
30 |
|
|
#include "wptCommonCtl.h" |
31 |
|
|
#include "wptKeylist.h" |
32 |
|
|
#include "wptW32API.h" |
33 |
|
|
#include "wptKeyserver.h" |
34 |
|
|
#include "wptNLS.h" |
35 |
|
|
#include "wptContext.h" /* for passwd_s */ |
36 |
|
|
#include "wptDlgs.h" |
37 |
|
|
#include "wptErrors.h" |
38 |
|
|
#include "wptVersion.h" |
39 |
|
|
|
40 |
|
|
/* XXX use out directly and do not use gpg_data_extract to store the plain text. */ |
41 |
|
|
|
42 |
|
|
|
43 |
|
|
/* Verify data from the clipboard. If @is_detached is set, a detached |
44 |
|
|
signature is assumed with the data the signature was calculated over |
45 |
|
|
in @det_data. The context is returned in @r_ctx and the signature |
46 |
|
|
in @r_sig. |
47 |
|
|
Return value: 0 on success. */ |
48 |
|
|
gpgme_error_t |
49 |
|
|
gpg_clip_verify (int is_detached, |
50 |
|
|
const char *det_data, size_t det_len, |
51 |
|
|
gpgme_ctx_t *r_ctx, |
52 |
|
|
gpgme_signature_t *r_sig) |
53 |
|
|
{ |
54 |
|
|
gpgme_error_t err; |
55 |
|
|
gpgme_ctx_t ctx; |
56 |
|
|
gpgme_data_t dat = NULL; |
57 |
|
|
gpgme_data_t out = NULL; |
58 |
|
|
gpgme_data_t sig = NULL; |
59 |
|
|
gpgme_verify_result_t res; |
60 |
|
|
|
61 |
|
|
if (is_detached) { |
62 |
|
|
err = gpgme_data_new_from_mem (&dat, det_data, det_len, 1); |
63 |
|
|
if (err) |
64 |
|
|
return err; |
65 |
|
|
} |
66 |
|
|
|
67 |
|
|
err = gpgme_new (&ctx); |
68 |
|
|
if (err) |
69 |
|
|
goto leave; |
70 |
|
|
err = gpg_data_new_from_clipboard (&sig, 0); |
71 |
|
|
if (err) |
72 |
|
|
goto leave; |
73 |
|
|
err = gpgme_data_new (&out); |
74 |
|
|
if (err) |
75 |
|
|
goto leave; |
76 |
|
|
|
77 |
|
|
err = gpgme_op_verify (ctx, sig, dat, out); |
78 |
|
|
if (err) |
79 |
|
|
goto leave; |
80 |
|
|
res = gpgme_op_verify_result (ctx); |
81 |
|
|
if (!res || !res->signatures) { |
82 |
|
|
err = gpg_error (GPG_ERR_NO_DATA); |
83 |
|
|
goto leave; |
84 |
|
|
} |
85 |
|
|
*r_sig = res->signatures; |
86 |
|
|
*r_ctx = ctx; |
87 |
|
|
|
88 |
|
|
leave: |
89 |
|
|
if (err) { |
90 |
|
|
gpgme_release (ctx); |
91 |
|
|
*r_ctx = NULL; |
92 |
|
|
} |
93 |
|
|
if (dat) |
94 |
|
|
gpgme_data_release (dat); |
95 |
|
|
if (out) |
96 |
|
|
gpgme_data_release (out); |
97 |
|
|
gpgme_data_release (sig); |
98 |
|
|
return err; |
99 |
|
|
} |
100 |
|
|
|
101 |
|
|
|
102 |
twoaday |
225 |
/* Display the policy URL and the notation data of a signature. */ |
103 |
werner |
36 |
static void |
104 |
twoaday |
225 |
show_notation_data (HWND dlg, gpgme_ctx_t ctx) |
105 |
werner |
36 |
{ |
106 |
twoaday |
225 |
gpgme_sig_notation_t nota; |
107 |
werner |
36 |
gpgme_sig_notation_t n; |
108 |
twoaday |
225 |
gpgme_verify_result_t res; |
109 |
werner |
36 |
size_t len=0; |
110 |
|
|
char *p; |
111 |
|
|
|
112 |
twoaday |
225 |
res = gpgme_op_verify_result (ctx); |
113 |
|
|
if (!res || !res->signatures || !res->signatures->notations) |
114 |
|
|
return; |
115 |
|
|
nota = res->signatures->notations; |
116 |
werner |
47 |
for (n=nota; n; n = n->next) { |
117 |
werner |
36 |
if (n->name) |
118 |
|
|
len += strlen (n->name) + 1 + 2; |
119 |
|
|
else |
120 |
|
|
len += strlen ("policy URL") + 1 + 2; |
121 |
|
|
len += strlen (n->value) + 1 + 2; |
122 |
|
|
len += 6; |
123 |
|
|
} |
124 |
|
|
p = (char *)calloc (1, len+64); |
125 |
|
|
if (!p) |
126 |
|
|
BUG (NULL); |
127 |
|
|
strcpy (p, "Notation data:\n"); |
128 |
werner |
47 |
for (n=nota; n; n = n->next) { |
129 |
werner |
36 |
if (!n->name) |
130 |
|
|
strcat (p, "policy url: "); |
131 |
|
|
else { |
132 |
|
|
strcat (p, n->name); |
133 |
|
|
strcat (p, " : "); |
134 |
|
|
} |
135 |
|
|
strcat (p, "\""); |
136 |
|
|
strcat (p, n->value); |
137 |
|
|
strcat (p, "\"\n"); |
138 |
|
|
} |
139 |
|
|
|
140 |
|
|
msg_box (dlg, p, _("Signature Information"), MB_INFO); |
141 |
twoaday |
225 |
safe_free (p); |
142 |
werner |
36 |
} |
143 |
|
|
|
144 |
|
|
|
145 |
twoaday |
225 |
/* Ask the user whether to fetch the missing key from the keyserver. */ |
146 |
|
|
static BOOL |
147 |
|
|
fetch_key_from_keyserver (HWND dlg, gpgme_signature_t sig) |
148 |
|
|
{ |
149 |
|
|
const char *keyid; |
150 |
|
|
const char *kserv; |
151 |
|
|
WORD port; |
152 |
|
|
int id; |
153 |
|
|
|
154 |
|
|
if (!sig->fpr) |
155 |
|
|
return FALSE; |
156 |
|
|
keyid = get_keyid_from_fpr (sig->fpr); |
157 |
|
|
id = log_box (_("Verify"), MB_INFO|MB_YESNO, |
158 |
|
|
_("Signature made %s using %s key ID 0x%s\n" |
159 |
|
|
"Cannot check signature: public key not found\n\n" |
160 |
|
|
"Do you want to try to retrieve the key from the keyserver?"), |
161 |
|
|
strtimestamp (sig->timestamp), |
162 |
|
|
get_key_pubalgo (sig->pubkey_algo), keyid); |
163 |
|
|
if (id == IDNO) { |
164 |
|
|
msg_box (dlg, get_gpg_sigstat (GPGME_SIGSUM_KEY_MISSING), |
165 |
|
|
_("Verify"), MB_WARN); |
166 |
|
|
return FALSE; |
167 |
|
|
} |
168 |
|
|
|
169 |
|
|
kserv = default_keyserver; |
170 |
|
|
port = default_keyserver_port; |
171 |
|
|
|
172 |
|
|
/* XXX: gpgme currently does not include to return the preferred |
173 |
|
|
keyserver that might be stored within the signature. */ |
174 |
|
|
|
175 |
|
|
if (!hkp_recv_key (dlg, kserv, port, keyid+8, 0, 0)) |
176 |
|
|
keycache_update (0, keyid+8); |
177 |
|
|
return TRUE; |
178 |
|
|
} |
179 |
|
|
|
180 |
|
|
|
181 |
werner |
36 |
/* Dialog procedure for the clipboard verification. */ |
182 |
|
|
BOOL CALLBACK |
183 |
|
|
clip_verify_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
184 |
|
|
{ |
185 |
twoaday |
219 |
static verlist_ctrl_t vlv = NULL; |
186 |
twoaday |
225 |
static gpgme_ctx_t c = NULL; |
187 |
twoaday |
219 |
text_input_s *ctx; |
188 |
werner |
36 |
gpgme_error_t err; |
189 |
|
|
gpgme_signature_t sig = NULL, s; |
190 |
|
|
gpg_keycache_t kc = NULL; |
191 |
|
|
const char *det_data=NULL; |
192 |
|
|
int rc = 0, det_len=0; |
193 |
|
|
|
194 |
twoaday |
201 |
switch (msg) { |
195 |
werner |
36 |
case WM_INITDIALOG: |
196 |
|
|
SetWindowText (dlg, _("Verify")); |
197 |
twoaday |
85 |
SetDlgItemText (dlg, IDC_VERIFY_SAVE, _("&Save")); |
198 |
werner |
36 |
kc = keycache_get_ctx (KEYCACHE_PUB); |
199 |
|
|
ctx = (text_input_s *)lparam; |
200 |
|
|
if (ctx) { |
201 |
|
|
det_data = ctx->data; |
202 |
|
|
det_len = ctx->length; |
203 |
|
|
} |
204 |
twoaday |
225 |
err = gpg_clip_verify (ctx && ctx->length > 0, |
205 |
werner |
36 |
det_data, det_len, &c, &sig); |
206 |
|
|
if (err) { |
207 |
|
|
msg_box (dlg, gpgme_strerror (err), _("Verify"), MB_ERR); |
208 |
|
|
if (c) |
209 |
|
|
gpgme_release (c); |
210 |
|
|
EndDialog (dlg, FALSE); |
211 |
|
|
return FALSE; |
212 |
|
|
} |
213 |
|
|
|
214 |
|
|
if (gpgme_err_code (sig->status) == GPG_ERR_NO_PUBKEY) { |
215 |
twoaday |
225 |
if (fetch_key_from_keyserver (dlg, sig)) { |
216 |
werner |
36 |
gpgme_release (c); |
217 |
twoaday |
225 |
EndDialog (dlg, FALSE); |
218 |
|
|
return TRUE; |
219 |
werner |
36 |
} |
220 |
twoaday |
225 |
} |
221 |
twoaday |
41 |
else if (gpgme_err_code (sig->status) == GPG_ERR_BAD_SIGNATURE && |
222 |
|
|
!sig->timestamp) |
223 |
werner |
36 |
; |
224 |
twoaday |
76 |
else if (!sig->timestamp) { |
225 |
werner |
36 |
msg_box (dlg, _("Invalid signature state."), _("Verify"), MB_ERR); |
226 |
|
|
gpgme_release (c); |
227 |
|
|
EndDialog (dlg, FALSE); |
228 |
|
|
return FALSE; |
229 |
|
|
} |
230 |
twoaday |
219 |
verlist_build (&vlv, GetDlgItem (dlg, IDC_VERIFY_SIGLIST), 0); |
231 |
|
|
verlist_set_info_control (vlv, GetDlgItem (dlg, IDC_VERIFY_INFO)); |
232 |
werner |
36 |
|
233 |
|
|
for (s = sig; s; s = s->next) { |
234 |
twoaday |
219 |
rc = verlist_add_sig (vlv, s); |
235 |
werner |
36 |
if (rc) |
236 |
|
|
msg_box (dlg, _("Could not extract key or signature information."), |
237 |
|
|
_("Verify"), MB_ERR); |
238 |
|
|
} |
239 |
twoaday |
219 |
|
240 |
twoaday |
225 |
if (!sig->notations) |
241 |
|
|
ShowWindow (GetDlgItem (dlg, IDC_VERIFY_SIGNOT), SW_HIDE); |
242 |
werner |
36 |
SetForegroundWindow (dlg); |
243 |
|
|
return TRUE; |
244 |
|
|
|
245 |
|
|
case WM_DESTROY: |
246 |
twoaday |
225 |
if (c != NULL) { |
247 |
|
|
gpgme_release (c); |
248 |
|
|
c = NULL; |
249 |
|
|
} |
250 |
|
|
if (vlv != NULL) { |
251 |
twoaday |
219 |
verlist_delete (vlv); |
252 |
|
|
vlv = NULL; |
253 |
werner |
36 |
} |
254 |
|
|
return FALSE; |
255 |
|
|
|
256 |
|
|
case WM_COMMAND: |
257 |
|
|
switch (LOWORD (wparam )) { |
258 |
twoaday |
219 |
case IDCANCEL: |
259 |
|
|
EndDialog (dlg, FALSE); |
260 |
twoaday |
225 |
return TRUE; |
261 |
twoaday |
219 |
|
262 |
werner |
36 |
case IDOK: |
263 |
|
|
EndDialog (dlg, TRUE); |
264 |
twoaday |
225 |
return TRUE; |
265 |
|
|
|
266 |
|
|
case IDC_VERIFY_SIGNOT: |
267 |
|
|
show_notation_data (dlg, c); |
268 |
|
|
return TRUE; |
269 |
werner |
36 |
|
270 |
|
|
case IDC_VERIFY_SAVE: |
271 |
|
|
dialog_box_param (glob_hinst, (LPCTSTR)IDD_WINPT_FILE_SAVE, dlg, |
272 |
twoaday |
73 |
file_save_dlg_proc, 0, _("Save Plaintext"), |
273 |
werner |
36 |
IDS_WINPT_FILE_SAVE); |
274 |
|
|
break; |
275 |
|
|
} |
276 |
|
|
break; |
277 |
|
|
} |
278 |
|
|
|
279 |
|
|
return FALSE; |
280 |
|
|
} |
281 |
|
|
|