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

Contents of /trunk/Src/wptClipDecryptDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 41 - (show annotations)
Fri Oct 28 07:15:26 2005 UTC (19 years, 4 months ago) by twoaday
File size: 5951 byte(s)
A lot of bug fixes. 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 #include <windows.h>
23
24 #include "wptTypes.h"
25 #include "wptW32API.h"
26 #include "wptAgent.h"
27 #include "wptNLS.h"
28 #include "wptGPG.h"
29 #include "wptVersion.h"
30 #include "wptErrors.h"
31 #include "wptCommonCtl.h"
32 #include "wptContext.h"
33 #include "wptDlgs.h"
34 #include "wptKeylist.h"
35 #include "wptFileManager.h"
36 #include "../resource.h"
37
38 bool secret_key_available (gpgme_recipient_t rset);
39
40 /* Return the primary user-ID of the key with the keyid @keyid.
41 Caller must free string. */
42 char*
43 get_key_userid (const char *keyid)
44 {
45 gpgme_key_t key;
46 const char *s;
47 char *p;
48
49 if (get_pubkey (keyid, &key))
50 return m_strdup (_("user ID not found"));
51 s = key->uids->uid;
52 if (!s)
53 s = _("user ID not found");
54 p = new char[strlen (s) + 4 + 8];
55 if (!p)
56 BUG (NULL);
57 sprintf (p, "\n \"%s\"", s);
58 return p;
59 }
60
61
62 /* Decrypt the clipboard contents and on success
63 replace the data with the plaintext.
64 Return value: 0 on success. */
65 gpgme_error_t
66 gpgme_op_clip_decrypt (gpgme_ctx_t ctx)
67 {
68 gpgme_error_t err;
69 gpgme_data_t ciph = NULL;
70 gpgme_data_t plain = NULL;
71
72 err = gpg_data_new_from_clipboard (&ciph, 0);
73 if (err)
74 return err;
75
76 err = gpgme_data_new (&plain);
77 if (err) {
78 gpgme_data_release (ciph);
79 return err;
80 }
81
82 err = gpgme_op_decrypt_verify (ctx, ciph, plain);
83
84 gpg_data_release_and_set_clipboard (plain, 0);
85 gpgme_data_release (ciph);
86 return err;
87 }
88
89
90 /* Convenient function to provide clipboard decryption.
91 @hwnd is the parent window used for showing messsages.
92 Return value: 0 on success. */
93 gpgme_error_t
94 clip_decrypt_dlg (HWND hwnd)
95 {
96 gpgme_error_t err;
97 gpgme_ctx_t ctx = NULL;
98 gpgme_signature_t sig = NULL;
99 gpgme_decrypt_result_t res;
100 gpgme_verify_result_t sigres;
101 passphrase_cb_s pwd;
102 const char *s;
103 int novalid = 0;
104
105 /* allow to verify data generated by 'gpg -a --sign foo' */
106 if (fm_assume_onepass_sig (NULL) == 0) {
107 /* XXX: addtitional steps needed? */
108 }
109
110 err = gpgme_new (&ctx);
111 if (err)
112 BUG (NULL);
113 set_gpg_passphrase_cb (&pwd, ctx, GPG_CMD_DECRYPT, hwnd, _("Decryption"));
114 err = gpgme_op_clip_decrypt (ctx);
115 if (pwd.cancel)
116 goto leave;
117 if (gpgme_err_code (err) == GPG_ERR_BAD_PASSPHRASE)
118 agent_del_cache (pwd.keyid);
119
120 res = gpgme_op_decrypt_result (ctx);
121 if (err && res->recipients && !secret_key_available (res->recipients)) {
122 gpgme_recipient_t r = res->recipients;
123 char *p = get_key_userid (r->keyid+8);
124
125 log_box (_("Decryption"), MB_ERR,
126 _("Encrypted with %s key, ID %s.%s\n"
127 "Decryption failed: secret key not available."),
128 get_key_pubalgo (r->pubkey_algo), r->keyid+8, p);
129 free_if_alloc (p);
130 goto leave;
131 }
132 else if (res->unsupported_algorithm) {
133 log_box (_("Decryption"), MB_ERR, _("Unsupported algorithm: %s"),
134 res->unsupported_algorithm);
135 }
136 else if (err) {
137 int pgp_type;
138 gpg_clip_get_pgptype (&pgp_type);
139 if (gpgme_err_code (err) == GPG_ERR_NO_DATA && (pgp_type & PGP_MESSAGE))
140 msg_box (hwnd, _("Broken OpenPGP message (maybe: quoted printable "
141 "character in armor)."), _("Decryption"), MB_INFO);
142 else
143 msg_box (hwnd, gpgme_strerror (err), _("Decryption"), MB_ERR);
144 goto leave;
145 }
146
147 /* Too paranoid??
148 if (flags & GPGME_OPFLAG_BADMDC) {
149 const char *s;
150 s = _("WARNING: encrypted message has been manipulated!\n"
151 "\n"
152 "Do *NOT* trust any text or data output from this file!\n"
153 "It is likely, the data was corrupted during the transport\n"
154 "but it might be also possible that this is part of an attack.");
155 msg_box (hwnd, s, _("*** IMPORTANT ***"), MB_INFO);
156 }*/
157
158
159 show_msg (GetDesktopWindow (), 1500, _("GnuPG Status: Finished"));
160
161 sigres = gpgme_op_verify_result (ctx);
162 if (sigres && sigres->signatures) {
163 gpgme_key_t key=NULL;
164 const char *keyid;
165 const char *uid;
166
167 sig = sigres->signatures;
168 if (!sig->fpr)
169 BUG (NULL);
170 keyid = strlen (sig->fpr) == 40? sig->fpr+24 : sig->fpr+16;
171
172 get_pubkey (keyid, &key);
173 if (key) {
174 if (key->owner_trust == GPGME_VALIDITY_FULL ||
175 key->owner_trust == GPGME_VALIDITY_ULTIMATE)
176 s = _("Signature Status: Created with a fully trusted key");
177 else if (key->owner_trust == GPGME_VALIDITY_MARGINAL)
178 s = _("Signature Status: Created with a marginal trusted key");
179 else if (key->owner_trust == GPGME_VALIDITY_NEVER) {
180 novalid = 1;
181 s = _("Signature Status: Created with an UNTRUSTED key");
182 }
183 else
184 s = _("Signature Status: Created with an undefined trusted key");
185 }
186 else
187 s = "";
188
189 if (key)
190 uid = key->uids->uid;
191 else
192 uid = _("user ID not found");
193 log_box (_("WinPT Verify"), MB_OK,
194 _("%s\n"
195 "%s\n"
196 "Signature made %s\n"
197 "From \"%s\" using key ID 0x%s"
198 "%s %s"),
199 s, get_gpg_sigstat (sig->summary),
200 strtimestamp (sig->timestamp),
201 uid, keyid+8,
202 novalid? "\nPrimary key fingerprint: " : "",
203 novalid? get_key_fpr (key) : "");
204 }
205
206 leave:
207 release_gpg_passphrase_cb (&pwd);
208 gpgme_release (ctx);
209 return err;
210 }

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26