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

Contents of /trunk/Src/wptClipDecryptDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 208 - (show annotations)
Mon May 1 12:22:18 2006 UTC (18 years, 10 months ago) by twoaday
File size: 6015 byte(s)
See ChangeLog.


1 /* wptClipDecryptDlg.cpp - Clipboard decrypt dialog
2 * Copyright (C) 2000-2005 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 secret_key_available (gpgme_recipient_t rset);
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 char *uid;
51
52 memset (&key, 0, sizeof (key));
53 if (winpt_get_pubkey (keyid, &key))
54 return m_strdup (_("user ID not found"));
55 uid = new char[strlen (key.ext->uids->uid) + 4 + 8];
56 if (!uid)
57 BUG (NULL);
58 sprintf (uid, "\n \"%s\"", key.ext->uids->uid);
59 return uid;
60 }
61
62
63 /* Decrypt the clipboard contents and on success
64 replace the data with the plaintext.
65 Return value: 0 on success. */
66 gpgme_error_t
67 gpgme_op_clip_decrypt (gpgme_ctx_t ctx)
68 {
69 gpgme_error_t err;
70 gpgme_data_t ciph = NULL;
71 gpgme_data_t plain = NULL;
72
73 err = gpg_data_new_from_clipboard (&ciph, 0);
74 if (err)
75 return err;
76
77 err = gpgme_data_new (&plain);
78 if (err) {
79 gpgme_data_release (ciph);
80 return err;
81 }
82
83 err = gpgme_op_decrypt_verify (ctx, ciph, plain);
84
85 gpg_data_release_and_set_clipboard (plain, 0);
86 gpgme_data_release (ciph);
87 return err;
88 }
89
90
91 /* Convenient function to provide clipboard decryption.
92 @hwnd is the parent window used for showing messsages.
93 Return value: 0 on success. */
94 gpgme_error_t
95 clip_decrypt_dlg (HWND hwnd)
96 {
97 gpgme_error_t err;
98 gpgme_ctx_t ctx = NULL;
99 gpgme_signature_t sig = NULL;
100 gpgme_decrypt_result_t res;
101 gpgme_verify_result_t sigres;
102 passphrase_cb_s pwd;
103 const char *s;
104 const char *uid;
105 int pgp_type = 0;
106 int novalid = 0;
107
108 /* allow to verify data generated by 'gpg -a --sign foo' */
109 if (fm_assume_onepass_sig (NULL) == 0) {
110 /* XXX: addtitional steps needed? */
111 }
112
113 err = gpgme_new (&ctx);
114 if (err)
115 BUG (NULL);
116 set_gpg_passphrase_cb (&pwd, ctx, GPG_CMD_DECRYPT, hwnd, _("Decryption"));
117 gpg_get_recipients (NULL, &pwd.recipients);
118
119 err = gpgme_op_clip_decrypt (ctx);
120 if (pwd.cancel)
121 goto leave;
122 if (gpgme_err_code (err) == GPG_ERR_BAD_PASSPHRASE)
123 agent_del_cache (pwd.keyid);
124
125 res = gpgme_op_decrypt_result (ctx);
126 if (err && res->recipients && !secret_key_available (res->recipients)) {
127 gpgme_recipient_t r = res->recipients;
128 char *u = get_key_userid (r->keyid+8);
129 log_box (_("Decryption"), MB_ERR,
130 _("Encrypted with %s key, ID %s.%s\n"
131 "Decryption failed: secret key not available."),
132 get_key_pubalgo (r->pubkey_algo), r->keyid+8, u);
133 free_if_alloc (u);
134 goto leave;
135 }
136 else if (res->unsupported_algorithm) {
137 log_box (_("Decryption"), MB_ERR, _("Unsupported algorithm: %s"),
138 res->unsupported_algorithm);
139 }
140 else if (err) {
141 gpg_clip_get_pgptype (&pgp_type);
142 if (gpgme_err_code (err) == GPG_ERR_NO_DATA && (pgp_type & PGP_MESSAGE))
143 msg_box (hwnd, _("Broken OpenPGP message (maybe: quoted printable "
144 "character in armor)."), _("Decryption"), MB_INFO);
145 else
146 msg_box (hwnd, gpgme_strerror (err), _("Decryption"), MB_ERR);
147 goto leave;
148 }
149
150 if (0) { /* XXX: Bad MDC */
151 s = _("WARNING: encrypted message has been manipulated!\n"
152 "\n"
153 "Do *NOT* trust any text or data output from this file!\n"
154 "It is likely, the data was corrupted during the transport\n"
155 "but it might be also possible that this is part of an attack.");
156 msg_box (hwnd, s, _("*** IMPORTANT ***"), MB_INFO);
157 }
158
159 show_msg (GetDesktopWindow (), 1500, _("GnuPG Status: Finished"));
160
161 sigres = gpgme_op_verify_result (ctx);
162 if (sigres && sigres->signatures) {
163 winpt_key_s key;
164 const char *keyid;
165
166 sig = sigres->signatures;
167 if (!sig->fpr)
168 BUG (NULL);
169 keyid = get_keyid_from_fpr (sig->fpr);
170 memset (&key, 0, sizeof (key));
171 if (!winpt_get_pubkey (keyid, &key)) {
172 if (key.ctx->owner_trust == GPGME_VALIDITY_FULL ||
173 key.ctx->owner_trust == GPGME_VALIDITY_ULTIMATE)
174 s = _("Signature Status: Created with a fully trusted key");
175 else if (key.ctx->owner_trust == GPGME_VALIDITY_MARGINAL)
176 s = _("Signature Status: Created with a marginal trusted key");
177 else if (key.ctx->owner_trust == GPGME_VALIDITY_NEVER) {
178 novalid = 1;
179 s = _("Signature Status: Created with an UNTRUSTED key");
180 }
181 else
182 s = _("Signature Status: Created with an undefined trusted key");
183 uid = key.ext->uids->uid;
184 }
185 else {
186 s = "";
187 uid = _("user ID not found");
188 }
189 log_box (_("WinPT Verify"), MB_OK,
190 _("%s\n"
191 "%s\n"
192 "Signature made: %s\n"
193 "From \"%s\" using key ID 0x%s"
194 "%s %s"),
195 s, get_gpg_sigstat (sig->summary),
196 strtimestamp (sig->timestamp),
197 uid, keyid,
198 novalid? "\nPrimary key fingerprint: " : "",
199 novalid? get_key_fpr (key.ctx) : "");
200 }
201
202 leave:
203 release_gpg_passphrase_cb (&pwd);
204 gpgme_release (ctx);
205 return err;
206 }

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26