1 |
/* wptClipDecryptDlg.cpp - Clipboard decryption |
2 |
* Copyright (C) 2000-2007 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 |
#ifdef HAVE_CONFIG_H |
18 |
#include <config.h> |
19 |
#endif |
20 |
|
21 |
#include <windows.h> |
22 |
#include <assert.h> |
23 |
#include <time.h> |
24 |
|
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 |
#include "wptUTF8.h" |
37 |
#include "resource.h" |
38 |
#include "StringBuffer.h" |
39 |
|
40 |
bool is_seckey_available (gpgme_recipient_t rset); |
41 |
char* get_pka_status (gpgme_signature_t sig); |
42 |
|
43 |
|
44 |
/* Return the primary user-ID of the key with the keyid @keyid. |
45 |
Caller must free string. */ |
46 |
char* |
47 |
get_key_userid (const char *keyid) |
48 |
{ |
49 |
winpt_key_s key; |
50 |
StringBuffer p; |
51 |
const char *userid; |
52 |
char *uid; |
53 |
|
54 |
p = "\n "; |
55 |
memset (&key, 0, sizeof (key)); |
56 |
if (winpt_get_pubkey (keyid, &key)) |
57 |
userid = (_("user ID not found")); |
58 |
else |
59 |
userid = key.ext->uids->uid; |
60 |
p = p + "\"" + userid + "\""; |
61 |
|
62 |
winpt_release_pubkey (&key); |
63 |
uid = p.getBufferCopy (); |
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 |
int is_utf8=0; |
78 |
|
79 |
err = gpg_data_utf8_new_from_clipboard (&ciph, 0, &is_utf8); |
80 |
if (err) |
81 |
return err; |
82 |
|
83 |
err = gpgme_data_new (&plain); |
84 |
if (err) { |
85 |
gpgme_data_release (ciph); |
86 |
return err; |
87 |
} |
88 |
|
89 |
err = gpgme_op_decrypt_verify (ctx, ciph, plain); |
90 |
if (is_utf8) |
91 |
gpg_data_release_utf8_to_clipboard (plain); |
92 |
else |
93 |
gpg_data_release_to_clipboard (plain, 0); |
94 |
gpgme_data_release (ciph); |
95 |
return err; |
96 |
} |
97 |
|
98 |
|
99 |
/* Return humand readable ownertrust description for verification info. */ |
100 |
const char* |
101 |
verify_get_key_ownertrust (gpgme_validity_t key_ot, int *novalid) |
102 |
{ |
103 |
const char *s; |
104 |
|
105 |
if (novalid) |
106 |
*novalid = 0; /* reset */ |
107 |
if (key_ot == GPGME_VALIDITY_FULL || |
108 |
key_ot == GPGME_VALIDITY_ULTIMATE) |
109 |
s = _("Signature status: created with a fully trusted key"); |
110 |
else if (key_ot == GPGME_VALIDITY_MARGINAL) |
111 |
s = _("Signature status: created with a marginal trusted key"); |
112 |
else if (key_ot == GPGME_VALIDITY_NEVER) { |
113 |
if (novalid) |
114 |
*novalid = 1; |
115 |
s = _("Signature status: created with an UNTRUSTED key"); |
116 |
} |
117 |
else |
118 |
s = _("Signature status: created with an undefined trusted key"); |
119 |
return s; |
120 |
} |
121 |
|
122 |
|
123 |
/* Return a signature specific header and footer for the clipboard. */ |
124 |
void |
125 |
verify_get_clip_info (gpgme_signature_t sig, char **r_header, char **r_footer) |
126 |
{ |
127 |
struct winpt_key_s pk; |
128 |
const char *head = _("*** PGP SIGNATURE VERIFICATION ***\r\n" |
129 |
"*** Signature made: %s\r\n" |
130 |
"*** Signature verfied: %s\r\n" |
131 |
"*** %s\r\n" |
132 |
"*** Signature result: %s\r\n" |
133 |
"*** Signer: %s (0x%s)\r\n" |
134 |
"*** BEGIN PGP DECRYPTED TEXT ***\r\n"); |
135 |
const char *foot = _("\r\n*** END PGP DECRYPTED TEXT ***"); |
136 |
const char *stat, *ot, *uid, *keyid; |
137 |
char made[128], ver[128]; |
138 |
char *p; |
139 |
|
140 |
if (winpt_get_pubkey (sig->fpr, &pk)) { |
141 |
uid = _("user ID not found"); |
142 |
ot = verify_get_key_ownertrust (GPGME_VALIDITY_UNDEFINED, NULL); |
143 |
} |
144 |
else { |
145 |
uid = pk.ext->uids->uid; |
146 |
ot = verify_get_key_ownertrust (pk.ctx->owner_trust, NULL); |
147 |
} |
148 |
|
149 |
if (!get_locale_timedate (sig->timestamp, made, DIM (made)-1)) |
150 |
_snprintf (made, DIM (made)-1, "'unknown time'"); |
151 |
if (!get_locale_timedate (time (NULL), ver, DIM (ver)-1)) |
152 |
_snprintf (ver, DIM (ver)-1, "'unknown time'"); |
153 |
stat = get_gpg_sigstat (sig->summary); |
154 |
p = new char[strlen (head) + strlen (stat) + strlen (made) + |
155 |
strlen (sig->fpr) + strlen (ot) + strlen (ver) + |
156 |
strlen (uid) + 1]; |
157 |
if (!p) |
158 |
BUG (0); |
159 |
if (strlen (sig->fpr) == 32) { /* RSA:MD5 */ |
160 |
if (pk.ext != NULL) |
161 |
keyid = pk.ext->key->subkeys->keyid+8; |
162 |
else |
163 |
keyid = sig->fpr; /* show the fingerprint in case of problems. */ |
164 |
} |
165 |
else |
166 |
keyid = get_keyid_from_fpr (sig->fpr); |
167 |
sprintf (p, head, made, ver, ot, stat, uid, keyid); |
168 |
*r_header = p; |
169 |
*r_footer = m_strdup (foot); |
170 |
} |
171 |
|
172 |
|
173 |
/* Show a human readable description of the given signature @sig. */ |
174 |
void |
175 |
verify_show_signature_state (gpgme_signature_t sig) |
176 |
{ |
177 |
winpt_key_s key; |
178 |
const char *keyid, *uid; |
179 |
const char *s; |
180 |
char *pka_info = NULL, timebuf[128]; |
181 |
int novalid = 0; |
182 |
|
183 |
assert (sig->fpr != NULL); |
184 |
|
185 |
memset (&key, 0, sizeof (key)); |
186 |
if (!winpt_get_pubkey (sig->fpr, &key)) { |
187 |
s = verify_get_key_ownertrust (key.ctx->owner_trust, &novalid); |
188 |
uid = key.ext->uids->uid; |
189 |
} |
190 |
else { |
191 |
s = ""; |
192 |
uid = _("user ID not found"); |
193 |
} |
194 |
|
195 |
if (!get_locale_timedate (sig->timestamp, timebuf, DIM (timebuf)-1)) |
196 |
_snprintf (timebuf, DIM (timebuf)-1, "'unknown time'"); |
197 |
|
198 |
if (strlen (sig->fpr) == 32) { |
199 |
if (key.ctx != NULL) |
200 |
keyid = key.ctx->subkeys->keyid+8; |
201 |
else |
202 |
keyid = sig->fpr; /* in case of problems show the fingerprint. */ |
203 |
} |
204 |
else |
205 |
keyid = get_keyid_from_fpr (sig->fpr); |
206 |
|
207 |
pka_info = get_pka_status (sig); |
208 |
log_box (_("Decrypt Verify"), novalid? MB_WARN : MB_OK, |
209 |
_("%s\n" |
210 |
"%s\n" |
211 |
"Signature made: %s\n" |
212 |
"From \"%s\" using key ID 0x%s" |
213 |
"%s %s\n%s"), |
214 |
s, get_gpg_sigstat (sig->summary), |
215 |
timebuf, uid, keyid, |
216 |
novalid? "\nPrimary key fingerprint: " : "", |
217 |
novalid? get_key_fpr (key.ctx) : "", |
218 |
pka_info? pka_info : "" |
219 |
); |
220 |
free_if_alloc (pka_info); |
221 |
winpt_release_pubkey (&key); |
222 |
} |
223 |
|
224 |
|
225 |
/* Convenient function to provide clipboard decryption. |
226 |
@hwnd is the parent window used for showing messsages. |
227 |
Return value: 0 on success. */ |
228 |
gpgme_error_t |
229 |
clip_decrypt_dlg (HWND hwnd, int use_viewer) |
230 |
{ |
231 |
gpgme_error_t err; |
232 |
gpgme_ctx_t ctx = NULL; |
233 |
gpgme_decrypt_result_t res; |
234 |
gpgme_verify_result_t sigres; |
235 |
passphrase_cb_s pwd; |
236 |
int pgp_type = 0; |
237 |
|
238 |
/* allow to verify data generated by 'gpg -a --sign foo' */ |
239 |
if (fm_assume_onepass_sig (NULL) == 1) { |
240 |
dialog_box_param (glob_hinst, (LPCSTR)IDD_WINPT_VERIFY, hwnd, |
241 |
clip_verify_dlg_proc, 0, |
242 |
_("Verify"), IDS_WINPT_VERIFY); |
243 |
return 0; |
244 |
} |
245 |
|
246 |
err = gpgme_new (&ctx); |
247 |
if (err) |
248 |
BUG (NULL); |
249 |
set_gpg_passphrase_cb (&pwd, ctx, GPG_CMD_DECRYPT, hwnd, _("Decryption")); |
250 |
gpg_get_recipients (NULL, &pwd.recipients); |
251 |
|
252 |
err = gpgme_op_clip_decrypt (ctx); |
253 |
if (pwd.cancel) |
254 |
goto leave; |
255 |
if (gpgme_err_code (err) == GPG_ERR_BAD_PASSPHRASE) |
256 |
agent_del_cache (pwd.keyid); |
257 |
|
258 |
res = gpgme_op_decrypt_result (ctx); |
259 |
if (err && res->recipients && !is_seckey_available (res->recipients)) { |
260 |
gpgme_recipient_t r = res->recipients; |
261 |
char *u = get_key_userid (r->keyid+8); |
262 |
log_box (_("Decryption"), MB_ERR, |
263 |
_("Encrypted with %s key, ID 0x%s.%s\n" |
264 |
"Decryption failed: secret key not available."), |
265 |
get_key_pubalgo (r->pubkey_algo), r->keyid+8, u); |
266 |
free_if_alloc (u); |
267 |
goto leave; |
268 |
} |
269 |
else if (res->unsupported_algorithm) { |
270 |
log_box (_("Decryption"), MB_ERR, _("Unsupported algorithm: %s"), |
271 |
res->unsupported_algorithm); |
272 |
} |
273 |
else if (err) { |
274 |
gpg_clip_get_pgptype (&pgp_type); |
275 |
if (gpgme_err_code (err) == GPG_ERR_NO_DATA && (pgp_type & PGP_MESSAGE)) |
276 |
msg_box (hwnd, _("Broken OpenPGP message (maybe: quoted printable " |
277 |
"character in armor)."), _("Decryption"), MB_INFO); |
278 |
else |
279 |
msg_box (hwnd, gpgme_strerror (err), _("Decryption"), MB_ERR); |
280 |
goto leave; |
281 |
} |
282 |
|
283 |
show_msg (GetDesktopWindow (), 1500, _("GnuPG Status: Finished")); |
284 |
|
285 |
sigres = gpgme_op_verify_result (ctx); |
286 |
if (sigres && sigres->signatures) { |
287 |
if (!use_viewer) |
288 |
verify_show_signature_state (sigres->signatures); |
289 |
else |
290 |
DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_CLIPEDIT, |
291 |
hwnd, clip_edit_dlg_proc, |
292 |
(LPARAM)sigres->signatures); |
293 |
} |
294 |
else if (use_viewer) |
295 |
DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_CLIPEDIT, |
296 |
hwnd, clip_edit_dlg_proc, 0); |
297 |
|
298 |
leave: |
299 |
release_gpg_passphrase_cb (&pwd); |
300 |
gpgme_release (ctx); |
301 |
return err; |
302 |
} |