1 |
/* wptClipDecryptDlg.cpp - Clipboard decrypt dialog |
/* wptClipDecryptDlg.cpp - Clipboard decryption |
2 |
* Copyright (C) 2000-2005 Timo Schulz |
* Copyright (C) 2000-2006 Timo Schulz |
3 |
* Copyright (C) 2005 g10 Code GmbH |
* Copyright (C) 2005 g10 Code GmbH |
4 |
* |
* |
5 |
* This file is part of WinPT. |
* This file is part of WinPT. |
36 |
#include "wptContext.h" |
#include "wptContext.h" |
37 |
#include "wptDlgs.h" |
#include "wptDlgs.h" |
38 |
#include "wptKeylist.h" |
#include "wptKeylist.h" |
|
#include "wptFileManager.h" |
|
39 |
#include "wptUTF8.h" |
#include "wptUTF8.h" |
40 |
#include "resource.h" |
#include "resource.h" |
41 |
|
|
42 |
bool secret_key_available (gpgme_recipient_t rset); |
bool is_seckey_available (gpgme_recipient_t rset); |
43 |
|
char* get_pka_status (gpgme_signature_t sig); |
44 |
|
|
45 |
|
|
46 |
/* Return the primary user-ID of the key with the keyid @keyid. |
/* Return the primary user-ID of the key with the keyid @keyid. |
47 |
Caller must free string. */ |
Caller must free string. */ |
48 |
char* |
char* |
49 |
get_key_userid (const char *keyid) |
get_key_userid (const char *keyid) |
50 |
{ |
{ |
51 |
gpgme_key_t key; |
winpt_key_s key; |
52 |
char *p, *uid; |
const char *fmt; |
53 |
|
char *uid; |
54 |
|
|
55 |
if (get_pubkey (keyid, &key)) |
memset (&key, 0, sizeof (key)); |
56 |
|
if (winpt_get_pubkey (keyid, &key)) |
57 |
return m_strdup (_("user ID not found")); |
return m_strdup (_("user ID not found")); |
58 |
uid = utf8_to_wincp2 (key->uids->uid); |
fmt = "\n \"%s\""; |
59 |
|
uid = new char[strlen (key.ext->uids->uid) + strlen (fmt)+ 2]; |
60 |
if (!uid) |
if (!uid) |
|
uid = strdup (_("user ID not found")); |
|
|
p = new char[strlen (uid) + 4 + 8]; |
|
|
if (!p) |
|
61 |
BUG (NULL); |
BUG (NULL); |
62 |
sprintf (p, "\n \"%s\"", uid); |
sprintf (uid, fmt, key.ext->uids->uid); |
63 |
safe_free (uid); |
winpt_release_pubkey (&key); |
64 |
return p; |
return uid; |
65 |
} |
} |
66 |
|
|
67 |
|
|
93 |
} |
} |
94 |
|
|
95 |
|
|
96 |
|
/* Return humand readable ownertrust description for verification info. */ |
97 |
|
const char* |
98 |
|
verify_get_key_ownertrust (gpgme_validity_t key_ot, int *novalid) |
99 |
|
{ |
100 |
|
const char *s; |
101 |
|
|
102 |
|
if (key_ot == GPGME_VALIDITY_FULL || |
103 |
|
key_ot == GPGME_VALIDITY_ULTIMATE) |
104 |
|
s = _("Signature status: created with a fully trusted key"); |
105 |
|
else if (key_ot == GPGME_VALIDITY_MARGINAL) |
106 |
|
s = _("Signature status: created with a marginal trusted key"); |
107 |
|
else if (key_ot == GPGME_VALIDITY_NEVER) { |
108 |
|
if (novalid) *novalid = 1; |
109 |
|
s = _("Signature status: created with an UNTRUSTED key"); |
110 |
|
} |
111 |
|
else |
112 |
|
s = _("Signature status: created with an undefined trusted key"); |
113 |
|
return s; |
114 |
|
} |
115 |
|
|
116 |
|
|
117 |
|
/* Return a signature specific header and footer for the clipboard. */ |
118 |
|
void |
119 |
|
verify_get_clip_info (gpgme_signature_t sig, char **r_header, char **r_footer) |
120 |
|
{ |
121 |
|
struct winpt_key_s pk; |
122 |
|
const char *head = "***** BEGIN PGP SIGNED TEXT *****\r\n" |
123 |
|
"***** Signature made %s using key ID 0x%s\r\n" |
124 |
|
"***** %s\r\n" |
125 |
|
"***** \"%s\" from %s\r\n"; |
126 |
|
const char *foot = "***** END PGP SIGNED TEXT *****"; |
127 |
|
const char *s, *made, *ot; |
128 |
|
char *p; |
129 |
|
|
130 |
|
if (winpt_get_pubkey (sig->fpr, &pk)) |
131 |
|
BUG (0); |
132 |
|
ot = verify_get_key_ownertrust (pk.ctx->owner_trust, NULL); |
133 |
|
made = strtimestamp (sig->timestamp); |
134 |
|
s = get_gpg_sigstat (sig->summary); |
135 |
|
p = new char[strlen (head) + strlen (s) + strlen (made) + |
136 |
|
strlen (sig->fpr) + strlen (ot) + |
137 |
|
strlen (pk.ext->uids->uid) + 1]; |
138 |
|
if (!p) |
139 |
|
BUG (0); |
140 |
|
sprintf (p, head, made, get_keyid_from_fpr (sig->fpr), ot, s, pk.ext->uids->uid); |
141 |
|
*r_header = p; |
142 |
|
*r_footer = m_strdup (foot); |
143 |
|
} |
144 |
|
|
145 |
|
|
146 |
|
/* Show a human readable description of the given signature @sig. */ |
147 |
|
void |
148 |
|
verify_show_signature_state (gpgme_signature_t sig) |
149 |
|
{ |
150 |
|
winpt_key_s key; |
151 |
|
const char *keyid, *uid; |
152 |
|
const char *s; |
153 |
|
char *pka_info = NULL; |
154 |
|
int novalid = 0; |
155 |
|
|
156 |
|
if (!sig->fpr) |
157 |
|
BUG (NULL); |
158 |
|
|
159 |
|
keyid = get_keyid_from_fpr (sig->fpr); |
160 |
|
memset (&key, 0, sizeof (key)); |
161 |
|
|
162 |
|
if (!winpt_get_pubkey (keyid, &key)) { |
163 |
|
s = verify_get_key_ownertrust (key.ctx->owner_trust, &novalid); |
164 |
|
uid = key.ext->uids->uid; |
165 |
|
} |
166 |
|
else { |
167 |
|
s = ""; |
168 |
|
uid = _("user ID not found"); |
169 |
|
} |
170 |
|
|
171 |
|
pka_info = get_pka_status (sig); |
172 |
|
log_box (_("Decrypt Verify"), novalid? MB_WARN : MB_OK, |
173 |
|
_("%s\n" |
174 |
|
"%s\n" |
175 |
|
"Signature made: %s\n" |
176 |
|
"From \"%s\" using key ID 0x%s" |
177 |
|
"%s %s\n%s"), |
178 |
|
s, get_gpg_sigstat (sig->summary), |
179 |
|
strtimestamp (sig->timestamp), |
180 |
|
uid, keyid, |
181 |
|
novalid? "\nPrimary key fingerprint: " : "", |
182 |
|
novalid? get_key_fpr (key.ctx) : "", |
183 |
|
pka_info? pka_info : "" |
184 |
|
); |
185 |
|
free_if_alloc (pka_info); |
186 |
|
winpt_release_pubkey (&key); |
187 |
|
} |
188 |
|
|
189 |
|
|
190 |
/* Convenient function to provide clipboard decryption. |
/* Convenient function to provide clipboard decryption. |
191 |
@hwnd is the parent window used for showing messsages. |
@hwnd is the parent window used for showing messsages. |
192 |
Return value: 0 on success. */ |
Return value: 0 on success. */ |
193 |
gpgme_error_t |
gpgme_error_t |
194 |
clip_decrypt_dlg (HWND hwnd) |
clip_decrypt_dlg (HWND hwnd, int use_viewer) |
195 |
{ |
{ |
196 |
gpgme_error_t err; |
gpgme_error_t err; |
197 |
gpgme_ctx_t ctx = NULL; |
gpgme_ctx_t ctx = NULL; |
|
gpgme_signature_t sig = NULL; |
|
198 |
gpgme_decrypt_result_t res; |
gpgme_decrypt_result_t res; |
199 |
gpgme_verify_result_t sigres; |
gpgme_verify_result_t sigres; |
200 |
passphrase_cb_s pwd; |
passphrase_cb_s pwd; |
201 |
const char *s; |
const char *s; |
202 |
int novalid = 0; |
int pgp_type = 0; |
203 |
|
|
204 |
/* allow to verify data generated by 'gpg -a --sign foo' */ |
/* allow to verify data generated by 'gpg -a --sign foo' */ |
205 |
if (fm_assume_onepass_sig (NULL) == 0) { |
if (fm_assume_onepass_sig (NULL) == 1) { |
206 |
/* XXX: addtitional steps needed? */ |
dialog_box_param (glob_hinst, (LPCSTR)IDD_WINPT_VERIFY, hwnd, |
207 |
|
clip_verify_dlg_proc, 0, |
208 |
|
_("Verify"), IDS_WINPT_VERIFY); |
209 |
|
return 0; |
210 |
} |
} |
211 |
|
|
212 |
err = gpgme_new (&ctx); |
err = gpgme_new (&ctx); |
213 |
if (err) |
if (err) |
214 |
BUG (NULL); |
BUG (NULL); |
215 |
set_gpg_passphrase_cb (&pwd, ctx, GPG_CMD_DECRYPT, hwnd, _("Decryption")); |
set_gpg_passphrase_cb (&pwd, ctx, GPG_CMD_DECRYPT, hwnd, _("Decryption")); |
216 |
|
gpg_get_recipients (NULL, &pwd.recipients); |
217 |
|
|
218 |
err = gpgme_op_clip_decrypt (ctx); |
err = gpgme_op_clip_decrypt (ctx); |
219 |
if (pwd.cancel) |
if (pwd.cancel) |
220 |
goto leave; |
goto leave; |
222 |
agent_del_cache (pwd.keyid); |
agent_del_cache (pwd.keyid); |
223 |
|
|
224 |
res = gpgme_op_decrypt_result (ctx); |
res = gpgme_op_decrypt_result (ctx); |
225 |
if (err && res->recipients && !secret_key_available (res->recipients)) { |
if (err && res->recipients && !is_seckey_available (res->recipients)) { |
226 |
gpgme_recipient_t r = res->recipients; |
gpgme_recipient_t r = res->recipients; |
227 |
char *p = get_key_userid (r->keyid+8); |
char *u = get_key_userid (r->keyid+8); |
|
|
|
228 |
log_box (_("Decryption"), MB_ERR, |
log_box (_("Decryption"), MB_ERR, |
229 |
_("Encrypted with %s key, ID %s.%s\n" |
_("Encrypted with %s key, ID %s.%s\n" |
230 |
"Decryption failed: secret key not available."), |
"Decryption failed: secret key not available."), |
231 |
get_key_pubalgo (r->pubkey_algo), r->keyid+8, p); |
get_key_pubalgo (r->pubkey_algo), r->keyid+8, u); |
232 |
free_if_alloc (p); |
free_if_alloc (u); |
233 |
goto leave; |
goto leave; |
234 |
} |
} |
235 |
else if (res->unsupported_algorithm) { |
else if (res->unsupported_algorithm) { |
237 |
res->unsupported_algorithm); |
res->unsupported_algorithm); |
238 |
} |
} |
239 |
else if (err) { |
else if (err) { |
|
int pgp_type; |
|
240 |
gpg_clip_get_pgptype (&pgp_type); |
gpg_clip_get_pgptype (&pgp_type); |
241 |
if (gpgme_err_code (err) == GPG_ERR_NO_DATA && (pgp_type & PGP_MESSAGE)) |
if (gpgme_err_code (err) == GPG_ERR_NO_DATA && (pgp_type & PGP_MESSAGE)) |
242 |
msg_box (hwnd, _("Broken OpenPGP message (maybe: quoted printable " |
msg_box (hwnd, _("Broken OpenPGP message (maybe: quoted printable " |
246 |
goto leave; |
goto leave; |
247 |
} |
} |
248 |
|
|
249 |
/* Too paranoid?? |
if (0) { /* XXX: Bad MDC */ |
|
if (flags & GPGME_OPFLAG_BADMDC) { |
|
|
const char *s; |
|
250 |
s = _("WARNING: encrypted message has been manipulated!\n" |
s = _("WARNING: encrypted message has been manipulated!\n" |
251 |
"\n" |
"\n" |
252 |
"Do *NOT* trust any text or data output from this file!\n" |
"Do *NOT* trust any text or data output from this file!\n" |
253 |
"It is likely, the data was corrupted during the transport\n" |
"It is likely, the data was corrupted during the transport\n" |
254 |
"but it might be also possible that this is part of an attack."); |
"but it might be also possible that this is part of an attack."); |
255 |
msg_box (hwnd, s, _("*** IMPORTANT ***"), MB_INFO); |
msg_box (hwnd, s, _("*** IMPORTANT ***"), MB_INFO); |
256 |
}*/ |
} |
|
|
|
257 |
|
|
258 |
show_msg (GetDesktopWindow (), 1500, _("GnuPG Status: Finished")); |
show_msg (GetDesktopWindow (), 1500, _("GnuPG Status: Finished")); |
259 |
|
|
260 |
sigres = gpgme_op_verify_result (ctx); |
sigres = gpgme_op_verify_result (ctx); |
261 |
if (sigres && sigres->signatures) { |
if (sigres && sigres->signatures) { |
262 |
gpgme_key_t key=NULL; |
if (!use_viewer) |
263 |
const char *keyid; |
verify_show_signature_state (sigres->signatures); |
|
char *uid; |
|
|
|
|
|
sig = sigres->signatures; |
|
|
if (!sig->fpr) |
|
|
BUG (NULL); |
|
|
if (strlen (sig->fpr) > 16) |
|
|
keyid = strlen (sig->fpr) == 40? sig->fpr+24 : sig->fpr+16; |
|
|
else |
|
|
keyid = sig->fpr; |
|
|
|
|
|
get_pubkey (keyid, &key); |
|
|
if (key) { |
|
|
if (key->owner_trust == GPGME_VALIDITY_FULL || |
|
|
key->owner_trust == GPGME_VALIDITY_ULTIMATE) |
|
|
s = _("Signature Status: Created with a fully trusted key"); |
|
|
else if (key->owner_trust == GPGME_VALIDITY_MARGINAL) |
|
|
s = _("Signature Status: Created with a marginal trusted key"); |
|
|
else if (key->owner_trust == GPGME_VALIDITY_NEVER) { |
|
|
novalid = 1; |
|
|
s = _("Signature Status: Created with an UNTRUSTED key"); |
|
|
} |
|
|
else |
|
|
s = _("Signature Status: Created with an undefined trusted key"); |
|
|
} |
|
264 |
else |
else |
265 |
s = ""; |
DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_CLIPEDIT, |
266 |
|
hwnd, clip_edit_dlg_proc, |
267 |
if (key) |
(LPARAM)sigres->signatures); |
|
uid = utf8_to_wincp2 (key->uids->uid); |
|
|
else |
|
|
uid = strdup (_("user ID not found")); |
|
|
log_box (_("WinPT Verify"), MB_OK, |
|
|
_("%s\n" |
|
|
"%s\n" |
|
|
"Signature made: %s\n" |
|
|
"From \"%s\" using key ID 0x%s" |
|
|
"%s %s"), |
|
|
s, get_gpg_sigstat (sig->summary), |
|
|
strtimestamp (sig->timestamp), |
|
|
uid, keyid+8, |
|
|
novalid? "\nPrimary key fingerprint: " : "", |
|
|
novalid? get_key_fpr (key) : ""); |
|
|
safe_free (uid); |
|
268 |
} |
} |
269 |
|
|
270 |
leave: |
leave: |
271 |
release_gpg_passphrase_cb (&pwd); |
release_gpg_passphrase_cb (&pwd); |
272 |
gpgme_release (ctx); |
gpgme_release (ctx); |