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

Annotation of /trunk/Src/wptClipDecryptDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 328 - (hide annotations)
Fri Sep 25 16:07:38 2009 UTC (15 years, 5 months ago) by twoaday
File size: 8739 byte(s)


1 twoaday 220 /* wptClipDecryptDlg.cpp - Clipboard decryption
2 twoaday 286 * Copyright (C) 2000-2007 Timo Schulz
3 werner 36 * 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 werner 42 #ifdef HAVE_CONFIG_H
18     #include <config.h>
19     #endif
20    
21 werner 36 #include <windows.h>
22 twoaday 231 #include <assert.h>
23     #include <time.h>
24 werner 36
25     #include "wptTypes.h"
26     #include "wptW32API.h"
27     #include "wptAgent.h"
28     #include "wptNLS.h"
29     #include "wptGPG.h"
30     #include "wptVersion.h"
31     #include "wptErrors.h"
32     #include "wptCommonCtl.h"
33     #include "wptContext.h"
34     #include "wptDlgs.h"
35     #include "wptKeylist.h"
36 twoaday 129 #include "wptUTF8.h"
37 werner 47 #include "resource.h"
38 twoaday 278 #include "StringBuffer.h"
39 werner 36
40 twoaday 217 bool is_seckey_available (gpgme_recipient_t rset);
41 twoaday 220 char* get_pka_status (gpgme_signature_t sig);
42 werner 36
43 twoaday 220
44 werner 36 /* Return the primary user-ID of the key with the keyid @keyid.
45     Caller must free string. */
46     char*
47     get_key_userid (const char *keyid)
48     {
49 twoaday 208 winpt_key_s key;
50 twoaday 278 StringBuffer p;
51     const char *userid;
52 twoaday 208 char *uid;
53 werner 36
54 twoaday 278 p = "\n ";
55 twoaday 208 memset (&key, 0, sizeof (key));
56     if (winpt_get_pubkey (keyid, &key))
57 twoaday 256 userid = (_("user ID not found"));
58     else
59     userid = key.ext->uids->uid;
60 twoaday 278 p = p + "\"" + userid + "\"";
61    
62 twoaday 217 winpt_release_pubkey (&key);
63 twoaday 278 uid = p.getBufferCopy ();
64 twoaday 208 return uid;
65 werner 36 }
66    
67    
68     /* Decrypt the clipboard contents and on success
69     replace the data with the plaintext.
70     Return value: 0 on success. */
71     gpgme_error_t
72     gpgme_op_clip_decrypt (gpgme_ctx_t ctx)
73     {
74     gpgme_error_t err;
75     gpgme_data_t ciph = NULL;
76     gpgme_data_t plain = NULL;
77 twoaday 328 int is_utf8=0;
78 werner 36
79 twoaday 328 err = gpg_data_utf8_new_from_clipboard (&ciph, 0, &is_utf8);
80 werner 36 if (err)
81     return err;
82    
83     err = gpgme_data_new (&plain);
84     if (err) {
85     gpgme_data_release (ciph);
86     return err;
87     }
88    
89 twoaday 41 err = gpgme_op_decrypt_verify (ctx, ciph, plain);
90 twoaday 328 if (is_utf8)
91     gpg_data_release_utf8_to_clipboard (plain);
92     else
93     gpg_data_release_to_clipboard (plain, 0);
94 werner 36 gpgme_data_release (ciph);
95     return err;
96     }
97    
98    
99 twoaday 229 /* Return humand readable ownertrust description for verification info. */
100     const char*
101     verify_get_key_ownertrust (gpgme_validity_t key_ot, int *novalid)
102     {
103     const char *s;
104    
105 twoaday 273 if (novalid)
106     *novalid = 0; /* reset */
107 twoaday 229 if (key_ot == GPGME_VALIDITY_FULL ||
108     key_ot == GPGME_VALIDITY_ULTIMATE)
109     s = _("Signature status: created with a fully trusted key");
110     else if (key_ot == GPGME_VALIDITY_MARGINAL)
111     s = _("Signature status: created with a marginal trusted key");
112     else if (key_ot == GPGME_VALIDITY_NEVER) {
113 twoaday 273 if (novalid)
114     *novalid = 1;
115 twoaday 229 s = _("Signature status: created with an UNTRUSTED key");
116     }
117     else
118     s = _("Signature status: created with an undefined trusted key");
119     return s;
120     }
121    
122    
123     /* Return a signature specific header and footer for the clipboard. */
124     void
125     verify_get_clip_info (gpgme_signature_t sig, char **r_header, char **r_footer)
126     {
127     struct winpt_key_s pk;
128 twoaday 231 const char *head = _("*** PGP SIGNATURE VERIFICATION ***\r\n"
129     "*** Signature made: %s\r\n"
130     "*** Signature verfied: %s\r\n"
131     "*** %s\r\n"
132     "*** Signature result: %s\r\n"
133     "*** Signer: %s (0x%s)\r\n"
134     "*** BEGIN PGP DECRYPTED TEXT ***\r\n");
135     const char *foot = _("\r\n*** END PGP DECRYPTED TEXT ***");
136 twoaday 286 const char *stat, *ot, *uid, *keyid;
137 twoaday 278 char made[128], ver[128];
138     char *p;
139 twoaday 229
140 twoaday 273 if (winpt_get_pubkey (sig->fpr, &pk)) {
141     uid = _("user ID not found");
142     ot = verify_get_key_ownertrust (GPGME_VALIDITY_UNDEFINED, NULL);
143     }
144     else {
145     uid = pk.ext->uids->uid;
146     ot = verify_get_key_ownertrust (pk.ctx->owner_trust, NULL);
147     }
148    
149 twoaday 278 if (!get_locale_timedate (sig->timestamp, made, DIM (made)-1))
150     _snprintf (made, DIM (made)-1, "'unknown time'");
151     if (!get_locale_timedate (time (NULL), ver, DIM (ver)-1))
152     _snprintf (ver, DIM (ver)-1, "'unknown time'");
153     stat = get_gpg_sigstat (sig->summary);
154     p = new char[strlen (head) + strlen (stat) + strlen (made) +
155 twoaday 231 strlen (sig->fpr) + strlen (ot) + strlen (ver) +
156 twoaday 273 strlen (uid) + 1];
157 twoaday 229 if (!p)
158     BUG (0);
159 twoaday 286 if (strlen (sig->fpr) == 32) { /* RSA:MD5 */
160     if (pk.ext != NULL)
161     keyid = pk.ext->key->subkeys->keyid+8;
162     else
163     keyid = sig->fpr; /* show the fingerprint in case of problems. */
164     }
165     else
166     keyid = get_keyid_from_fpr (sig->fpr);
167     sprintf (p, head, made, ver, ot, stat, uid, keyid);
168 twoaday 229 *r_header = p;
169     *r_footer = m_strdup (foot);
170     }
171    
172    
173 twoaday 220 /* Show a human readable description of the given signature @sig. */
174     void
175     verify_show_signature_state (gpgme_signature_t sig)
176     {
177     winpt_key_s key;
178     const char *keyid, *uid;
179     const char *s;
180 twoaday 278 char *pka_info = NULL, timebuf[128];
181 twoaday 220 int novalid = 0;
182    
183 twoaday 231 assert (sig->fpr != NULL);
184 twoaday 220
185     memset (&key, 0, sizeof (key));
186 twoaday 286 if (!winpt_get_pubkey (sig->fpr, &key)) {
187 twoaday 229 s = verify_get_key_ownertrust (key.ctx->owner_trust, &novalid);
188 twoaday 220 uid = key.ext->uids->uid;
189 twoaday 231 }
190 twoaday 220 else {
191     s = "";
192     uid = _("user ID not found");
193     }
194    
195 twoaday 278 if (!get_locale_timedate (sig->timestamp, timebuf, DIM (timebuf)-1))
196     _snprintf (timebuf, DIM (timebuf)-1, "'unknown time'");
197    
198 twoaday 286 if (strlen (sig->fpr) == 32) {
199     if (key.ctx != NULL)
200     keyid = key.ctx->subkeys->keyid+8;
201     else
202     keyid = sig->fpr; /* in case of problems show the fingerprint. */
203     }
204     else
205     keyid = get_keyid_from_fpr (sig->fpr);
206    
207 twoaday 229 pka_info = get_pka_status (sig);
208 twoaday 220 log_box (_("Decrypt Verify"), novalid? MB_WARN : MB_OK,
209     _("%s\n"
210     "%s\n"
211     "Signature made: %s\n"
212     "From \"%s\" using key ID 0x%s"
213     "%s %s\n%s"),
214     s, get_gpg_sigstat (sig->summary),
215 twoaday 278 timebuf, uid, keyid,
216 twoaday 220 novalid? "\nPrimary key fingerprint: " : "",
217     novalid? get_key_fpr (key.ctx) : "",
218     pka_info? pka_info : ""
219     );
220     free_if_alloc (pka_info);
221     winpt_release_pubkey (&key);
222     }
223    
224    
225 werner 36 /* Convenient function to provide clipboard decryption.
226     @hwnd is the parent window used for showing messsages.
227     Return value: 0 on success. */
228     gpgme_error_t
229 twoaday 229 clip_decrypt_dlg (HWND hwnd, int use_viewer)
230 werner 36 {
231     gpgme_error_t err;
232     gpgme_ctx_t ctx = NULL;
233     gpgme_decrypt_result_t res;
234     gpgme_verify_result_t sigres;
235     passphrase_cb_s pwd;
236 twoaday 179 int pgp_type = 0;
237 werner 36
238     /* allow to verify data generated by 'gpg -a --sign foo' */
239 twoaday 214 if (fm_assume_onepass_sig (NULL) == 1) {
240     dialog_box_param (glob_hinst, (LPCSTR)IDD_WINPT_VERIFY, hwnd,
241     clip_verify_dlg_proc, 0,
242     _("Verify"), IDS_WINPT_VERIFY);
243     return 0;
244 werner 36 }
245    
246     err = gpgme_new (&ctx);
247     if (err)
248     BUG (NULL);
249     set_gpg_passphrase_cb (&pwd, ctx, GPG_CMD_DECRYPT, hwnd, _("Decryption"));
250 twoaday 179 gpg_get_recipients (NULL, &pwd.recipients);
251    
252 werner 36 err = gpgme_op_clip_decrypt (ctx);
253     if (pwd.cancel)
254     goto leave;
255     if (gpgme_err_code (err) == GPG_ERR_BAD_PASSPHRASE)
256     agent_del_cache (pwd.keyid);
257    
258     res = gpgme_op_decrypt_result (ctx);
259 twoaday 217 if (err && res->recipients && !is_seckey_available (res->recipients)) {
260 werner 36 gpgme_recipient_t r = res->recipients;
261 twoaday 208 char *u = get_key_userid (r->keyid+8);
262 werner 36 log_box (_("Decryption"), MB_ERR,
263 twoaday 256 _("Encrypted with %s key, ID 0x%s.%s\n"
264 werner 36 "Decryption failed: secret key not available."),
265 twoaday 208 get_key_pubalgo (r->pubkey_algo), r->keyid+8, u);
266     free_if_alloc (u);
267 werner 36 goto leave;
268     }
269 twoaday 41 else if (res->unsupported_algorithm) {
270     log_box (_("Decryption"), MB_ERR, _("Unsupported algorithm: %s"),
271     res->unsupported_algorithm);
272     }
273 werner 36 else if (err) {
274     gpg_clip_get_pgptype (&pgp_type);
275 twoaday 129 if (gpgme_err_code (err) == GPG_ERR_NO_DATA && (pgp_type & PGP_MESSAGE))
276 werner 36 msg_box (hwnd, _("Broken OpenPGP message (maybe: quoted printable "
277 twoaday 129 "character in armor)."), _("Decryption"), MB_INFO);
278 werner 36 else
279     msg_box (hwnd, gpgme_strerror (err), _("Decryption"), MB_ERR);
280     goto leave;
281     }
282    
283     show_msg (GetDesktopWindow (), 1500, _("GnuPG Status: Finished"));
284    
285     sigres = gpgme_op_verify_result (ctx);
286 twoaday 229 if (sigres && sigres->signatures) {
287     if (!use_viewer)
288     verify_show_signature_state (sigres->signatures);
289     else
290     DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_CLIPEDIT,
291     hwnd, clip_edit_dlg_proc,
292     (LPARAM)sigres->signatures);
293     }
294 twoaday 236 else if (use_viewer)
295     DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_CLIPEDIT,
296     hwnd, clip_edit_dlg_proc, 0);
297 twoaday 229
298 werner 36 leave:
299     release_gpg_passphrase_cb (&pwd);
300     gpgme_release (ctx);
301     return err;
302     }

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26