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

Contents of /trunk/Src/wptClipDecryptDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 23 - (show annotations)
Fri Sep 30 10:10:16 2005 UTC (19 years, 5 months ago) by twoaday
File size: 5988 byte(s)
Almost finished phase 1 of the WinPT GPGME port.
Still need more cleanup, comments and tests.


1 /* wptClipDecryptDlg.cpp - Clipboard decrypt dialog
2 * Copyright (C) 2000-2005 Timo Schulz
3 *
4 * This file is part of WinPT.
5 *
6 * WinPT is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * WinPT is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with WinPT; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19 */
20
21 #include <windows.h>
22
23 #include "wptTypes.h"
24 #include "wptW32API.h"
25 #include "wptAgent.h"
26 #include "wptNLS.h"
27 #include "wptGPG.h"
28 #include "wptVersion.h"
29 #include "wptErrors.h"
30 #include "wptCommonCtl.h"
31 #include "wptContext.h"
32 #include "wptDlgs.h"
33 #include "wptKeylist.h"
34 #include "wptFileManager.h"
35 #include "../resource.h"
36
37 /* Return the primary user-ID of the key with the keyid @keyid.
38 Caller must free string. */
39 char*
40 get_key_userid (const char *keyid)
41 {
42 gpgme_key_t key;
43 const char *s;
44 char *p;
45
46 if (get_pubkey (keyid, &key))
47 return m_strdup (_("user ID not found"));
48 s = key->uids->uid;
49 if (!s)
50 s = _("user ID not found");
51 p = new char[strlen (s) + 4 + 8];
52 if (!p)
53 BUG (NULL);
54 sprintf (p, "\n \"%s\"", s);
55 return p;
56 }
57
58
59 /* Decrypt the clipboard contents and on success
60 replace the data with the plaintext.
61 Return value: 0 on success. */
62 gpgme_error_t
63 gpgme_op_clip_decrypt (gpgme_ctx_t ctx)
64 {
65 gpgme_error_t err;
66 gpgme_data_t ciph = NULL;
67 gpgme_data_t plain = NULL;
68
69 err = gpg_data_new_from_clipboard (&ciph, 0);
70 if (err)
71 return err;
72
73 err = gpgme_data_new (&plain);
74 if (err) {
75 gpgme_data_release (ciph);
76 return err;
77 }
78
79 err = gpgme_op_decrypt (ctx, ciph, plain);
80
81 gpg_data_release_and_set_clipboard (plain, 0);
82 gpgme_data_release (ciph);
83 return err;
84 }
85
86
87 /* Convenient function to provide clipboard decryption.
88 @hwnd is the parent window used for showing messsages.
89 Return value: 0 on success. */
90 gpgme_error_t
91 clip_decrypt_dlg (HWND hwnd)
92 {
93 gpgme_error_t err;
94 gpgme_ctx_t ctx = NULL;
95 gpgme_signature_t sig = NULL;
96 gpgme_decrypt_result_t res;
97 gpgme_verify_result_t sigres;
98 passphrase_cb_s pwd;
99 const char *created, *s;
100 char keyid[16+1] = {0};
101 int novalid = 0;
102
103 /* allow to verify data generated by 'gpg -a --sign foo' */
104 if (fm_assume_onepass_sig (NULL) == 0) {
105 /* XXX: addtitional steps needed? */
106 }
107
108 err = gpgme_new (&ctx);
109 if (err)
110 BUG (NULL);
111 set_gpg_passphrase_cb (&pwd, ctx, GPG_CMD_DECRYPT, hwnd, _("Decryption"));
112 err = gpgme_op_clip_decrypt (ctx);
113 memset (pwd.pwd, 0, sizeof pwd.pwd);
114 if (pwd.cancel)
115 goto leave;
116 if (err == gpg_error (GPG_ERR_BAD_PASSPHRASE))
117 agent_del_cache (pwd.keyid);
118
119 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 log_box (_("Decryption"), MB_ERR,
125 _("Encrypted with %s key, ID %s.%s\n"
126 "Decryption failed: secret key not available."),
127 get_key_pubalgo (r->pubkey_algo), r->keyid+8, p);
128 free_if_alloc (p);
129 goto leave;
130 }
131 else if (err) {
132 gpgme_pgptype_t pgp_type;
133 gpgme_clip_get_pgptype (&pgp_type);
134 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 else
138 msg_box (hwnd, gpgme_strerror (err), _("Decryption"), MB_ERR);
139 goto leave;
140 }
141
142 /* Too paranoid??
143 if (flags & GPGME_OPFLAG_BADMDC) {
144 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 msg_box (hwnd, s, _("*** IMPORTANT ***"), MB_INFO);
151 }*/
152
153
154 show_msg (GetDesktopWindow (), 1500, _("GnuPG Status: Finished"));
155
156 sigres = gpgme_op_verify_result (ctx);
157 if (sigres && sigres->signatures) {
158 gpgme_key_t key;
159 const char * val;
160 char keyid[16+1];
161
162 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 s = _("Signature Status: Created with a fully trusted key");
173 else if (key->owner_trust == GPGME_VALIDITY_MARGINAL)
174 s = _("Signature Status: Created with a marginal trusted key");
175 else if (key->owner_trust == GPGME_VALIDITY_NEVER) {
176 novalid = 1;
177 s = _("Signature Status: Created with an UNTRUSTED key");
178 }
179 else
180 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 _("%s\n"
189 "%s\n"
190 "Signature made %s\n"
191 "From \"%s\" using key ID 0x%s"
192 "%s %s"),
193 s, get_gpg_sigstat (sig->summary), created,
194 val, keyid,
195 novalid? "\nPrimary key fingerprint: " : "",
196 novalid? get_key_fpr (key) : "");
197 }
198
199 leave:
200 gpgme_release (ctx);
201 return err;
202 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26