1 |
twoaday |
220 |
/* wptClipDecryptDlg.cpp - Clipboard decryption |
2 |
|
|
* Copyright (C) 2000-2006 Timo Schulz |
3 |
werner |
36 |
* Copyright (C) 2005 g10 Code GmbH |
4 |
|
|
* |
5 |
|
|
* This file is part of WinPT. |
6 |
|
|
* |
7 |
|
|
* WinPT is free software; you can redistribute it and/or modify |
8 |
|
|
* it under the terms of the GNU General Public License as published by |
9 |
|
|
* the Free Software Foundation; either version 2 of the License, or |
10 |
|
|
* (at your option) any later version. |
11 |
|
|
* |
12 |
|
|
* WinPT is distributed in the hope that it will be useful, |
13 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
|
|
* GNU General Public License for more details. |
16 |
|
|
*/ |
17 |
werner |
42 |
#ifdef HAVE_CONFIG_H |
18 |
|
|
#include <config.h> |
19 |
|
|
#endif |
20 |
|
|
|
21 |
werner |
36 |
#include <windows.h> |
22 |
twoaday |
231 |
#include <assert.h> |
23 |
|
|
#include <time.h> |
24 |
werner |
36 |
|
25 |
|
|
#include "wptTypes.h" |
26 |
|
|
#include "wptW32API.h" |
27 |
|
|
#include "wptAgent.h" |
28 |
|
|
#include "wptNLS.h" |
29 |
|
|
#include "wptGPG.h" |
30 |
|
|
#include "wptVersion.h" |
31 |
|
|
#include "wptErrors.h" |
32 |
|
|
#include "wptCommonCtl.h" |
33 |
|
|
#include "wptContext.h" |
34 |
|
|
#include "wptDlgs.h" |
35 |
|
|
#include "wptKeylist.h" |
36 |
twoaday |
129 |
#include "wptUTF8.h" |
37 |
werner |
47 |
#include "resource.h" |
38 |
werner |
36 |
|
39 |
twoaday |
217 |
bool is_seckey_available (gpgme_recipient_t rset); |
40 |
twoaday |
220 |
char* get_pka_status (gpgme_signature_t sig); |
41 |
werner |
36 |
|
42 |
twoaday |
220 |
|
43 |
werner |
36 |
/* Return the primary user-ID of the key with the keyid @keyid. |
44 |
|
|
Caller must free string. */ |
45 |
|
|
char* |
46 |
|
|
get_key_userid (const char *keyid) |
47 |
|
|
{ |
48 |
twoaday |
208 |
winpt_key_s key; |
49 |
twoaday |
256 |
const char *fmt, *userid; |
50 |
twoaday |
208 |
char *uid; |
51 |
werner |
36 |
|
52 |
twoaday |
256 |
fmt = "\n \"%s\""; |
53 |
twoaday |
208 |
memset (&key, 0, sizeof (key)); |
54 |
|
|
if (winpt_get_pubkey (keyid, &key)) |
55 |
twoaday |
256 |
userid = (_("user ID not found")); |
56 |
|
|
else |
57 |
|
|
userid = key.ext->uids->uid; |
58 |
|
|
uid = new char[strlen (userid) + strlen (fmt)+ 2]; |
59 |
twoaday |
129 |
if (!uid) |
60 |
werner |
36 |
BUG (NULL); |
61 |
twoaday |
256 |
sprintf (uid, fmt, userid); |
62 |
twoaday |
217 |
winpt_release_pubkey (&key); |
63 |
twoaday |
208 |
return uid; |
64 |
werner |
36 |
} |
65 |
|
|
|
66 |
|
|
|
67 |
|
|
/* Decrypt the clipboard contents and on success |
68 |
|
|
replace the data with the plaintext. |
69 |
|
|
Return value: 0 on success. */ |
70 |
|
|
gpgme_error_t |
71 |
|
|
gpgme_op_clip_decrypt (gpgme_ctx_t ctx) |
72 |
|
|
{ |
73 |
|
|
gpgme_error_t err; |
74 |
|
|
gpgme_data_t ciph = NULL; |
75 |
|
|
gpgme_data_t plain = NULL; |
76 |
|
|
|
77 |
|
|
err = gpg_data_new_from_clipboard (&ciph, 0); |
78 |
|
|
if (err) |
79 |
|
|
return err; |
80 |
|
|
|
81 |
|
|
err = gpgme_data_new (&plain); |
82 |
|
|
if (err) { |
83 |
|
|
gpgme_data_release (ciph); |
84 |
|
|
return err; |
85 |
|
|
} |
86 |
|
|
|
87 |
twoaday |
41 |
err = gpgme_op_decrypt_verify (ctx, ciph, plain); |
88 |
werner |
36 |
|
89 |
|
|
gpg_data_release_and_set_clipboard (plain, 0); |
90 |
|
|
gpgme_data_release (ciph); |
91 |
|
|
return err; |
92 |
|
|
} |
93 |
|
|
|
94 |
|
|
|
95 |
twoaday |
229 |
/* Return humand readable ownertrust description for verification info. */ |
96 |
|
|
const char* |
97 |
|
|
verify_get_key_ownertrust (gpgme_validity_t key_ot, int *novalid) |
98 |
|
|
{ |
99 |
|
|
const char *s; |
100 |
|
|
|
101 |
twoaday |
273 |
if (novalid) |
102 |
|
|
*novalid = 0; /* reset */ |
103 |
twoaday |
229 |
if (key_ot == GPGME_VALIDITY_FULL || |
104 |
|
|
key_ot == GPGME_VALIDITY_ULTIMATE) |
105 |
|
|
s = _("Signature status: created with a fully trusted key"); |
106 |
|
|
else if (key_ot == GPGME_VALIDITY_MARGINAL) |
107 |
|
|
s = _("Signature status: created with a marginal trusted key"); |
108 |
|
|
else if (key_ot == GPGME_VALIDITY_NEVER) { |
109 |
twoaday |
273 |
if (novalid) |
110 |
|
|
*novalid = 1; |
111 |
twoaday |
229 |
s = _("Signature status: created with an UNTRUSTED key"); |
112 |
|
|
} |
113 |
|
|
else |
114 |
|
|
s = _("Signature status: created with an undefined trusted key"); |
115 |
|
|
return s; |
116 |
|
|
} |
117 |
|
|
|
118 |
|
|
|
119 |
|
|
/* Return a signature specific header and footer for the clipboard. */ |
120 |
|
|
void |
121 |
|
|
verify_get_clip_info (gpgme_signature_t sig, char **r_header, char **r_footer) |
122 |
|
|
{ |
123 |
|
|
struct winpt_key_s pk; |
124 |
twoaday |
231 |
const char *head = _("*** PGP SIGNATURE VERIFICATION ***\r\n" |
125 |
|
|
"*** Signature made: %s\r\n" |
126 |
|
|
"*** Signature verfied: %s\r\n" |
127 |
|
|
"*** %s\r\n" |
128 |
|
|
"*** Signature result: %s\r\n" |
129 |
|
|
"*** Signer: %s (0x%s)\r\n" |
130 |
|
|
"*** BEGIN PGP DECRYPTED TEXT ***\r\n"); |
131 |
|
|
const char *foot = _("\r\n*** END PGP DECRYPTED TEXT ***"); |
132 |
twoaday |
273 |
const char *s, *ver, *ot, *uid; |
133 |
twoaday |
256 |
char *p, *made; |
134 |
twoaday |
229 |
|
135 |
twoaday |
273 |
if (winpt_get_pubkey (sig->fpr, &pk)) { |
136 |
|
|
uid = _("user ID not found"); |
137 |
|
|
ot = verify_get_key_ownertrust (GPGME_VALIDITY_UNDEFINED, NULL); |
138 |
|
|
} |
139 |
|
|
else { |
140 |
|
|
uid = pk.ext->uids->uid; |
141 |
|
|
ot = verify_get_key_ownertrust (pk.ctx->owner_trust, NULL); |
142 |
|
|
} |
143 |
|
|
|
144 |
twoaday |
256 |
made = m_strdup (strtimestamp (sig->timestamp)); |
145 |
twoaday |
231 |
ver = strtimestamp (time (NULL)); |
146 |
twoaday |
229 |
s = get_gpg_sigstat (sig->summary); |
147 |
|
|
p = new char[strlen (head) + strlen (s) + strlen (made) + |
148 |
twoaday |
231 |
strlen (sig->fpr) + strlen (ot) + strlen (ver) + |
149 |
twoaday |
273 |
strlen (uid) + 1]; |
150 |
twoaday |
229 |
if (!p) |
151 |
|
|
BUG (0); |
152 |
twoaday |
273 |
sprintf (p, head, made, ver, ot, s, uid, get_keyid_from_fpr (sig->fpr)); |
153 |
twoaday |
229 |
*r_header = p; |
154 |
|
|
*r_footer = m_strdup (foot); |
155 |
twoaday |
256 |
free_if_alloc (made); |
156 |
twoaday |
229 |
} |
157 |
|
|
|
158 |
|
|
|
159 |
twoaday |
220 |
/* Show a human readable description of the given signature @sig. */ |
160 |
|
|
void |
161 |
|
|
verify_show_signature_state (gpgme_signature_t sig) |
162 |
|
|
{ |
163 |
|
|
winpt_key_s key; |
164 |
|
|
const char *keyid, *uid; |
165 |
|
|
const char *s; |
166 |
|
|
char *pka_info = NULL; |
167 |
|
|
int novalid = 0; |
168 |
|
|
|
169 |
twoaday |
231 |
assert (sig->fpr != NULL); |
170 |
twoaday |
220 |
|
171 |
|
|
keyid = get_keyid_from_fpr (sig->fpr); |
172 |
|
|
memset (&key, 0, sizeof (key)); |
173 |
|
|
if (!winpt_get_pubkey (keyid, &key)) { |
174 |
twoaday |
229 |
s = verify_get_key_ownertrust (key.ctx->owner_trust, &novalid); |
175 |
twoaday |
220 |
uid = key.ext->uids->uid; |
176 |
twoaday |
231 |
} |
177 |
twoaday |
220 |
else { |
178 |
|
|
s = ""; |
179 |
|
|
uid = _("user ID not found"); |
180 |
|
|
} |
181 |
|
|
|
182 |
twoaday |
229 |
pka_info = get_pka_status (sig); |
183 |
twoaday |
220 |
log_box (_("Decrypt Verify"), novalid? MB_WARN : MB_OK, |
184 |
|
|
_("%s\n" |
185 |
|
|
"%s\n" |
186 |
|
|
"Signature made: %s\n" |
187 |
|
|
"From \"%s\" using key ID 0x%s" |
188 |
|
|
"%s %s\n%s"), |
189 |
|
|
s, get_gpg_sigstat (sig->summary), |
190 |
|
|
strtimestamp (sig->timestamp), |
191 |
|
|
uid, keyid, |
192 |
|
|
novalid? "\nPrimary key fingerprint: " : "", |
193 |
|
|
novalid? get_key_fpr (key.ctx) : "", |
194 |
|
|
pka_info? pka_info : "" |
195 |
|
|
); |
196 |
|
|
free_if_alloc (pka_info); |
197 |
|
|
winpt_release_pubkey (&key); |
198 |
|
|
} |
199 |
|
|
|
200 |
|
|
|
201 |
werner |
36 |
/* Convenient function to provide clipboard decryption. |
202 |
|
|
@hwnd is the parent window used for showing messsages. |
203 |
|
|
Return value: 0 on success. */ |
204 |
|
|
gpgme_error_t |
205 |
twoaday |
229 |
clip_decrypt_dlg (HWND hwnd, int use_viewer) |
206 |
werner |
36 |
{ |
207 |
|
|
gpgme_error_t err; |
208 |
|
|
gpgme_ctx_t ctx = NULL; |
209 |
|
|
gpgme_decrypt_result_t res; |
210 |
|
|
gpgme_verify_result_t sigres; |
211 |
|
|
passphrase_cb_s pwd; |
212 |
twoaday |
179 |
int pgp_type = 0; |
213 |
werner |
36 |
|
214 |
|
|
/* allow to verify data generated by 'gpg -a --sign foo' */ |
215 |
twoaday |
214 |
if (fm_assume_onepass_sig (NULL) == 1) { |
216 |
|
|
dialog_box_param (glob_hinst, (LPCSTR)IDD_WINPT_VERIFY, hwnd, |
217 |
|
|
clip_verify_dlg_proc, 0, |
218 |
|
|
_("Verify"), IDS_WINPT_VERIFY); |
219 |
|
|
return 0; |
220 |
werner |
36 |
} |
221 |
|
|
|
222 |
|
|
err = gpgme_new (&ctx); |
223 |
|
|
if (err) |
224 |
|
|
BUG (NULL); |
225 |
|
|
set_gpg_passphrase_cb (&pwd, ctx, GPG_CMD_DECRYPT, hwnd, _("Decryption")); |
226 |
twoaday |
179 |
gpg_get_recipients (NULL, &pwd.recipients); |
227 |
|
|
|
228 |
werner |
36 |
err = gpgme_op_clip_decrypt (ctx); |
229 |
|
|
if (pwd.cancel) |
230 |
|
|
goto leave; |
231 |
|
|
if (gpgme_err_code (err) == GPG_ERR_BAD_PASSPHRASE) |
232 |
|
|
agent_del_cache (pwd.keyid); |
233 |
|
|
|
234 |
|
|
res = gpgme_op_decrypt_result (ctx); |
235 |
twoaday |
217 |
if (err && res->recipients && !is_seckey_available (res->recipients)) { |
236 |
werner |
36 |
gpgme_recipient_t r = res->recipients; |
237 |
twoaday |
208 |
char *u = get_key_userid (r->keyid+8); |
238 |
werner |
36 |
log_box (_("Decryption"), MB_ERR, |
239 |
twoaday |
256 |
_("Encrypted with %s key, ID 0x%s.%s\n" |
240 |
werner |
36 |
"Decryption failed: secret key not available."), |
241 |
twoaday |
208 |
get_key_pubalgo (r->pubkey_algo), r->keyid+8, u); |
242 |
|
|
free_if_alloc (u); |
243 |
werner |
36 |
goto leave; |
244 |
|
|
} |
245 |
twoaday |
41 |
else if (res->unsupported_algorithm) { |
246 |
|
|
log_box (_("Decryption"), MB_ERR, _("Unsupported algorithm: %s"), |
247 |
|
|
res->unsupported_algorithm); |
248 |
|
|
} |
249 |
werner |
36 |
else if (err) { |
250 |
|
|
gpg_clip_get_pgptype (&pgp_type); |
251 |
twoaday |
129 |
if (gpgme_err_code (err) == GPG_ERR_NO_DATA && (pgp_type & PGP_MESSAGE)) |
252 |
werner |
36 |
msg_box (hwnd, _("Broken OpenPGP message (maybe: quoted printable " |
253 |
twoaday |
129 |
"character in armor)."), _("Decryption"), MB_INFO); |
254 |
werner |
36 |
else |
255 |
|
|
msg_box (hwnd, gpgme_strerror (err), _("Decryption"), MB_ERR); |
256 |
|
|
goto leave; |
257 |
|
|
} |
258 |
|
|
|
259 |
twoaday |
236 |
#if 0 |
260 |
|
|
if (status_bad_mdc) { /* XXX: Bad MDC */ |
261 |
|
|
const char *s = |
262 |
|
|
_("WARNING: encrypted message has been manipulated!\n" |
263 |
werner |
36 |
"\n" |
264 |
|
|
"Do *NOT* trust any text or data output from this file!\n" |
265 |
twoaday |
248 |
"It is likely that the data was corrupted in transport\n" |
266 |
werner |
36 |
"but it might be also possible that this is part of an attack."); |
267 |
|
|
msg_box (hwnd, s, _("*** IMPORTANT ***"), MB_INFO); |
268 |
twoaday |
179 |
} |
269 |
twoaday |
236 |
#endif |
270 |
werner |
36 |
|
271 |
|
|
show_msg (GetDesktopWindow (), 1500, _("GnuPG Status: Finished")); |
272 |
|
|
|
273 |
|
|
sigres = gpgme_op_verify_result (ctx); |
274 |
twoaday |
229 |
if (sigres && sigres->signatures) { |
275 |
|
|
if (!use_viewer) |
276 |
|
|
verify_show_signature_state (sigres->signatures); |
277 |
|
|
else |
278 |
|
|
DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_CLIPEDIT, |
279 |
|
|
hwnd, clip_edit_dlg_proc, |
280 |
|
|
(LPARAM)sigres->signatures); |
281 |
|
|
} |
282 |
twoaday |
236 |
else if (use_viewer) |
283 |
|
|
DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_CLIPEDIT, |
284 |
|
|
hwnd, clip_edit_dlg_proc, 0); |
285 |
twoaday |
229 |
|
286 |
werner |
36 |
leave: |
287 |
|
|
release_gpg_passphrase_cb (&pwd); |
288 |
|
|
gpgme_release (ctx); |
289 |
|
|
return err; |
290 |
|
|
} |