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

Annotation of /trunk/Src/wptClipSignDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 328 - (hide annotations)
Fri Sep 25 16:07:38 2009 UTC (15 years, 5 months ago) by twoaday
File size: 7032 byte(s)


1 werner 36 /* wptClipSignDlg.cpp - WinPT clipboard sign dialog
2 twoaday 328 * Copyright (C) 2000-2006, 2009 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 twoaday 23
18 werner 36 #ifdef HAVE_CONFIG_H
19     #include <config.h>
20     #endif
21    
22     #include <windows.h>
23     #include <commctrl.h>
24    
25 werner 47 #include "resource.h"
26 werner 36 #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 twoaday 328 err = gpg_data_utf8_new_from_clipboard (&plain, wraplen, NULL);
56 werner 36 if (err)
57     return err;
58 twoaday 212 err = get_pubkey (keyid, &key);
59     if (err)
60 werner 36 goto leave;
61 twoaday 212 err = gpgme_signers_add (ctx, key);
62     if (err)
63     goto leave;
64 werner 36 err = gpgme_data_new (&sig);
65     if (err)
66 twoaday 256 goto leave;
67 werner 36 err = gpgme_op_sign (ctx, plain, sig, GPGME_SIG_MODE_CLEAR);
68     if (err)
69     goto leave;
70    
71 twoaday 328 gpg_data_release_to_clipboard (sig, 1);
72 werner 36 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 twoaday 77 it doesn't make sense to offer a dialog for this case. */
85 werner 36 void
86     one_key_proc (HWND dlg)
87 twoaday 256 {
88 werner 36 gpgme_ctx_t ctx;
89     gpgme_error_t err;
90     passphrase_cb_s pwd;
91 twoaday 256 char *signer;
92 werner 36 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 twoaday 77 err = gpg_clip_sign (ctx, signer, n);
106 twoaday 175 if (pwd.cancel)
107     goto leave;
108    
109 werner 36 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 twoaday 175 leave:
116 werner 36 gpgme_release (ctx);
117     free_if_alloc (signer);
118 twoaday 77 release_gpg_passphrase_cb (&pwd);
119 werner 36 }
120    
121    
122 twoaday 175 /* 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 twoaday 328 static keylist_ctrl_t
140 twoaday 188 on_init_dialog (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
141     {
142 twoaday 328 keylist_ctrl_t kl;
143 twoaday 188 gpg_keycache_t kc, sec_kc;
144 twoaday 225 int nkeys;
145 twoaday 188
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 twoaday 328 kl = keylist_load (GetDlgItem (dlg, IDC_SIGN_KEYLIST), kc, sec_kc,
160 twoaday 188 KEYLIST_SIGN, KEY_SORT_USERID);
161     center_window (dlg, NULL);
162     SetForegroundWindow (dlg);
163 twoaday 328 return kl;
164 twoaday 188 }
165    
166    
167 twoaday 225 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 werner 36 /* Dialog box procedure for clipboard signing. */
180     BOOL CALLBACK
181     clip_sign_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
182     {
183 twoaday 328 static keylist_ctrl_t kl = NULL;
184 werner 36 gpgme_ctx_t ctx;
185     gpgme_error_t err;
186     passphrase_cb_s pwd;
187     int lv_idx = 0;
188 twoaday 225 int rc = 0, sel_signer = 0;
189 twoaday 175 char *signer = NULL;
190 werner 36
191 twoaday 188 switch (msg) {
192 werner 36 case WM_INITDIALOG:
193 twoaday 328 kl = on_init_dialog (dlg, msg, wparam, lparam);
194     if (!kl)
195 twoaday 188 EndDialog (dlg, TRUE);
196 werner 36 return FALSE;
197    
198     case WM_DESTROY:
199 twoaday 328 if (kl) {
200     keylist_delete (kl);
201     kl = NULL;
202 werner 36 }
203     return FALSE;
204    
205     case WM_NOTIFY:
206 twoaday 225 NMHDR *notify;
207 werner 36 notify = (NMHDR *)lparam;
208 twoaday 225 if (notify && notify->code == NM_DBLCLK
209     && notify->idFrom == IDC_SIGN_KEYLIST)
210     PostMessage (dlg, WM_COMMAND, MAKEWPARAM (IDOK, 0), 0);
211 werner 36 return TRUE;
212    
213     case WM_COMMAND:
214     switch (LOWORD (wparam)) {
215     case IDOK:
216 twoaday 225 sel_signer = 1;
217 werner 36 signer = get_gnupg_default_key ();
218     if (!signer) {
219 twoaday 225 msg_box (dlg, _("Could not get default key."),
220     _("Signing"), MB_ERR);
221 werner 36 return FALSE;
222     }
223 twoaday 328 else if ((lv_idx = listview_get_selected_item (kl->lv)) == -1) {
224 werner 36 rc = log_box (_("Signing"), MB_YESNO,
225 twoaday 225 _("No key was chosen.\n"
226     "Use the GPG default key '%s'?"),
227     lookup_key_userid (signer));
228 werner 36 if (rc == IDNO) {
229     free_if_alloc (signer);
230     return FALSE;
231     }
232 twoaday 225 sel_signer = 0;
233 werner 36 }
234 twoaday 225 if (sel_signer) {
235 werner 36 free_if_alloc (signer);
236     signer = new char[32+1];
237     if (!signer)
238     BUG (NULL);
239 twoaday 328 listview_get_item_text (kl->lv, lv_idx, KM_COL_KEYID, signer, 32);
240 werner 36 }
241     err = gpgme_new (&ctx);
242     if (err)
243 twoaday 225 BUG (NULL);
244 werner 36 set_gpg_passphrase_cb (&pwd, ctx, GPG_CMD_SIGN, dlg, _("Signing"));
245     err = gpg_clip_sign (ctx, signer, reg_prefs.word_wrap);
246     free_if_alloc (signer);
247     release_gpg_passphrase_cb (&pwd);
248    
249 twoaday 256 if (pwd.cancel && gpgme_err_code (err) == GPG_ERR_BAD_PASSPHRASE) {
250 werner 36 /* The user hit the cancel button or bad passphrase */
251     gpgme_release (ctx);
252     return TRUE;
253     }
254     if (err) {
255     msg_box (dlg, gpgme_strerror (err), _("Signing"), MB_ERR);
256     gpgme_release (ctx);
257 twoaday 225 return TRUE;
258 werner 36 }
259 twoaday 225 else
260     show_msg (dlg, 1500, _("GnuPG Status: Finished"));
261 werner 36 gpgme_release (ctx);
262     EndDialog (dlg, TRUE);
263     return TRUE;
264    
265     case IDCANCEL:
266     EndDialog (dlg, FALSE);
267     return FALSE;
268     }
269     break;
270     }
271    
272     return FALSE;
273     }

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26