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

Annotation of /trunk/Src/wptClipDecryptDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 24 - (hide annotations)
Sat Oct 8 10:43:08 2005 UTC (19 years, 4 months ago) by twoaday
File size: 6028 byte(s)
Bug fixes to correct some problems introduced by
the MyGPGME to GPGME port.

1 twoaday 2 /* wptClipDecryptDlg.cpp - Clipboard decrypt dialog
2 twoaday 23 * Copyright (C) 2000-2005 Timo Schulz
3 twoaday 24 * Copyright (C) 2005 g10 Code GmbH
4 twoaday 2 *
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     #include <windows.h>
23    
24     #include "wptTypes.h"
25     #include "wptW32API.h"
26     #include "wptAgent.h"
27     #include "wptNLS.h"
28     #include "wptGPG.h"
29     #include "wptVersion.h"
30     #include "wptErrors.h"
31     #include "wptCommonCtl.h"
32     #include "wptContext.h"
33     #include "wptDlgs.h"
34     #include "wptKeylist.h"
35     #include "wptFileManager.h"
36     #include "../resource.h"
37    
38 twoaday 23 /* Return the primary user-ID of the key with the keyid @keyid.
39     Caller must free string. */
40     char*
41     get_key_userid (const char *keyid)
42 twoaday 2 {
43     gpgme_key_t key;
44 twoaday 23 const char *s;
45     char *p;
46 twoaday 2
47 twoaday 23 if (get_pubkey (keyid, &key))
48     return m_strdup (_("user ID not found"));
49     s = key->uids->uid;
50     if (!s)
51 twoaday 2 s = _("user ID not found");
52 twoaday 23 p = new char[strlen (s) + 4 + 8];
53     if (!p)
54     BUG (NULL);
55     sprintf (p, "\n \"%s\"", s);
56 twoaday 2 return p;
57 twoaday 23 }
58 twoaday 2
59    
60 twoaday 23 /* Decrypt the clipboard contents and on success
61     replace the data with the plaintext.
62     Return value: 0 on success. */
63     gpgme_error_t
64     gpgme_op_clip_decrypt (gpgme_ctx_t ctx)
65 twoaday 2 {
66 twoaday 23 gpgme_error_t err;
67     gpgme_data_t ciph = NULL;
68     gpgme_data_t plain = NULL;
69    
70     err = gpg_data_new_from_clipboard (&ciph, 0);
71     if (err)
72     return err;
73 twoaday 2
74 twoaday 23 err = gpgme_data_new (&plain);
75     if (err) {
76     gpgme_data_release (ciph);
77     return err;
78     }
79 twoaday 2
80 twoaday 23 err = gpgme_op_decrypt (ctx, ciph, plain);
81 twoaday 2
82 twoaday 23 gpg_data_release_and_set_clipboard (plain, 0);
83     gpgme_data_release (ciph);
84     return err;
85     }
86 twoaday 2
87    
88 twoaday 23 /* Convenient function to provide clipboard decryption.
89     @hwnd is the parent window used for showing messsages.
90     Return value: 0 on success. */
91     gpgme_error_t
92 twoaday 2 clip_decrypt_dlg (HWND hwnd)
93     {
94 twoaday 23 gpgme_error_t err;
95 twoaday 2 gpgme_ctx_t ctx = NULL;
96 twoaday 23 gpgme_signature_t sig = NULL;
97     gpgme_decrypt_result_t res;
98     gpgme_verify_result_t sigres;
99 twoaday 2 passphrase_cb_s pwd;
100     const char *created, *s;
101 twoaday 23 char keyid[16+1] = {0};
102 twoaday 2 int novalid = 0;
103    
104     /* allow to verify data generated by 'gpg -a --sign foo' */
105     if (fm_assume_onepass_sig (NULL) == 0) {
106 twoaday 23 /* XXX: addtitional steps needed? */
107 twoaday 2 }
108    
109 twoaday 23 err = gpgme_new (&ctx);
110     if (err)
111     BUG (NULL);
112     set_gpg_passphrase_cb (&pwd, ctx, GPG_CMD_DECRYPT, hwnd, _("Decryption"));
113     err = gpgme_op_clip_decrypt (ctx);
114 twoaday 2 if (pwd.cancel)
115     goto leave;
116 twoaday 24 if (gpgme_err_code (err) == GPG_ERR_BAD_PASSPHRASE)
117 twoaday 23 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 24 release_gpg_passphrase_cb (&pwd);
201 twoaday 23 gpgme_release (ctx);
202     return err;
203     }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26