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