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