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