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

Contents of /trunk/Src/wptClipSignDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 451 - (show annotations)
Sat May 5 14:06:10 2012 UTC (12 years, 9 months ago) by twoaday
File size: 7123 byte(s)
2012-05-05  Timo Schulz  <twoaday@gmx.net>

        * wptListview.cpp (keylist_listview_notify): Cleanups.
	* wptClipSignDlg.cpp (clip_sign_dlg_proc): Fixed a race condition
        that could cause a segfault.
        * wptClipSignEncDlg.cpp (clip_signenc_dlg_proc): Likewise.
        * wptClipEncryptDlg.cpp (clip_encrypt_dlg_proc): Likewise and
        enabled the 'redraw' code again.        
	

1 /* wptClipSignDlg.cpp - WinPT clipboard sign dialog
2 * Copyright (C) 2000-2006, 2009 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
18 #ifdef HAVE_CONFIG_H
19 #include <config.h>
20 #endif
21
22 #include <windows.h>
23 #include <commctrl.h>
24
25 #include "resource.h"
26 #include "wptTypes.h"
27 #include "wptAgent.h"
28 #include "wptNLS.h"
29 #include "wptCrypto.h"
30 #include "wptGPG.h"
31 #include "wptCommonCtl.h"
32 #include "wptRegistry.h"
33 #include "wptKeylist.h"
34 #include "wptErrors.h"
35 #include "wptW32API.h"
36 #include "wptVersion.h"
37 #include "wptContext.h" /* for passphrase_s */
38
39
40 /* Sign the clipboard contents with the key @keyid and wrap
41 text lines to @wraplen (0 disable line wrapping).
42 Return value: 0 on success. */
43 gpgme_error_t
44 gpg_clip_sign (gpgme_ctx_t ctx, const char *keyid, int wraplen)
45 {
46 gpgme_error_t err;
47 gpgme_data_t plain = NULL;
48 gpgme_data_t sig = NULL;
49 gpgme_key_t key = NULL;
50
51 if (!keyid)
52 return gpg_error (GPG_ERR_INV_ARG);
53
54 gpgme_set_armor (ctx, 1);
55 err = gpg_data_utf8_new_from_clipboard (&plain, wraplen, NULL);
56 if (err)
57 return err;
58 err = get_pubkey (keyid, &key);
59 if (err)
60 goto leave;
61 err = gpgme_signers_add (ctx, key);
62 if (err)
63 goto leave;
64 err = gpgme_data_new (&sig);
65 if (err)
66 goto leave;
67 err = gpgme_op_sign (ctx, plain, sig, GPGME_SIG_MODE_CLEAR);
68 if (err)
69 goto leave;
70
71 gpg_data_release_to_clipboard (sig, 1);
72 sig = NULL;
73
74 leave:
75 if (plain)
76 gpgme_data_release (plain);
77 if (sig)
78 gpgme_data_release (sig);
79 return err;
80 }
81
82
83 /* This function is used when only one secret key is available.
84 it doesn't make sense to offer a dialog for this case. */
85 void
86 one_key_proc (HWND dlg)
87 {
88 gpgme_ctx_t ctx;
89 gpgme_error_t err;
90 passphrase_cb_s pwd;
91 char *signer;
92 int n = reg_prefs.word_wrap;
93
94 signer = get_gnupg_default_key ();
95 if (!signer) {
96 msg_box (dlg, _("Could not get default key."), _("Signing"), MB_ERR);
97 return;
98 }
99
100 err = gpgme_new (&ctx);
101 if (err)
102 BUG (dlg);
103
104 set_gpg_passphrase_cb (&pwd, ctx, GPG_CMD_SIGN, dlg, _("Signing"));
105 err = gpg_clip_sign (ctx, signer, n);
106 if (pwd.cancel)
107 goto leave;
108
109 if (gpgme_err_code (err) == GPG_ERR_BAD_PASSPHRASE)
110 agent_del_cache (pwd.keyid);
111 if (err)
112 msg_box (dlg, gpgme_strerror (err), _("Signing"), MB_ERR);
113 else
114 show_msg (dlg, 1500, _("GnuPG Status: Finished"));
115 leave:
116 gpgme_release (ctx);
117 free_if_alloc (signer);
118 release_gpg_passphrase_cb (&pwd);
119 }
120
121
122 /* Count only useable secret keys.
123 Ignore expired, revoked and disabled keys.
124 Return value: amount of keys. */
125 static DWORD
126 count_useable_seckeys (gpg_keycache_t kc)
127 {
128 struct keycache_s *c;
129 DWORD n = 0;
130
131 for (c = kc->item; c; c = c->next) {
132 if (c->pubpart && key_is_useable (c->pubpart->key))
133 n++;
134 }
135 return n;
136 }
137
138
139 static keylist_ctrl_t
140 on_init_dialog (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
141 {
142 keylist_ctrl_t kl;
143 gpg_keycache_t kc, sec_kc;
144 int nkeys;
145
146 SetWindowText (dlg, _("Signing"));
147 kc = keycache_get_ctx (KEYCACHE_PUB);
148 sec_kc = keycache_get_ctx (KEYCACHE_PRV);
149
150 nkeys = count_useable_seckeys (sec_kc);
151 if (nkeys < 1) {
152 msg_box (dlg, _("No useable signing key found"), _("Signing"), MB_ERR);
153 return NULL;
154 }
155 else if (nkeys == 1) {
156 one_key_proc (dlg);
157 return NULL;
158 }
159 kl = keylist_load (GetDlgItem (dlg, IDC_SIGN_KEYLIST), kc, sec_kc,
160 KEYLIST_SIGN, KEY_SORT_USERID);
161 center_window (dlg, NULL);
162 SetForegroundWindow (dlg);
163 return kl;
164 }
165
166
167 static const char*
168 lookup_key_userid (const char *patt)
169 {
170 winpt_key_s key;
171
172 memset (&key, 0, sizeof (key));
173 if (winpt_get_pubkey (patt, &key))
174 return patt;
175 return key.ext->uids->uid;
176 }
177
178
179 /* Dialog box procedure for clipboard signing. */
180 BOOL CALLBACK
181 clip_sign_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
182 {
183 static keylist_ctrl_t kl = NULL;
184 gpgme_ctx_t ctx;
185 gpgme_error_t err;
186 passphrase_cb_s pwd;
187 int lv_idx = 0;
188 int sel_signer = 0;
189 char *signer = NULL;
190
191 /* XXX: there seems to be a memory corruption in the code */
192
193 switch (msg) {
194 case WM_INITDIALOG:
195 kl = on_init_dialog (dlg, msg, wparam, lparam);
196 if (!kl)
197 EndDialog (dlg, TRUE);
198 return FALSE;
199
200 case WM_DESTROY:
201 if (kl) {
202 keylist_delete (kl);
203 kl = NULL;
204 }
205 return FALSE;
206
207 case WM_NOTIFY:
208 /* Wait until the dialog initialization is done */
209 if (kl == NULL)
210 break;
211
212 NMHDR *notify;
213 notify = (NMHDR *)lparam;
214 if (notify && notify->code == NM_DBLCLK &&
215 notify->idFrom == IDC_SIGN_KEYLIST)
216 PostMessage (dlg, WM_COMMAND, MAKEWPARAM (IDOK, 0), 0);
217
218 return FALSE;
219
220 case WM_COMMAND:
221 switch (LOWORD (wparam)) {
222 case IDOK:
223 sel_signer = 1;
224 signer = get_gnupg_default_key ();
225 if (!signer) {
226 msg_box (dlg, _("Could not get default key."), _("Signing"), MB_ERR);
227 return FALSE;
228 }
229 else if ((lv_idx = listview_get_selected_item (kl->lv)) == -1) {
230 int rc = log_box (_("Signing"), MB_YESNO,
231 _("No key was chosen.\n"
232 "Use the GPG default key '%s'?"),
233 lookup_key_userid (signer));
234 if (rc == IDNO) {
235 free_if_alloc (signer);
236 return FALSE;
237 }
238 sel_signer = 0;
239 }
240 if (sel_signer) {
241 free_if_alloc (signer);
242 signer = new char[32+1];
243 if (!signer)
244 BUG (NULL);
245 listview_get_item_text (kl->lv, lv_idx, KM_COL_KEYID, signer, 32);
246 }
247 err = gpgme_new (&ctx);
248 if (err)
249 BUG (NULL);
250 set_gpg_passphrase_cb (&pwd, ctx, GPG_CMD_SIGN, dlg, _("Signing"));
251 err = gpg_clip_sign (ctx, signer, reg_prefs.word_wrap);
252 free_if_alloc (signer);
253 release_gpg_passphrase_cb (&pwd);
254 gpgme_release (ctx);
255 if (pwd.cancel && gpgme_err_code (err) == GPG_ERR_BAD_PASSPHRASE) {
256 /* The user hit the cancel button or bad passphrase */
257 return TRUE;
258 }
259 if (err) {
260 msg_box (dlg, gpgme_strerror (err), _("Signing"), MB_ERR);
261 return TRUE;
262 }
263 else
264 show_msg (dlg, 1500, _("GnuPG Status: Finished"));
265 EndDialog (dlg, TRUE);
266 return TRUE;
267
268 case IDCANCEL:
269 EndDialog (dlg, FALSE);
270 return FALSE;
271 }
272 break;
273 }
274
275 return FALSE;
276 }

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26