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

Annotation of /trunk/Src/wptClipSignDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 225 - (hide annotations)
Tue Jun 6 13:37:59 2006 UTC (18 years, 8 months ago) by twoaday
File size: 7267 byte(s)


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

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26