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

Annotation of /trunk/Src/wptClipDecryptDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 47 - (hide annotations)
Mon Oct 31 14:04:59 2005 UTC (19 years, 4 months ago) by werner
File size: 6074 byte(s)
Minor changes; compiles now but gettext is still missing.

1 werner 36 /* wptClipDecryptDlg.cpp - Clipboard decrypt dialog
2     * Copyright (C) 2000-2005 Timo Schulz
3     * 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     * 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 werner 42 #ifdef HAVE_CONFIG_H
23     #include <config.h>
24     #endif
25    
26 werner 36 #include <windows.h>
27 werner 42 #include <windows.h>
28 werner 36
29     #include "wptTypes.h"
30     #include "wptW32API.h"
31     #include "wptAgent.h"
32     #include "wptNLS.h"
33     #include "wptGPG.h"
34     #include "wptVersion.h"
35     #include "wptErrors.h"
36     #include "wptCommonCtl.h"
37     #include "wptContext.h"
38     #include "wptDlgs.h"
39     #include "wptKeylist.h"
40     #include "wptFileManager.h"
41 werner 47 #include "wptCrypto.h"
42     #include "resource.h"
43 werner 36
44     bool secret_key_available (gpgme_recipient_t rset);
45    
46     /* Return the primary user-ID of the key with the keyid @keyid.
47     Caller must free string. */
48     char*
49     get_key_userid (const char *keyid)
50     {
51     gpgme_key_t key;
52     const char *s;
53     char *p;
54    
55     if (get_pubkey (keyid, &key))
56     return m_strdup (_("user ID not found"));
57     s = key->uids->uid;
58     if (!s)
59     s = _("user ID not found");
60     p = new char[strlen (s) + 4 + 8];
61     if (!p)
62     BUG (NULL);
63     sprintf (p, "\n \"%s\"", s);
64     return p;
65     }
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    
78     err = gpg_data_new_from_clipboard (&ciph, 0);
79     if (err)
80     return err;
81    
82     err = gpgme_data_new (&plain);
83     if (err) {
84     gpgme_data_release (ciph);
85     return err;
86     }
87    
88 twoaday 41 err = gpgme_op_decrypt_verify (ctx, ciph, plain);
89 werner 36
90     gpg_data_release_and_set_clipboard (plain, 0);
91     gpgme_data_release (ciph);
92     return err;
93     }
94    
95    
96     /* Convenient function to provide clipboard decryption.
97     @hwnd is the parent window used for showing messsages.
98     Return value: 0 on success. */
99     gpgme_error_t
100     clip_decrypt_dlg (HWND hwnd)
101     {
102     gpgme_error_t err;
103     gpgme_ctx_t ctx = NULL;
104     gpgme_signature_t sig = NULL;
105     gpgme_decrypt_result_t res;
106     gpgme_verify_result_t sigres;
107     passphrase_cb_s pwd;
108 twoaday 41 const char *s;
109 werner 36 int novalid = 0;
110    
111     /* allow to verify data generated by 'gpg -a --sign foo' */
112     if (fm_assume_onepass_sig (NULL) == 0) {
113     /* XXX: addtitional steps needed? */
114     }
115    
116     err = gpgme_new (&ctx);
117     if (err)
118     BUG (NULL);
119     set_gpg_passphrase_cb (&pwd, ctx, GPG_CMD_DECRYPT, hwnd, _("Decryption"));
120     err = gpgme_op_clip_decrypt (ctx);
121     if (pwd.cancel)
122     goto leave;
123     if (gpgme_err_code (err) == GPG_ERR_BAD_PASSPHRASE)
124     agent_del_cache (pwd.keyid);
125    
126     res = gpgme_op_decrypt_result (ctx);
127     if (err && res->recipients && !secret_key_available (res->recipients)) {
128     gpgme_recipient_t r = res->recipients;
129     char *p = get_key_userid (r->keyid+8);
130    
131     log_box (_("Decryption"), MB_ERR,
132     _("Encrypted with %s key, ID %s.%s\n"
133     "Decryption failed: secret key not available."),
134     get_key_pubalgo (r->pubkey_algo), r->keyid+8, p);
135     free_if_alloc (p);
136     goto leave;
137     }
138 twoaday 41 else if (res->unsupported_algorithm) {
139     log_box (_("Decryption"), MB_ERR, _("Unsupported algorithm: %s"),
140     res->unsupported_algorithm);
141     }
142 werner 36 else if (err) {
143     int pgp_type;
144     gpg_clip_get_pgptype (&pgp_type);
145 werner 47 if (gpgme_err_code (err) == GPG_ERR_NO_DATA
146     && (pgp_type & PGP_MESSAGE))
147 werner 36 msg_box (hwnd, _("Broken OpenPGP message (maybe: quoted printable "
148 werner 47 "character in armor)."),
149     _("Decryption"), MB_INFO);
150 werner 36 else
151     msg_box (hwnd, gpgme_strerror (err), _("Decryption"), MB_ERR);
152     goto leave;
153     }
154    
155     /* Too paranoid??
156     if (flags & GPGME_OPFLAG_BADMDC) {
157     const char *s;
158     s = _("WARNING: encrypted message has been manipulated!\n"
159     "\n"
160     "Do *NOT* trust any text or data output from this file!\n"
161     "It is likely, the data was corrupted during the transport\n"
162     "but it might be also possible that this is part of an attack.");
163     msg_box (hwnd, s, _("*** IMPORTANT ***"), MB_INFO);
164     }*/
165    
166    
167     show_msg (GetDesktopWindow (), 1500, _("GnuPG Status: Finished"));
168    
169     sigres = gpgme_op_verify_result (ctx);
170     if (sigres && sigres->signatures) {
171 twoaday 41 gpgme_key_t key=NULL;
172     const char *keyid;
173     const char *uid;
174 werner 36
175     sig = sigres->signatures;
176     if (!sig->fpr)
177 twoaday 41 BUG (NULL);
178     keyid = strlen (sig->fpr) == 40? sig->fpr+24 : sig->fpr+16;
179 werner 36
180     get_pubkey (keyid, &key);
181 twoaday 41 if (key) {
182     if (key->owner_trust == GPGME_VALIDITY_FULL ||
183     key->owner_trust == GPGME_VALIDITY_ULTIMATE)
184     s = _("Signature Status: Created with a fully trusted key");
185     else if (key->owner_trust == GPGME_VALIDITY_MARGINAL)
186     s = _("Signature Status: Created with a marginal trusted key");
187     else if (key->owner_trust == GPGME_VALIDITY_NEVER) {
188     novalid = 1;
189     s = _("Signature Status: Created with an UNTRUSTED key");
190     }
191     else
192     s = _("Signature Status: Created with an undefined trusted key");
193 werner 36 }
194     else
195 twoaday 41 s = "";
196 werner 36
197     if (key)
198 twoaday 41 uid = key->uids->uid;
199 werner 36 else
200 twoaday 41 uid = _("user ID not found");
201 werner 36 log_box (_("WinPT Verify"), MB_OK,
202     _("%s\n"
203     "%s\n"
204     "Signature made %s\n"
205     "From \"%s\" using key ID 0x%s"
206     "%s %s"),
207 twoaday 41 s, get_gpg_sigstat (sig->summary),
208     strtimestamp (sig->timestamp),
209     uid, keyid+8,
210 werner 36 novalid? "\nPrimary key fingerprint: " : "",
211     novalid? get_key_fpr (key) : "");
212     }
213    
214     leave:
215     release_gpg_passphrase_cb (&pwd);
216     gpgme_release (ctx);
217     return err;
218     }

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26