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