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

Annotation of /trunk/Src/wptClipSignDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 68 - (hide annotations)
Sat Nov 5 12:00:55 2005 UTC (19 years, 3 months ago) by twoaday
File size: 6817 byte(s)
More minor changes to avoid GCC warnings.


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     get_pubkey (keyid, &key);
66     if (key)
67     err = gpgme_signers_add (ctx, key);
68     else {
69     err = gpg_error (GPG_ERR_NO_PUBKEY);
70     goto leave;
71     }
72     err = gpgme_data_new (&sig);
73     if (err)
74     goto leave;
75     err = gpgme_op_sign (ctx, plain, sig, GPGME_SIG_MODE_CLEAR);
76     if (err)
77     goto leave;
78    
79     gpg_data_release_and_set_clipboard (sig, 1);
80     sig = NULL;
81    
82     leave:
83     if (plain)
84     gpgme_data_release (plain);
85     if (sig)
86     gpgme_data_release (sig);
87     return err;
88     }
89    
90    
91     /* This function is used when only one secret key is available.
92     * it doesn't make sense to offer a dialog for this case.
93     */
94     void
95     one_key_proc (HWND dlg)
96     {
97     char * signer;
98     gpgme_ctx_t ctx;
99     gpgme_error_t err;
100     passphrase_cb_s pwd;
101     int n = reg_prefs.word_wrap;
102    
103     signer = get_gnupg_default_key ();
104     if (!signer) {
105     msg_box (dlg, _("Could not get default key."), _("Signing"), MB_ERR);
106     return;
107     }
108    
109     err = gpgme_new (&ctx);
110     if (err)
111     BUG (dlg);
112    
113     set_gpg_passphrase_cb (&pwd, ctx, GPG_CMD_SIGN, dlg, _("Signing"));
114     err = gpg_clip_sign (ctx, signer, n );
115     wipememory (pwd.pwd, sizeof (pwd.pwd));
116     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     gpgme_release (ctx);
123     free_if_alloc (signer);
124     }
125    
126    
127     /* Dialog box procedure for clipboard signing. */
128     BOOL CALLBACK
129     clip_sign_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
130     {
131     static listview_ctrl_t lv = NULL;
132     gpg_keycache_t kc, sec_kc;
133     gpgme_ctx_t ctx;
134     gpgme_error_t err;
135     passphrase_cb_s pwd;
136     int lv_idx = 0;
137     int rc = 0, no_signer = 0;
138     char * signer = NULL;
139    
140     switch( msg ) {
141     case WM_INITDIALOG:
142     SetWindowText (dlg, _("Signing"));
143    
144     kc = keycache_get_ctx (KEYCACHE_PUB);
145     if (!kc)
146     BUG( NULL );
147     sec_kc = keycache_get_ctx (KEYCACHE_PRV);
148     if (!sec_kc)
149     BUG (dlg);
150     if (gpg_keycache_get_size (sec_kc) == 1) {
151     one_key_proc (dlg);
152     EndDialog (dlg, TRUE);
153     return FALSE;
154     }
155     lv = keylist_load (GetDlgItem (dlg, IDC_SIGN_KEYLIST), kc, sec_kc,
156     KEYLIST_SIGN, KEY_SORT_USERID);
157     center_window (dlg, NULL);
158     SetForegroundWindow (dlg);
159     set_active_window (dlg);
160     return FALSE;
161    
162     case WM_DESTROY:
163     reset_active_window ();
164     if (lv) {
165     keylist_delete (lv);
166     lv = NULL;
167     }
168     return FALSE;
169    
170     case WM_NOTIFY:
171     NMHDR * notify;
172     notify = (NMHDR *)lparam;
173     if( notify && notify->code == NM_DBLCLK
174     && notify->idFrom == IDC_SIGN_KEYLIST )
175 twoaday 65 PostMessage (dlg, WM_COMMAND, MAKEWPARAM(IDOK, 0), 0);
176 werner 36 return TRUE;
177    
178     case WM_SYSCOMMAND:
179     if( LOWORD (wparam) == SC_CLOSE )
180     EndDialog(dlg, TRUE);
181     return FALSE;
182    
183     case WM_COMMAND:
184     switch (LOWORD (wparam)) {
185     case IDOK:
186     signer = get_gnupg_default_key ();
187     if (!signer) {
188     msg_box( dlg, _("Could not get default key."), _("Signing"), MB_ERR );
189     return FALSE;
190     }
191     if (listview_count_items (lv, 0) == 1) {
192     listview_get_item_text (lv, 0, 1, signer, sizeof signer-1);
193     no_signer = 0;
194     }
195     else if ((lv_idx = listview_get_curr_pos (lv)) == -1) {
196     rc = log_box (_("Signing"), MB_YESNO,
197     _("No key was chosen.\nUse the GPG default key '%s'?"),
198     signer);
199     if (rc == IDNO) {
200     free_if_alloc (signer);
201     return FALSE;
202     }
203     no_signer = 1;
204     }
205     if (!no_signer) {
206     free_if_alloc (signer);
207     signer = new char[32+1];
208     if (!signer)
209     BUG (NULL);
210     listview_get_item_text (lv, lv_idx, 1, signer, 32);
211     }
212     err = gpgme_new (&ctx);
213     if (err)
214     BUG (dlg);
215     set_gpg_passphrase_cb (&pwd, ctx, GPG_CMD_SIGN, dlg, _("Signing"));
216     err = gpg_clip_sign (ctx, signer, reg_prefs.word_wrap);
217     free_if_alloc (signer);
218     release_gpg_passphrase_cb (&pwd);
219    
220     if (pwd.cancel && gpgme_err_code(err) == GPG_ERR_BAD_PASSPHRASE) {
221     /* The user hit the cancel button or bad passphrase */
222     gpgme_release (ctx);
223     EndDialog (dlg, TRUE);
224     return TRUE;
225     }
226     if (err) {
227     msg_box (dlg, gpgme_strerror (err), _("Signing"), MB_ERR);
228     gpgme_release (ctx);
229     return FALSE;
230     }
231     else
232     show_msg( dlg, 1500, _("GnuPG Status: Finished") );
233     gpgme_release (ctx);
234     EndDialog (dlg, TRUE);
235     return TRUE;
236    
237     case IDCANCEL:
238     EndDialog (dlg, FALSE);
239     return FALSE;
240     }
241     break;
242     }
243    
244     return FALSE;
245     }
246    

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26