1 |
/* wptClipDecryptDlg.cpp - Clipboard decrypt dialog |
2 |
* Copyright (C) 2000-2004 Timo Schulz |
3 |
* |
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 |
char * |
38 |
get_key_userid (const char * keyid) |
39 |
{ |
40 |
gpgme_key_t key; |
41 |
const char * s; |
42 |
char * p; |
43 |
|
44 |
if( get_pubkey( keyid, &key ) ) |
45 |
return m_strdup( "" ); |
46 |
s = gpgme_key_get_string_attr( key, GPGME_ATTR_USERID, NULL, 0 ); |
47 |
if( !s ) |
48 |
s = _("user ID not found"); |
49 |
p = new char[strlen( s ) + 4 + 8]; |
50 |
if( !p ) |
51 |
BUG( NULL ); |
52 |
sprintf( p, "\n \"%s\"", s ); |
53 |
return p; |
54 |
} /* get_key_userid */ |
55 |
|
56 |
|
57 |
static gpgme_error_t |
58 |
list_recipients (gpgme_ctx_t ctx, gpgme_recipients_t *r_rset) |
59 |
{ |
60 |
gpgme_error_t rc; |
61 |
gpgme_data_t clipdat; |
62 |
|
63 |
rc = gpgme_data_new_from_clipboard (&clipdat); |
64 |
if (!rc) |
65 |
rc = gpgme_op_list_keys (clipdat, NULL, r_rset); |
66 |
gpgme_data_release (clipdat); |
67 |
return rc; |
68 |
} /* list_recipients */ |
69 |
|
70 |
|
71 |
int |
72 |
algo_from_list (gpgme_recipients_t rset, const char * keyid) |
73 |
{ |
74 |
void * ctx = NULL; |
75 |
const char * s; |
76 |
|
77 |
gpgme_recipients_enum_open (rset, &ctx); |
78 |
while ((s = gpgme_recipients_enum_read (rset, &ctx))) { |
79 |
if (!strcmp( s+1, keyid)) |
80 |
return *s; |
81 |
} |
82 |
return 0; |
83 |
} /* algo_from_list */ |
84 |
|
85 |
|
86 |
int |
87 |
clip_decrypt_dlg (HWND hwnd) |
88 |
{ |
89 |
gpgme_error_t rc; |
90 |
gpgme_ctx_t ctx = NULL; |
91 |
gpgme_recipients_t keys = NULL; |
92 |
gpgme_sig_t sig = NULL; |
93 |
gpgme_op_flags_t flags; |
94 |
passphrase_cb_s pwd; |
95 |
const char *created, *s; |
96 |
char keyid[17] = {0}; |
97 |
int novalid = 0; |
98 |
unsigned int pkalgo = 0; |
99 |
u32 t, sigstat; |
100 |
|
101 |
rc = gpgme_new (&ctx); |
102 |
if (rc) |
103 |
BUG (0); |
104 |
gpgme_enable_logging (ctx); |
105 |
|
106 |
/* allow to verify data generated by 'gpg -a --sign foo' */ |
107 |
if (fm_assume_onepass_sig (NULL) == 0) { |
108 |
rc = list_recipients (ctx, &keys); |
109 |
if (rc) { |
110 |
gpgme_show_error (hwnd, rc, ctx, _("Decryption"), MB_ERR); |
111 |
gpgme_release (ctx); |
112 |
return rc; |
113 |
} |
114 |
} |
115 |
|
116 |
set_gpg_passphrase_cb (ctx, &pwd, GPG_CMD_DECRYPT, hwnd, _("Decryption")); |
117 |
pwd.enc_to = keys; |
118 |
rc = gpgme_op_clip_decrypt (ctx); |
119 |
memset (pwd.pwd, 0, sizeof pwd.pwd); |
120 |
if (pwd.cancel) |
121 |
goto leave; |
122 |
|
123 |
if (rc == GPGME_Bad_Passphrase) |
124 |
agent_del_cache (pwd.keyid); |
125 |
gpgme_decrypt_get_status (ctx, keyid, &flags); |
126 |
if (rc == GPGME_No_Seckey && (flags & GPGME_OPFLAG_NOSECKEY)) { |
127 |
char * p = get_key_userid (keyid+8); |
128 |
int pkalgo = algo_from_list (keys, keyid); |
129 |
log_box (_("Decryption"), MB_ERR, |
130 |
_("Encrypted with %s key, ID %s.%s\n" |
131 |
"Decryption failed: secret key not available."), |
132 |
gpgme_key_expand_attr( GPGME_ATTR_ALGO, pkalgo), |
133 |
keyid+8, p); |
134 |
free_if_alloc (p); |
135 |
goto leave; |
136 |
} |
137 |
else if (rc) { |
138 |
gpgme_cliptype_t pgp_type; |
139 |
gpgme_clip_get_pgptype (&pgp_type); |
140 |
if (rc == GPGME_No_Data && (pgp_type & GPGME_CLIP_MESSAGE)) |
141 |
msg_box (hwnd, _("Broken OpenPGP message (maybe: quoted printable character in armor)."), _("Decryption"), MB_INFO); |
142 |
else |
143 |
gpgme_show_error (hwnd, rc, ctx, _("Decryption"), MB_ERR); |
144 |
goto leave; |
145 |
} |
146 |
|
147 |
if (flags & GPGME_OPFLAG_BADMDC) { |
148 |
const char *s; |
149 |
s = _("WARNING: encrypted message has been manipulated!\n" |
150 |
"\n" |
151 |
"Do *NOT* trust any text or data output from this file!\n" |
152 |
"It is likely, the data was corrupted during the transport\n" |
153 |
"but it might be also possible that this is part of an attack."); |
154 |
msg_box (hwnd, s, _("*** IMPORTANT ***"), MB_INFO); |
155 |
} |
156 |
|
157 |
show_msg (hwnd, 1500, _("GnuPG Status: Finished")); |
158 |
gpgme_decrypt_get_sig_ctx (ctx, &sig); |
159 |
sigstat = gpgme_sig_get_ulong_attr (sig, 0, GPGME_ATTR_VALIDITY); |
160 |
if (sig && sigstat != GPGME_SIG_STAT_ERROR) { |
161 |
gpgme_key_t key; |
162 |
const char * val; |
163 |
char keyid[16+1]; |
164 |
|
165 |
val = gpgme_sig_get_string_attr( sig, GPGME_ATTR_KEYID ); |
166 |
if( !val ) |
167 |
val = "DEADBEEFDEADBEEF"; |
168 |
_snprintf( keyid, sizeof(keyid)-1, "%s", val+8 ); |
169 |
sigstat = gpgme_sig_get_ulong_attr( sig, 0, GPGME_ATTR_VALIDITY ); |
170 |
if( get_pubkey( keyid, &key ) ) |
171 |
goto leave; |
172 |
|
173 |
t = gpgme_sig_get_ulong_attr( sig, 0, GPGME_ATTR_OTRUST ); |
174 |
if( t == GPGME_VALIDITY_FULL || t == GPGME_VALIDITY_ULTIMATE ) |
175 |
s = _("Signature Status: Created with a fully trusted key"); |
176 |
else if ( t == GPGME_VALIDITY_MARGINAL ) |
177 |
s = _("Signature Status: Created with a marginal trusted key"); |
178 |
else if ( t == GPGME_VALIDITY_UNKNOWN |
179 |
|| t == GPGME_VALIDITY_UNDEFINED |
180 |
|| t == GPGME_VALIDITY_NEVER ) { |
181 |
novalid = 1; |
182 |
s = _("Signature Status: Created with an UNTRUSTED key"); |
183 |
} |
184 |
else |
185 |
s = _("Signature Status: Created with an invalid key"); |
186 |
t = gpgme_sig_get_ulong_attr( sig, 0, GPGME_ATTR_CREATED ); |
187 |
created = strtimestamp( t ); |
188 |
t = gpgme_key_get_ulong_attr( key, GPGME_ATTR_VALIDITY, NULL, 0 ); |
189 |
val = gpgme_sig_get_string_attr( sig, GPGME_ATTR_USERID ); |
190 |
if( !val ) |
191 |
val = _("Invalid User ID"); |
192 |
log_box( _("WinPT Verify"), MB_OK, |
193 |
_("%s\n" |
194 |
"%s\n" |
195 |
"Signature made %s\n" |
196 |
"From \"%s\" using key ID 0x%s" |
197 |
"%s %s"), |
198 |
s, gpg_sigstat[sigstat % SIGSTAT_MASK], created, |
199 |
val, keyid, |
200 |
novalid? "\nPrimary key fingerprint: " : "", |
201 |
novalid? get_key_fpr( key ) : "" |
202 |
); |
203 |
} |
204 |
|
205 |
leave: |
206 |
gpgme_release( ctx ); |
207 |
gpgme_recipients_release( keys ); |
208 |
gpgme_sig_release( sig ); |
209 |
return rc; |
210 |
} /* clip_decrypt_dlg */ |