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 rc = 0; |
102 |
|
|
int n = reg_prefs.word_wrap; |
103 |
|
|
|
104 |
|
|
signer = get_gnupg_default_key (); |
105 |
|
|
if (!signer) { |
106 |
|
|
msg_box (dlg, _("Could not get default key."), _("Signing"), MB_ERR); |
107 |
|
|
return; |
108 |
|
|
} |
109 |
|
|
|
110 |
|
|
err = gpgme_new (&ctx); |
111 |
|
|
if (err) |
112 |
|
|
BUG (dlg); |
113 |
|
|
|
114 |
|
|
set_gpg_passphrase_cb (&pwd, ctx, GPG_CMD_SIGN, dlg, _("Signing")); |
115 |
|
|
err = gpg_clip_sign (ctx, signer, n ); |
116 |
|
|
wipememory (pwd.pwd, sizeof (pwd.pwd)); |
117 |
|
|
if (gpgme_err_code (err) == GPG_ERR_BAD_PASSPHRASE) |
118 |
|
|
agent_del_cache (pwd.keyid); |
119 |
|
|
if (err) |
120 |
|
|
msg_box (dlg, gpgme_strerror (err), _("Signing"), MB_ERR); |
121 |
|
|
else |
122 |
|
|
show_msg (dlg, 1500, _("GnuPG Status: Finished")); |
123 |
|
|
gpgme_release (ctx); |
124 |
|
|
free_if_alloc (signer); |
125 |
|
|
} |
126 |
|
|
|
127 |
|
|
|
128 |
|
|
/* Dialog box procedure for clipboard signing. */ |
129 |
|
|
BOOL CALLBACK |
130 |
|
|
clip_sign_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
131 |
|
|
{ |
132 |
|
|
static listview_ctrl_t lv = NULL; |
133 |
|
|
gpg_keycache_t kc, sec_kc; |
134 |
|
|
gpgme_ctx_t ctx; |
135 |
|
|
gpgme_error_t err; |
136 |
|
|
passphrase_cb_s pwd; |
137 |
|
|
int lv_idx = 0; |
138 |
|
|
int rc = 0, no_signer = 0; |
139 |
|
|
char * signer = NULL; |
140 |
|
|
|
141 |
|
|
switch( msg ) { |
142 |
|
|
case WM_INITDIALOG: |
143 |
|
|
SetWindowText (dlg, _("Signing")); |
144 |
|
|
|
145 |
|
|
kc = keycache_get_ctx (KEYCACHE_PUB); |
146 |
|
|
if (!kc) |
147 |
|
|
BUG( NULL ); |
148 |
|
|
sec_kc = keycache_get_ctx (KEYCACHE_PRV); |
149 |
|
|
if (!sec_kc) |
150 |
|
|
BUG (dlg); |
151 |
|
|
if (gpg_keycache_get_size (sec_kc) == 1) { |
152 |
|
|
one_key_proc (dlg); |
153 |
|
|
EndDialog (dlg, TRUE); |
154 |
|
|
return FALSE; |
155 |
|
|
} |
156 |
|
|
lv = keylist_load (GetDlgItem (dlg, IDC_SIGN_KEYLIST), kc, sec_kc, |
157 |
|
|
KEYLIST_SIGN, KEY_SORT_USERID); |
158 |
|
|
center_window (dlg, NULL); |
159 |
|
|
SetForegroundWindow (dlg); |
160 |
|
|
set_active_window (dlg); |
161 |
|
|
return FALSE; |
162 |
|
|
|
163 |
|
|
case WM_DESTROY: |
164 |
|
|
reset_active_window (); |
165 |
|
|
if (lv) { |
166 |
|
|
keylist_delete (lv); |
167 |
|
|
lv = NULL; |
168 |
|
|
} |
169 |
|
|
return FALSE; |
170 |
|
|
|
171 |
|
|
case WM_NOTIFY: |
172 |
|
|
NMHDR * notify; |
173 |
|
|
notify = (NMHDR *)lparam; |
174 |
|
|
if( notify && notify->code == NM_DBLCLK |
175 |
|
|
&& notify->idFrom == IDC_SIGN_KEYLIST ) |
176 |
|
|
PostMessage( dlg, WM_COMMAND, MAKEWPARAM(IDOK, 0), NULL ); |
177 |
|
|
return TRUE; |
178 |
|
|
|
179 |
|
|
case WM_SYSCOMMAND: |
180 |
|
|
if( LOWORD (wparam) == SC_CLOSE ) |
181 |
|
|
EndDialog(dlg, TRUE); |
182 |
|
|
return FALSE; |
183 |
|
|
|
184 |
|
|
case WM_COMMAND: |
185 |
|
|
switch (LOWORD (wparam)) { |
186 |
|
|
case IDOK: |
187 |
|
|
signer = get_gnupg_default_key (); |
188 |
|
|
if (!signer) { |
189 |
|
|
msg_box( dlg, _("Could not get default key."), _("Signing"), MB_ERR ); |
190 |
|
|
return FALSE; |
191 |
|
|
} |
192 |
|
|
if (listview_count_items (lv, 0) == 1) { |
193 |
|
|
listview_get_item_text (lv, 0, 1, signer, sizeof signer-1); |
194 |
|
|
no_signer = 0; |
195 |
|
|
} |
196 |
|
|
else if ((lv_idx = listview_get_curr_pos (lv)) == -1) { |
197 |
|
|
rc = log_box (_("Signing"), MB_YESNO, |
198 |
|
|
_("No key was chosen.\nUse the GPG default key '%s'?"), |
199 |
|
|
signer); |
200 |
|
|
if (rc == IDNO) { |
201 |
|
|
free_if_alloc (signer); |
202 |
|
|
return FALSE; |
203 |
|
|
} |
204 |
|
|
no_signer = 1; |
205 |
|
|
} |
206 |
|
|
if (!no_signer) { |
207 |
|
|
free_if_alloc (signer); |
208 |
|
|
signer = new char[32+1]; |
209 |
|
|
if (!signer) |
210 |
|
|
BUG (NULL); |
211 |
|
|
listview_get_item_text (lv, lv_idx, 1, signer, 32); |
212 |
|
|
} |
213 |
|
|
err = gpgme_new (&ctx); |
214 |
|
|
if (err) |
215 |
|
|
BUG (dlg); |
216 |
|
|
set_gpg_passphrase_cb (&pwd, ctx, GPG_CMD_SIGN, dlg, _("Signing")); |
217 |
|
|
err = gpg_clip_sign (ctx, signer, reg_prefs.word_wrap); |
218 |
|
|
free_if_alloc (signer); |
219 |
|
|
release_gpg_passphrase_cb (&pwd); |
220 |
|
|
|
221 |
|
|
if (pwd.cancel && gpgme_err_code(err) == GPG_ERR_BAD_PASSPHRASE) { |
222 |
|
|
/* The user hit the cancel button or bad passphrase */ |
223 |
|
|
gpgme_release (ctx); |
224 |
|
|
EndDialog (dlg, TRUE); |
225 |
|
|
return TRUE; |
226 |
|
|
} |
227 |
|
|
if (err) { |
228 |
|
|
msg_box (dlg, gpgme_strerror (err), _("Signing"), MB_ERR); |
229 |
|
|
gpgme_release (ctx); |
230 |
|
|
return FALSE; |
231 |
|
|
} |
232 |
|
|
else |
233 |
|
|
show_msg( dlg, 1500, _("GnuPG Status: Finished") ); |
234 |
|
|
gpgme_release (ctx); |
235 |
|
|
EndDialog (dlg, TRUE); |
236 |
|
|
return TRUE; |
237 |
|
|
|
238 |
|
|
case IDCANCEL: |
239 |
|
|
EndDialog (dlg, FALSE); |
240 |
|
|
return FALSE; |
241 |
|
|
} |
242 |
|
|
break; |
243 |
|
|
} |
244 |
|
|
|
245 |
|
|
return FALSE; |
246 |
|
|
} |
247 |
|
|
|