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