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 |
#ifdef HAVE_CONFIG_H |
17 |
#include <config.h> |
18 |
#endif |
19 |
|
20 |
#include <windows.h> |
21 |
#include <time.h> |
22 |
|
23 |
#include "resource.h" |
24 |
#include "wptGPG.h" |
25 |
#include "wptTypes.h" |
26 |
#include "wptCommonCtl.h" |
27 |
#include "wptKeylist.h" |
28 |
#include "wptW32API.h" |
29 |
#include "wptKeyserver.h" |
30 |
#include "wptNLS.h" |
31 |
#include "wptContext.h" /* for passwd_s */ |
32 |
#include "wptDlgs.h" |
33 |
#include "wptErrors.h" |
34 |
#include "wptVersion.h" |
35 |
#include "StringBuffer.h" |
36 |
|
37 |
/* XXX use @out directly and do not use gpg_data_extract to store the plain text. */ |
38 |
|
39 |
|
40 |
/* Verify data from the clipboard. If @is_detached is set, a detached |
41 |
signature is assumed with the data the signature was calculated over |
42 |
in @det_data. The context is returned in @r_ctx and the signature |
43 |
in @r_sig. |
44 |
Return value: 0 on success. */ |
45 |
static gpgme_error_t |
46 |
gpg_clip_verify (int is_detached, |
47 |
const char *det_data, size_t det_len, |
48 |
gpgme_ctx_t *r_ctx, |
49 |
gpgme_signature_t *r_sig) |
50 |
{ |
51 |
gpgme_error_t err; |
52 |
gpgme_ctx_t ctx; |
53 |
gpgme_data_t dat = NULL; |
54 |
gpgme_data_t out = NULL; |
55 |
gpgme_data_t sig = NULL; |
56 |
gpgme_verify_result_t res; |
57 |
|
58 |
if (is_detached) { |
59 |
err = gpgme_data_new_from_mem (&dat, det_data, det_len, 1); |
60 |
if (err) |
61 |
return err; |
62 |
} |
63 |
|
64 |
err = gpgme_new (&ctx); |
65 |
if (err) |
66 |
goto leave; |
67 |
err = gpg_data_new_from_clipboard (&sig, 0); |
68 |
if (err) |
69 |
goto leave; |
70 |
err = gpgme_data_new (&out); |
71 |
if (err) |
72 |
goto leave; |
73 |
err = gpgme_op_verify (ctx, sig, dat, out); |
74 |
if (err) |
75 |
goto leave; |
76 |
res = gpgme_op_verify_result (ctx); |
77 |
if (!res || !res->signatures) { |
78 |
err = gpg_error (GPG_ERR_NO_DATA); |
79 |
goto leave; |
80 |
} |
81 |
*r_sig = res->signatures; |
82 |
*r_ctx = ctx; |
83 |
|
84 |
leave: |
85 |
if (err) { |
86 |
gpgme_release (ctx); |
87 |
*r_ctx = NULL; |
88 |
} |
89 |
if (dat) |
90 |
gpgme_data_release (dat); |
91 |
if (out) |
92 |
gpgme_data_release (out); |
93 |
gpgme_data_release (sig); |
94 |
return err; |
95 |
} |
96 |
|
97 |
|
98 |
/* Display the policy URL and/or the notation data of a signature. */ |
99 |
static void |
100 |
show_notation_data (HWND dlg, gpgme_ctx_t ctx) |
101 |
{ |
102 |
gpgme_sig_notation_t nota; |
103 |
gpgme_sig_notation_t n; |
104 |
gpgme_verify_result_t res; |
105 |
StringBuffer p; |
106 |
|
107 |
res = gpgme_op_verify_result (ctx); |
108 |
if (!res || !res->signatures || !res->signatures->notations) |
109 |
return; |
110 |
nota = res->signatures->notations; |
111 |
|
112 |
p = "Notation data:\n"; |
113 |
for (n=nota; n; n = n->next) { |
114 |
if (!n->name) |
115 |
p = p + "policy url: "; |
116 |
else |
117 |
p = p + n->name + " : " ; |
118 |
p = p + "\"" + n->value + "\"\n"; |
119 |
} |
120 |
|
121 |
msg_box (dlg, p.getBuffer (), _("Signature Information"), MB_INFO); |
122 |
} |
123 |
|
124 |
|
125 |
/* Dialog procedure for the clipboard verification. */ |
126 |
BOOL CALLBACK |
127 |
clip_verify_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
128 |
{ |
129 |
static verlist_ctrl_t vlv = NULL; |
130 |
static gpgme_ctx_t c = NULL; |
131 |
text_input_s *ctx; |
132 |
gpgme_error_t err; |
133 |
gpgme_signature_t sig = NULL, s; |
134 |
gpg_keycache_t kc = NULL; |
135 |
const char *det_data=NULL; |
136 |
int rc = 0, det_len=0; |
137 |
|
138 |
switch (msg) { |
139 |
case WM_INITDIALOG: |
140 |
SetWindowText (dlg, _("Verify")); |
141 |
SetDlgItemText (dlg, IDC_VERIFY_SAVE, _("&Save")); |
142 |
kc = keycache_get_ctx (KEYCACHE_PUB); |
143 |
ctx = (text_input_s *)lparam; |
144 |
if (ctx) { |
145 |
det_data = ctx->data; |
146 |
det_len = ctx->length; |
147 |
} |
148 |
err = gpg_clip_verify (ctx && ctx->length > 0, |
149 |
det_data, det_len, &c, &sig); |
150 |
if (err) { |
151 |
msg_box (dlg, gpgme_strerror (err), _("Verify"), MB_ERR); |
152 |
EndDialog (dlg, FALSE); |
153 |
return FALSE; |
154 |
} |
155 |
|
156 |
if (gpgme_err_code (sig->status) == GPG_ERR_NO_PUBKEY) { |
157 |
gpgme_verify_result_t res; |
158 |
|
159 |
if (fetch_key_from_keyserver (dlg, sig)) { |
160 |
EndDialog (dlg, FALSE); |
161 |
return TRUE; |
162 |
} |
163 |
/* We need to call GPG again to get the real sig status. */ |
164 |
err = gpg_clip_verify (ctx && ctx->length > 0, |
165 |
det_data, det_len, &c, &sig); |
166 |
if (err) { |
167 |
msg_box (dlg, gpgme_strerror (err), _("Verify"), MB_ERR); |
168 |
EndDialog (dlg, FALSE); |
169 |
return TRUE; |
170 |
} |
171 |
/* Refresh the pointer because a new GPG instance has been |
172 |
created for the new verify operation. */ |
173 |
res = gpgme_op_verify_result (c); |
174 |
if (!res || res->signatures == NULL) |
175 |
BUG (0); |
176 |
sig = res->signatures; |
177 |
|
178 |
} |
179 |
|
180 |
if (gpgme_err_code (sig->status) == GPG_ERR_BAD_SIGNATURE && |
181 |
!sig->timestamp) |
182 |
; |
183 |
else if (!sig->timestamp) { |
184 |
msg_box (dlg, _("Invalid signature state."), _("Verify"), MB_ERR); |
185 |
EndDialog (dlg, FALSE); |
186 |
return FALSE; |
187 |
} |
188 |
verlist_build (&vlv, GetDlgItem (dlg, IDC_VERIFY_SIGLIST), 0); |
189 |
verlist_set_info_control (vlv, GetDlgItem (dlg, IDC_VERIFY_INFO)); |
190 |
|
191 |
for (s = sig; s; s = s->next) { |
192 |
rc = verlist_add_sig (vlv, s); |
193 |
if (rc) { |
194 |
msg_box (dlg, _("Error while adding signature information."), |
195 |
_("Verify"), MB_ERR); |
196 |
break; |
197 |
} |
198 |
} |
199 |
|
200 |
if (!sig->notations) |
201 |
ShowWindow (GetDlgItem (dlg, IDC_VERIFY_SIGNOT), SW_HIDE); |
202 |
SetForegroundWindow (dlg); |
203 |
return TRUE; |
204 |
|
205 |
case WM_DESTROY: |
206 |
if (c != NULL) { |
207 |
gpgme_release (c); |
208 |
c = NULL; |
209 |
} |
210 |
if (vlv != NULL) { |
211 |
verlist_delete (vlv); |
212 |
vlv = NULL; |
213 |
} |
214 |
return FALSE; |
215 |
|
216 |
case WM_COMMAND: |
217 |
switch (LOWORD (wparam )) { |
218 |
case IDCANCEL: |
219 |
EndDialog (dlg, FALSE); |
220 |
return TRUE; |
221 |
|
222 |
case IDOK: |
223 |
EndDialog (dlg, TRUE); |
224 |
return TRUE; |
225 |
|
226 |
case IDC_VERIFY_SIGNOT: |
227 |
show_notation_data (dlg, c); |
228 |
return TRUE; |
229 |
|
230 |
case IDC_VERIFY_SAVE: |
231 |
dialog_box_param (glob_hinst, (LPCTSTR)IDD_WINPT_FILE_SAVE, dlg, |
232 |
file_save_dlg_proc, 0, _("Save Plaintext"), |
233 |
IDS_WINPT_FILE_SAVE); |
234 |
break; |
235 |
} |
236 |
break; |
237 |
} |
238 |
|
239 |
return FALSE; |
240 |
} |