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