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

Annotation of /trunk/Src/wptClipDecryptDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 195 - (hide annotations)
Mon Apr 3 17:10:47 2006 UTC (18 years, 10 months ago) by twoaday
File size: 6128 byte(s)
Prepare new release.


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

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26