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

Contents of /trunk/Src/wptClipVerifyDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 271 - (show annotations)
Sun Nov 5 08:57:45 2006 UTC (18 years, 3 months ago) by twoaday
File size: 6784 byte(s)


1 /* wptClipVerifyDlg.cpp - WinPT verify dialog
2 * Copyright (C) 2001-2006 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 static 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 err = gpgme_op_verify (ctx, sig, dat, out);
77 if (err)
78 goto leave;
79 res = gpgme_op_verify_result (ctx);
80 if (!res || !res->signatures) {
81 err = gpg_error (GPG_ERR_NO_DATA);
82 goto leave;
83 }
84 *r_sig = res->signatures;
85 *r_ctx = ctx;
86
87 leave:
88 if (err) {
89 gpgme_release (ctx);
90 *r_ctx = NULL;
91 }
92 if (dat)
93 gpgme_data_release (dat);
94 if (out)
95 gpgme_data_release (out);
96 gpgme_data_release (sig);
97 return err;
98 }
99
100
101 /* Display the policy URL and/or the notation data of a signature. */
102 static void
103 show_notation_data (HWND dlg, gpgme_ctx_t ctx)
104 {
105 gpgme_sig_notation_t nota;
106 gpgme_sig_notation_t n;
107 gpgme_verify_result_t res;
108 size_t len=0;
109 char *p;
110
111 res = gpgme_op_verify_result (ctx);
112 if (!res || !res->signatures || !res->signatures->notations)
113 return;
114 nota = res->signatures->notations;
115 for (n=nota; n; n = n->next) {
116 if (n->name)
117 len += strlen (n->name) + 1 + 2;
118 else
119 len += strlen ("policy URL") + 1 + 2;
120 len += strlen (n->value) + 1 + 2;
121 len += 6;
122 }
123 p = (char *)calloc (1, len+64);
124 if (!p)
125 BUG (NULL);
126 strcpy (p, "Notation data:\n");
127 for (n=nota; n; n = n->next) {
128 if (!n->name)
129 strcat (p, "policy url: ");
130 else {
131 strcat (p, n->name);
132 strcat (p, " : ");
133 }
134 strcat (p, "\"");
135 strcat (p, n->value);
136 strcat (p, "\"\n");
137 }
138
139 msg_box (dlg, p, _("Signature Information"), MB_INFO);
140 safe_free (p);
141 }
142
143
144 /* Dialog procedure for the clipboard verification. */
145 BOOL CALLBACK
146 clip_verify_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
147 {
148 static verlist_ctrl_t vlv = NULL;
149 static gpgme_ctx_t c = NULL;
150 text_input_s *ctx;
151 gpgme_error_t err;
152 gpgme_signature_t sig = NULL, s;
153 gpg_keycache_t kc = NULL;
154 const char *det_data=NULL;
155 int rc = 0, det_len=0;
156
157 switch (msg) {
158 case WM_INITDIALOG:
159 SetWindowText (dlg, _("Verify"));
160 SetDlgItemText (dlg, IDC_VERIFY_SAVE, _("&Save"));
161 kc = keycache_get_ctx (KEYCACHE_PUB);
162 ctx = (text_input_s *)lparam;
163 if (ctx) {
164 det_data = ctx->data;
165 det_len = ctx->length;
166 }
167 err = gpg_clip_verify (ctx && ctx->length > 0,
168 det_data, det_len, &c, &sig);
169 if (err) {
170 msg_box (dlg, gpgme_strerror (err), _("Verify"), MB_ERR);
171 EndDialog (dlg, FALSE);
172 return FALSE;
173 }
174
175 if (gpgme_err_code (sig->status) == GPG_ERR_NO_PUBKEY) {
176 gpgme_verify_result_t res;
177
178 if (fetch_key_from_keyserver (dlg, sig)) {
179 EndDialog (dlg, FALSE);
180 return TRUE;
181 }
182 /* We need to call GPG again to get the real sig status. */
183 err = gpg_clip_verify (ctx && ctx->length > 0,
184 det_data, det_len, &c, &sig);
185 if (err) {
186 msg_box (dlg, gpgme_strerror (err), _("Verify"), MB_ERR);
187 EndDialog (dlg, FALSE);
188 return TRUE;
189 }
190 /* Refresh the pointer because a new GPG instance has been
191 created for the new verify operation. */
192 res = gpgme_op_verify_result (c);
193 if (!res || res->signatures == NULL)
194 BUG (0);
195 sig = res->signatures;
196
197 }
198
199 if (gpgme_err_code (sig->status) == GPG_ERR_BAD_SIGNATURE &&
200 !sig->timestamp)
201 ;
202 else if (!sig->timestamp) {
203 msg_box (dlg, _("Invalid signature state."), _("Verify"), MB_ERR);
204 EndDialog (dlg, FALSE);
205 return FALSE;
206 }
207 verlist_build (&vlv, GetDlgItem (dlg, IDC_VERIFY_SIGLIST), 0);
208 verlist_set_info_control (vlv, GetDlgItem (dlg, IDC_VERIFY_INFO));
209
210 for (s = sig; s; s = s->next) {
211 rc = verlist_add_sig (vlv, s);
212 if (rc) {
213 msg_box (dlg, _("Error while adding signature information."),
214 _("Verify"), MB_ERR);
215 break;
216 }
217 }
218
219 if (!sig->notations)
220 ShowWindow (GetDlgItem (dlg, IDC_VERIFY_SIGNOT), SW_HIDE);
221 SetForegroundWindow (dlg);
222 return TRUE;
223
224 case WM_DESTROY:
225 if (c != NULL) {
226 gpgme_release (c);
227 c = NULL;
228 }
229 if (vlv != NULL) {
230 verlist_delete (vlv);
231 vlv = NULL;
232 }
233 return FALSE;
234
235 case WM_COMMAND:
236 switch (LOWORD (wparam )) {
237 case IDCANCEL:
238 EndDialog (dlg, FALSE);
239 return TRUE;
240
241 case IDOK:
242 EndDialog (dlg, TRUE);
243 return TRUE;
244
245 case IDC_VERIFY_SIGNOT:
246 show_notation_data (dlg, c);
247 return TRUE;
248
249 case IDC_VERIFY_SAVE:
250 dialog_box_param (glob_hinst, (LPCTSTR)IDD_WINPT_FILE_SAVE, dlg,
251 file_save_dlg_proc, 0, _("Save Plaintext"),
252 IDS_WINPT_FILE_SAVE);
253 break;
254 }
255 break;
256 }
257
258 return FALSE;
259 }

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26