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