/[winpt]/trunk/Src/wptClipVerifyDlg.cpp
ViewVC logotype

Contents of /trunk/Src/wptClipVerifyDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 201 - (show annotations)
Sat Apr 22 18:30:24 2006 UTC (18 years, 10 months ago) by twoaday
File size: 8177 byte(s)
See ChangeLog.


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 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include <windows.h>
25 #include <time.h>
26
27 #include "resource.h"
28 #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 /* Display the policy URL and the notation data of a signature.
103 If @not is NULL, it is assumed there is no data.
104 @dlg is the handle to the calling dialog. */
105 static void
106 show_notation_data (HWND dlg, gpgme_sig_notation_t nota)
107 {
108 gpgme_sig_notation_t n;
109 size_t len=0;
110 char *p;
111
112 for (n=nota; n; n = n->next) {
113 if (n->name)
114 len += strlen (n->name) + 1 + 2;
115 else
116 len += strlen ("policy URL") + 1 + 2;
117 len += strlen (n->value) + 1 + 2;
118 len += 6;
119 }
120 p = (char *)calloc (1, len+64);
121 if (!p)
122 BUG (NULL);
123 strcpy (p, "Notation data:\n");
124 for (n=nota; n; n = n->next) {
125 if (!n->name)
126 strcat (p, "policy url: ");
127 else {
128 strcat (p, n->name);
129 strcat (p, " : ");
130 }
131 strcat (p, "\"");
132 strcat (p, n->value);
133 strcat (p, "\"\n");
134 }
135
136 msg_box (dlg, p, _("Signature Information"), MB_INFO);
137 free (p);
138 }
139
140
141 /* Return human printable PKA status.
142 If no pka information is available, return NULL. */
143 char*
144 get_pka_status (gpgme_signature_t sig)
145 {
146 const char *fmt;
147 char *pka_inf;
148
149 if (sig->pka_trust == 0 || !sig->pka_address)
150 return NULL;
151 fmt = _("PKA: Verified signer's address is '%s'");
152 pka_inf = new char[strlen (fmt)+strlen (sig->pka_address)+2];
153 if (!pka_inf)
154 BUG (NULL);
155 sprintf (pka_inf, fmt, sig->pka_address);
156 return pka_inf;
157 }
158
159
160 /* Dialog procedure for the clipboard verification. */
161 BOOL CALLBACK
162 clip_verify_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
163 {
164 static listview_ctrl_t lv = NULL;
165 static text_input_s *ctx = NULL;
166 gpgme_error_t err;
167 gpgme_signature_t sig = NULL, s;
168 gpg_keycache_t kc = NULL;
169 gpgme_ctx_t c=NULL;
170 const char *det_data=NULL;
171 WORD port = HKP_PORT;
172 int rc = 0, det_len=0;
173
174 switch (msg) {
175 case WM_INITDIALOG:
176 SetWindowText (dlg, _("Verify"));
177 SetDlgItemText (dlg, IDC_VERIFY_SAVE, _("&Save"));
178 kc = keycache_get_ctx (KEYCACHE_PUB);
179 if (!kc)
180 BUG (NULL);
181 ctx = (text_input_s *)lparam;
182 if (ctx) {
183 det_data = ctx->data;
184 det_len = ctx->length;
185 }
186 err = gpg_clip_verify (ctx && ctx->length > 0,
187 det_data, det_len, &c, &sig);
188 if (err) {
189 msg_box (dlg, gpgme_strerror (err), _("Verify"), MB_ERR);
190 if (c)
191 gpgme_release (c);
192 EndDialog (dlg, FALSE);
193 return FALSE;
194 }
195
196 if (gpgme_err_code (sig->status) == GPG_ERR_NO_PUBKEY) {
197 const char *keyid;
198 const char *kserv;
199 if (!sig->fpr)
200 BUG (NULL);
201 if (strlen (sig->fpr) == 40)
202 keyid = sig->fpr+24;
203 else
204 keyid = sig->fpr+16;
205 rc = log_box (_("Verify"), MB_INFO|MB_YESNO,
206 _("Signature made %s using %s key ID 0x%s\n"
207 "Cannot check signature: public key not found\n\n"
208 "Do you want to try to retrieve the key from the keyserver?"),
209 strtimestamp (sig->timestamp),
210 get_key_pubalgo (sig->pubkey_algo), keyid+8);
211 if (rc == IDNO) {
212 msg_box (dlg, get_gpg_sigstat (GPGME_SIGSUM_KEY_MISSING), _("Verify"), MB_WARN);
213 gpgme_release (c);
214 EndDialog (dlg, FALSE);
215 return FALSE;
216 }
217 if (0) {
218 /* FIXME: does GPGME include the keyserver status
219 kserv = gpgme_sig_get_string_attr (sig, GPGME_ATTR_KEYSERVER);
220 if (kserv && strncmp (kserv, "hkp://", 6)) {
221 rc = log_box (_("Verify"), MB_INFO|MB_YESNO,
222 _("The users preferred keyserver is '%s'.\n"
223 "Do you want to use it to fetch the key?"), kserv);
224 if (rc == IDNO) {
225 kserv = default_keyserver;
226 port = default_keyserver_port;
227 }*/
228 }
229 else {
230 kserv = default_keyserver;
231 port = default_keyserver_port;
232 }
233 if (!hkp_recv_key (dlg, kserv, port, keyid+8, 0, 0)) {
234 keycache_update (0, keyid+8);
235 kc = keycache_get_ctx (KEYCACHE_PUB);
236 if (!kc)
237 BUG (dlg);
238 }
239 }
240 else if (gpgme_err_code (sig->status) == GPG_ERR_BAD_SIGNATURE &&
241 !sig->timestamp)
242 ;
243 else if (!sig->timestamp) {
244 msg_box (dlg, _("Invalid signature state."), _("Verify"), MB_ERR);
245 gpgme_release (c);
246 EndDialog (dlg, FALSE);
247 return FALSE;
248 }
249 verlist_build (&lv, GetDlgItem (dlg, IDC_VERIFY_SIGLIST), 0);
250
251 for (s = sig; s; s = s->next) {
252 rc = verlist_add_sig (lv, s);
253 if (rc)
254 msg_box (dlg, _("Could not extract key or signature information."),
255 _("Verify"), MB_ERR);
256 }
257 if (sig->exp_timestamp > (DWORD)time (NULL))
258 SetDlgItemText (dlg, IDC_VERIFY_INFO, _("The signature is expired!"));
259 else {
260 char *pka_info = get_pka_status (sig);
261 if (pka_info != NULL) {
262 SetDlgItemText (dlg, IDC_VERIFY_INFO, pka_info);
263 free_if_alloc (pka_info);
264 }
265 }
266 if (sig->notations)
267 show_notation_data (dlg, sig->notations);
268 gpgme_release (c);
269 SetForegroundWindow (dlg);
270 return TRUE;
271
272 case WM_DESTROY:
273 if (lv) {
274 listview_release (lv);
275 lv = NULL;
276 }
277 return FALSE;
278
279 case WM_SYSCOMMAND:
280 if (LOWORD (wparam) == SC_CLOSE)
281 EndDialog (dlg, TRUE);
282 return FALSE;
283
284 case WM_COMMAND:
285 switch (LOWORD (wparam )) {
286 case IDOK:
287 EndDialog (dlg, TRUE);
288 return FALSE;
289
290 case IDC_VERIFY_SAVE:
291 dialog_box_param (glob_hinst, (LPCTSTR)IDD_WINPT_FILE_SAVE, dlg,
292 file_save_dlg_proc, 0, _("Save Plaintext"),
293 IDS_WINPT_FILE_SAVE);
294 break;
295 }
296 break;
297 }
298
299 return FALSE;
300 }
301

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26