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

Contents of /trunk/Src/wptClipDecryptDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 256 - (show annotations)
Sat Aug 5 10:31:06 2006 UTC (18 years, 6 months ago) by twoaday
File size: 8431 byte(s)
1.0.0pre3 release.


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

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26