1 |
twoaday |
2 |
/* wptClipDecryptDlg.cpp - Clipboard decrypt dialog |
2 |
twoaday |
23 |
* Copyright (C) 2000-2005 Timo Schulz |
3 |
twoaday |
2 |
* |
4 |
|
|
* This file is part of WinPT. |
5 |
|
|
* |
6 |
|
|
* WinPT is free software; you can redistribute it and/or modify |
7 |
|
|
* it under the terms of the GNU General Public License as published by |
8 |
|
|
* the Free Software Foundation; either version 2 of the License, or |
9 |
|
|
* (at your option) any later version. |
10 |
|
|
* |
11 |
|
|
* WinPT is distributed in the hope that it will be useful, |
12 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
|
|
* GNU General Public License for more details. |
15 |
|
|
* |
16 |
|
|
* You should have received a copy of the GNU General Public License |
17 |
|
|
* along with WinPT; if not, write to the Free Software Foundation, |
18 |
|
|
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
19 |
|
|
*/ |
20 |
|
|
|
21 |
|
|
#include <windows.h> |
22 |
|
|
|
23 |
|
|
#include "wptTypes.h" |
24 |
|
|
#include "wptW32API.h" |
25 |
|
|
#include "wptAgent.h" |
26 |
|
|
#include "wptNLS.h" |
27 |
|
|
#include "wptGPG.h" |
28 |
|
|
#include "wptVersion.h" |
29 |
|
|
#include "wptErrors.h" |
30 |
|
|
#include "wptCommonCtl.h" |
31 |
|
|
#include "wptContext.h" |
32 |
|
|
#include "wptDlgs.h" |
33 |
|
|
#include "wptKeylist.h" |
34 |
|
|
#include "wptFileManager.h" |
35 |
|
|
#include "../resource.h" |
36 |
|
|
|
37 |
twoaday |
23 |
/* Return the primary user-ID of the key with the keyid @keyid. |
38 |
|
|
Caller must free string. */ |
39 |
|
|
char* |
40 |
|
|
get_key_userid (const char *keyid) |
41 |
twoaday |
2 |
{ |
42 |
|
|
gpgme_key_t key; |
43 |
twoaday |
23 |
const char *s; |
44 |
|
|
char *p; |
45 |
twoaday |
2 |
|
46 |
twoaday |
23 |
if (get_pubkey (keyid, &key)) |
47 |
|
|
return m_strdup (_("user ID not found")); |
48 |
|
|
s = key->uids->uid; |
49 |
|
|
if (!s) |
50 |
twoaday |
2 |
s = _("user ID not found"); |
51 |
twoaday |
23 |
p = new char[strlen (s) + 4 + 8]; |
52 |
|
|
if (!p) |
53 |
|
|
BUG (NULL); |
54 |
|
|
sprintf (p, "\n \"%s\"", s); |
55 |
twoaday |
2 |
return p; |
56 |
twoaday |
23 |
} |
57 |
twoaday |
2 |
|
58 |
|
|
|
59 |
twoaday |
23 |
/* Decrypt the clipboard contents and on success |
60 |
|
|
replace the data with the plaintext. |
61 |
|
|
Return value: 0 on success. */ |
62 |
|
|
gpgme_error_t |
63 |
|
|
gpgme_op_clip_decrypt (gpgme_ctx_t ctx) |
64 |
twoaday |
2 |
{ |
65 |
twoaday |
23 |
gpgme_error_t err; |
66 |
|
|
gpgme_data_t ciph = NULL; |
67 |
|
|
gpgme_data_t plain = NULL; |
68 |
|
|
|
69 |
|
|
err = gpg_data_new_from_clipboard (&ciph, 0); |
70 |
|
|
if (err) |
71 |
|
|
return err; |
72 |
twoaday |
2 |
|
73 |
twoaday |
23 |
err = gpgme_data_new (&plain); |
74 |
|
|
if (err) { |
75 |
|
|
gpgme_data_release (ciph); |
76 |
|
|
return err; |
77 |
|
|
} |
78 |
twoaday |
2 |
|
79 |
twoaday |
23 |
err = gpgme_op_decrypt (ctx, ciph, plain); |
80 |
twoaday |
2 |
|
81 |
twoaday |
23 |
gpg_data_release_and_set_clipboard (plain, 0); |
82 |
|
|
gpgme_data_release (ciph); |
83 |
|
|
return err; |
84 |
|
|
} |
85 |
twoaday |
2 |
|
86 |
|
|
|
87 |
twoaday |
23 |
/* Convenient function to provide clipboard decryption. |
88 |
|
|
@hwnd is the parent window used for showing messsages. |
89 |
|
|
Return value: 0 on success. */ |
90 |
|
|
gpgme_error_t |
91 |
twoaday |
2 |
clip_decrypt_dlg (HWND hwnd) |
92 |
|
|
{ |
93 |
twoaday |
23 |
gpgme_error_t err; |
94 |
twoaday |
2 |
gpgme_ctx_t ctx = NULL; |
95 |
twoaday |
23 |
gpgme_signature_t sig = NULL; |
96 |
|
|
gpgme_decrypt_result_t res; |
97 |
|
|
gpgme_verify_result_t sigres; |
98 |
twoaday |
2 |
passphrase_cb_s pwd; |
99 |
|
|
const char *created, *s; |
100 |
twoaday |
23 |
char keyid[16+1] = {0}; |
101 |
twoaday |
2 |
int novalid = 0; |
102 |
|
|
|
103 |
|
|
/* allow to verify data generated by 'gpg -a --sign foo' */ |
104 |
|
|
if (fm_assume_onepass_sig (NULL) == 0) { |
105 |
twoaday |
23 |
/* XXX: addtitional steps needed? */ |
106 |
twoaday |
2 |
} |
107 |
|
|
|
108 |
twoaday |
23 |
err = gpgme_new (&ctx); |
109 |
|
|
if (err) |
110 |
|
|
BUG (NULL); |
111 |
|
|
set_gpg_passphrase_cb (&pwd, ctx, GPG_CMD_DECRYPT, hwnd, _("Decryption")); |
112 |
|
|
err = gpgme_op_clip_decrypt (ctx); |
113 |
twoaday |
2 |
memset (pwd.pwd, 0, sizeof pwd.pwd); |
114 |
|
|
if (pwd.cancel) |
115 |
|
|
goto leave; |
116 |
twoaday |
23 |
if (err == gpg_error (GPG_ERR_BAD_PASSPHRASE)) |
117 |
|
|
agent_del_cache (pwd.keyid); |
118 |
twoaday |
2 |
|
119 |
twoaday |
23 |
res = gpgme_op_decrypt_result (ctx); |
120 |
|
|
if (err == gpg_error (GPG_ERR_NO_SECKEY) && res->recipients) { |
121 |
|
|
gpgme_recipient_t r = res->recipients; |
122 |
|
|
char *p = get_key_userid (r->keyid+8); |
123 |
|
|
|
124 |
twoaday |
12 |
log_box (_("Decryption"), MB_ERR, |
125 |
twoaday |
2 |
_("Encrypted with %s key, ID %s.%s\n" |
126 |
|
|
"Decryption failed: secret key not available."), |
127 |
twoaday |
23 |
get_key_pubalgo (r->pubkey_algo), r->keyid+8, p); |
128 |
twoaday |
12 |
free_if_alloc (p); |
129 |
twoaday |
2 |
goto leave; |
130 |
|
|
} |
131 |
twoaday |
23 |
else if (err) { |
132 |
twoaday |
19 |
gpgme_pgptype_t pgp_type; |
133 |
twoaday |
2 |
gpgme_clip_get_pgptype (&pgp_type); |
134 |
twoaday |
23 |
if (err == gpg_error (GPG_ERR_NO_DATA) && (pgp_type & PGP_MESSAGE)) |
135 |
|
|
msg_box (hwnd, _("Broken OpenPGP message (maybe: quoted printable " |
136 |
|
|
"character in armor)."), _("Decryption"), MB_INFO); |
137 |
twoaday |
2 |
else |
138 |
twoaday |
23 |
msg_box (hwnd, gpgme_strerror (err), _("Decryption"), MB_ERR); |
139 |
twoaday |
2 |
goto leave; |
140 |
|
|
} |
141 |
|
|
|
142 |
twoaday |
23 |
/* Too paranoid?? |
143 |
twoaday |
12 |
if (flags & GPGME_OPFLAG_BADMDC) { |
144 |
twoaday |
2 |
const char *s; |
145 |
|
|
s = _("WARNING: encrypted message has been manipulated!\n" |
146 |
|
|
"\n" |
147 |
|
|
"Do *NOT* trust any text or data output from this file!\n" |
148 |
|
|
"It is likely, the data was corrupted during the transport\n" |
149 |
|
|
"but it might be also possible that this is part of an attack."); |
150 |
twoaday |
12 |
msg_box (hwnd, s, _("*** IMPORTANT ***"), MB_INFO); |
151 |
twoaday |
23 |
}*/ |
152 |
twoaday |
2 |
|
153 |
twoaday |
23 |
|
154 |
|
|
show_msg (GetDesktopWindow (), 1500, _("GnuPG Status: Finished")); |
155 |
|
|
|
156 |
|
|
sigres = gpgme_op_verify_result (ctx); |
157 |
|
|
if (sigres && sigres->signatures) { |
158 |
twoaday |
2 |
gpgme_key_t key; |
159 |
|
|
const char * val; |
160 |
|
|
char keyid[16+1]; |
161 |
|
|
|
162 |
twoaday |
23 |
sig = sigres->signatures; |
163 |
|
|
if (!sig->fpr) |
164 |
|
|
val = "????????????????"; |
165 |
|
|
else |
166 |
|
|
val = strlen (sig->fpr) == 40? sig->fpr+32 : sig->fpr + 24; |
167 |
|
|
_snprintf (keyid, sizeof (keyid)-1, "%s", val+8); |
168 |
|
|
|
169 |
|
|
get_pubkey (keyid, &key); |
170 |
|
|
if (key->owner_trust == GPGME_VALIDITY_FULL || |
171 |
|
|
key->owner_trust == GPGME_VALIDITY_ULTIMATE) |
172 |
twoaday |
2 |
s = _("Signature Status: Created with a fully trusted key"); |
173 |
twoaday |
23 |
else if (key->owner_trust == GPGME_VALIDITY_MARGINAL) |
174 |
twoaday |
2 |
s = _("Signature Status: Created with a marginal trusted key"); |
175 |
twoaday |
23 |
else if (key->owner_trust == GPGME_VALIDITY_NEVER) { |
176 |
twoaday |
2 |
novalid = 1; |
177 |
|
|
s = _("Signature Status: Created with an UNTRUSTED key"); |
178 |
|
|
} |
179 |
|
|
else |
180 |
twoaday |
23 |
s = _("Signature Status: Created with an undefined trusted key"); |
181 |
|
|
created = strtimestamp (sig->timestamp); |
182 |
|
|
|
183 |
|
|
if (key) |
184 |
|
|
val = key->uids->uid; |
185 |
|
|
else |
186 |
|
|
val = _("user ID not found"); |
187 |
|
|
log_box (_("WinPT Verify"), MB_OK, |
188 |
twoaday |
2 |
_("%s\n" |
189 |
twoaday |
23 |
"%s\n" |
190 |
twoaday |
2 |
"Signature made %s\n" |
191 |
|
|
"From \"%s\" using key ID 0x%s" |
192 |
|
|
"%s %s"), |
193 |
twoaday |
23 |
s, get_gpg_sigstat (sig->summary), created, |
194 |
twoaday |
2 |
val, keyid, |
195 |
|
|
novalid? "\nPrimary key fingerprint: " : "", |
196 |
twoaday |
23 |
novalid? get_key_fpr (key) : ""); |
197 |
twoaday |
2 |
} |
198 |
|
|
|
199 |
|
|
leave: |
200 |
twoaday |
23 |
gpgme_release (ctx); |
201 |
|
|
return err; |
202 |
|
|
} |