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

Annotation of /trunk/Src/wptClipSignDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 212 - (hide annotations)
Tue May 9 10:29:07 2006 UTC (18 years, 9 months ago) by twoaday
File size: 7366 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     int nkeys = 0;
152    
153     SetWindowText (dlg, _("Signing"));
154     kc = keycache_get_ctx (KEYCACHE_PUB);
155     if (!kc)
156     BUG (0);
157     sec_kc = keycache_get_ctx (KEYCACHE_PRV);
158     if (!sec_kc)
159     BUG (0);
160    
161     nkeys = count_useable_seckeys (sec_kc);
162     if (nkeys < 1) {
163     msg_box (dlg, _("No useable signing key found"), _("Signing"), MB_ERR);
164     return NULL;
165     }
166     else if (nkeys == 1) {
167     one_key_proc (dlg);
168     return NULL;
169     }
170     lv = keylist_load (GetDlgItem (dlg, IDC_SIGN_KEYLIST), kc, sec_kc,
171     KEYLIST_SIGN, KEY_SORT_USERID);
172     center_window (dlg, NULL);
173     SetForegroundWindow (dlg);
174     return lv;
175     }
176    
177    
178 werner 36 /* Dialog box procedure for clipboard signing. */
179     BOOL CALLBACK
180     clip_sign_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
181     {
182 twoaday 188 static listview_ctrl_t lv = NULL;
183 werner 36 gpgme_ctx_t ctx;
184     gpgme_error_t err;
185     passphrase_cb_s pwd;
186     int lv_idx = 0;
187     int rc = 0, no_signer = 0;
188 twoaday 175 char *signer = NULL;
189 werner 36
190 twoaday 188 switch (msg) {
191 werner 36 case WM_INITDIALOG:
192 twoaday 188 lv = on_init_dialog (dlg, msg, wparam, lparam);
193     if (!lv)
194     EndDialog (dlg, TRUE);
195 werner 36 return FALSE;
196    
197     case WM_DESTROY:
198     if (lv) {
199     keylist_delete (lv);
200     lv = NULL;
201     }
202     return FALSE;
203    
204     case WM_NOTIFY:
205     NMHDR * notify;
206     notify = (NMHDR *)lparam;
207     if( notify && notify->code == NM_DBLCLK
208     && notify->idFrom == IDC_SIGN_KEYLIST )
209 twoaday 65 PostMessage (dlg, WM_COMMAND, MAKEWPARAM(IDOK, 0), 0);
210 werner 36 return TRUE;
211    
212     case WM_SYSCOMMAND:
213     if( LOWORD (wparam) == SC_CLOSE )
214     EndDialog(dlg, TRUE);
215     return FALSE;
216    
217     case WM_COMMAND:
218     switch (LOWORD (wparam)) {
219     case IDOK:
220     signer = get_gnupg_default_key ();
221     if (!signer) {
222     msg_box( dlg, _("Could not get default key."), _("Signing"), MB_ERR );
223     return FALSE;
224     }
225     if (listview_count_items (lv, 0) == 1) {
226     listview_get_item_text (lv, 0, 1, signer, sizeof signer-1);
227     no_signer = 0;
228     }
229 twoaday 193 else if ((lv_idx = listview_get_selected_item (lv)) == -1) {
230 werner 36 rc = log_box (_("Signing"), MB_YESNO,
231     _("No key was chosen.\nUse the GPG default key '%s'?"),
232     signer);
233     if (rc == IDNO) {
234     free_if_alloc (signer);
235     return FALSE;
236     }
237     no_signer = 1;
238     }
239     if (!no_signer) {
240     free_if_alloc (signer);
241     signer = new char[32+1];
242     if (!signer)
243     BUG (NULL);
244     listview_get_item_text (lv, lv_idx, 1, signer, 32);
245     }
246     err = gpgme_new (&ctx);
247     if (err)
248     BUG (dlg);
249     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     if (pwd.cancel && gpgme_err_code(err) == GPG_ERR_BAD_PASSPHRASE) {
255     /* 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     return FALSE;
263     }
264     else
265     show_msg( dlg, 1500, _("GnuPG Status: Finished") );
266     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