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

Annotation of /trunk/Src/wptClipSignDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 256 - (hide annotations)
Sat Aug 5 10:31:06 2006 UTC (18 years, 6 months ago) by twoaday
File size: 7246 byte(s)
1.0.0pre3 release.


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

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26