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

Contents of /trunk/Src/wptClipDecryptDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 220 - (show annotations)
Tue May 30 15:31:49 2006 UTC (18 years, 9 months ago) by twoaday
File size: 6638 byte(s)


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 /* Show a human readable description of the given signature @sig. */
97 void
98 verify_show_signature_state (gpgme_signature_t sig)
99 {
100 winpt_key_s key;
101 const char *keyid, *uid;
102 const char *s;
103 char *pka_info = NULL;
104 int novalid = 0;
105
106 if (!sig->fpr)
107 BUG (NULL);
108
109 keyid = get_keyid_from_fpr (sig->fpr);
110 memset (&key, 0, sizeof (key));
111
112 pka_info = get_pka_status (sig);
113
114 if (!winpt_get_pubkey (keyid, &key)) {
115 if (key.ctx->owner_trust == GPGME_VALIDITY_FULL ||
116 key.ctx->owner_trust == GPGME_VALIDITY_ULTIMATE)
117 s = _("Signature Status: Created with a fully trusted key");
118 else if (key.ctx->owner_trust == GPGME_VALIDITY_MARGINAL)
119 s = _("Signature Status: Created with a marginal trusted key");
120 else if (key.ctx->owner_trust == GPGME_VALIDITY_NEVER) {
121 novalid = 1;
122 s = _("Signature Status: Created with an UNTRUSTED key");
123 }
124 else
125 s = _("Signature Status: Created with an undefined trusted key");
126 uid = key.ext->uids->uid;
127 }
128 else {
129 s = "";
130 uid = _("user ID not found");
131 }
132
133 log_box (_("Decrypt Verify"), novalid? MB_WARN : MB_OK,
134 _("%s\n"
135 "%s\n"
136 "Signature made: %s\n"
137 "From \"%s\" using key ID 0x%s"
138 "%s %s\n%s"),
139 s, get_gpg_sigstat (sig->summary),
140 strtimestamp (sig->timestamp),
141 uid, keyid,
142 novalid? "\nPrimary key fingerprint: " : "",
143 novalid? get_key_fpr (key.ctx) : "",
144 pka_info? pka_info : ""
145 );
146 free_if_alloc (pka_info);
147 winpt_release_pubkey (&key);
148 }
149
150
151 /* Convenient function to provide clipboard decryption.
152 @hwnd is the parent window used for showing messsages.
153 Return value: 0 on success. */
154 gpgme_error_t
155 clip_decrypt_dlg (HWND hwnd)
156 {
157 gpgme_error_t err;
158 gpgme_ctx_t ctx = NULL;
159 gpgme_decrypt_result_t res;
160 gpgme_verify_result_t sigres;
161 passphrase_cb_s pwd;
162 const char *s;
163 int pgp_type = 0;
164
165 /* allow to verify data generated by 'gpg -a --sign foo' */
166 if (fm_assume_onepass_sig (NULL) == 1) {
167 dialog_box_param (glob_hinst, (LPCSTR)IDD_WINPT_VERIFY, hwnd,
168 clip_verify_dlg_proc, 0,
169 _("Verify"), IDS_WINPT_VERIFY);
170 return 0;
171 }
172
173 err = gpgme_new (&ctx);
174 if (err)
175 BUG (NULL);
176 set_gpg_passphrase_cb (&pwd, ctx, GPG_CMD_DECRYPT, hwnd, _("Decryption"));
177 gpg_get_recipients (NULL, &pwd.recipients);
178
179 err = gpgme_op_clip_decrypt (ctx);
180 if (pwd.cancel)
181 goto leave;
182 if (gpgme_err_code (err) == GPG_ERR_BAD_PASSPHRASE)
183 agent_del_cache (pwd.keyid);
184
185 res = gpgme_op_decrypt_result (ctx);
186 if (err && res->recipients && !is_seckey_available (res->recipients)) {
187 gpgme_recipient_t r = res->recipients;
188 char *u = get_key_userid (r->keyid+8);
189 log_box (_("Decryption"), MB_ERR,
190 _("Encrypted with %s key, ID %s.%s\n"
191 "Decryption failed: secret key not available."),
192 get_key_pubalgo (r->pubkey_algo), r->keyid+8, u);
193 free_if_alloc (u);
194 goto leave;
195 }
196 else if (res->unsupported_algorithm) {
197 log_box (_("Decryption"), MB_ERR, _("Unsupported algorithm: %s"),
198 res->unsupported_algorithm);
199 }
200 else if (err) {
201 gpg_clip_get_pgptype (&pgp_type);
202 if (gpgme_err_code (err) == GPG_ERR_NO_DATA && (pgp_type & PGP_MESSAGE))
203 msg_box (hwnd, _("Broken OpenPGP message (maybe: quoted printable "
204 "character in armor)."), _("Decryption"), MB_INFO);
205 else
206 msg_box (hwnd, gpgme_strerror (err), _("Decryption"), MB_ERR);
207 goto leave;
208 }
209
210 if (0) { /* XXX: Bad MDC */
211 s = _("WARNING: encrypted message has been manipulated!\n"
212 "\n"
213 "Do *NOT* trust any text or data output from this file!\n"
214 "It is likely, the data was corrupted during the transport\n"
215 "but it might be also possible that this is part of an attack.");
216 msg_box (hwnd, s, _("*** IMPORTANT ***"), MB_INFO);
217 }
218
219 show_msg (GetDesktopWindow (), 1500, _("GnuPG Status: Finished"));
220
221 sigres = gpgme_op_verify_result (ctx);
222 if (sigres && sigres->signatures)
223 verify_show_signature_state (sigres->signatures);
224
225 leave:
226 release_gpg_passphrase_cb (&pwd);
227 gpgme_release (ctx);
228 return err;
229 }

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26