1 |
/* wptSignEncDlg.cpp - Sign & encrypt dialog |
2 |
* Copyright (C) 2000-2006 Timo Schulz |
3 |
* |
4 |
* This file is part of WinPT. |
5 |
* |
6 |
* WinPT is free software; you can redistribute it and/or modify |
7 |
* it under the terms of the GNU General Public License as published by |
8 |
* the Free Software Foundation; either version 2 of the License, or |
9 |
* (at your option) any later version. |
10 |
* |
11 |
* WinPT is distributed in the hope that it will be useful, |
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
* GNU General Public License for more details. |
15 |
* |
16 |
* You should have received a copy of the GNU General Public License |
17 |
* along with WinPT; if not, write to the Free Software Foundation, |
18 |
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
19 |
*/ |
20 |
#ifdef HAVE_CONFIG_H |
21 |
#include <config.h> |
22 |
#endif |
23 |
|
24 |
#include <windows.h> |
25 |
#include <commctrl.h> |
26 |
|
27 |
#include "resource.h" |
28 |
#include "wptErrors.h" |
29 |
#include "wptAgent.h" |
30 |
#include "wptCrypto.h" |
31 |
#include "wptGPG.h" |
32 |
#include "wptCommonCtl.h" |
33 |
#include "wptKeylist.h" |
34 |
#include "wptTypes.h" |
35 |
#include "wptNLS.h" |
36 |
#include "wptContext.h" /* for passphrase_s */ |
37 |
#include "wptDlgs.h" |
38 |
#include "wptW32API.h" |
39 |
#include "wptKeylist.h" |
40 |
#include "wptVersion.h" |
41 |
#include "wptGPG.h" |
42 |
#include "wptRegistry.h" |
43 |
#include "wptUTF8.h" |
44 |
|
45 |
|
46 |
/* Encrypt the clipboard data with the recipients from @rset and |
47 |
additionally sign the data before @signer as the keyID. |
48 |
Return value: 0 on success. */ |
49 |
gpgme_error_t |
50 |
gpg_clip_sign_encrypt (gpgme_ctx_t ctx, const char *signer, |
51 |
gpgme_key_t *rset, int opts) |
52 |
{ |
53 |
gpgme_error_t err; |
54 |
gpgme_data_t plain = NULL; |
55 |
gpgme_data_t ciph = NULL; |
56 |
gpgme_key_t key = NULL; |
57 |
|
58 |
if (!signer) |
59 |
return gpg_error (GPG_ERR_INV_ARG); |
60 |
if (get_pubkey (signer, &key)) |
61 |
return gpg_error (GPG_ERR_NO_PUBKEY); |
62 |
|
63 |
gpgme_set_armor (ctx, 1); |
64 |
gpgme_set_textmode (ctx, 1); |
65 |
|
66 |
err = gpg_data_new_from_clipboard (&plain, 0); |
67 |
if (err) |
68 |
goto leave; |
69 |
err = gpgme_data_new (&ciph); |
70 |
if (err) |
71 |
goto leave; |
72 |
err = gpgme_signers_add (ctx, key); |
73 |
if (err) |
74 |
goto leave; |
75 |
|
76 |
err = gpgme_op_encrypt_sign (ctx, rset, |
77 |
opts? GPGME_ENCRYPT_ALWAYS_TRUST : (gpgme_encrypt_flags_t)0, |
78 |
plain, ciph); |
79 |
if (err) |
80 |
goto leave; |
81 |
|
82 |
gpg_data_release_and_set_clipboard (ciph, 1); |
83 |
ciph = NULL; |
84 |
|
85 |
leave: |
86 |
if (plain) |
87 |
gpgme_data_release (plain); |
88 |
if (ciph) |
89 |
gpgme_data_release (ciph); |
90 |
return err; |
91 |
} |
92 |
|
93 |
|
94 |
/* Dialog procedure for clipboard sign + encrypt. */ |
95 |
BOOL CALLBACK |
96 |
clip_signenc_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
97 |
{ |
98 |
static listview_ctrl_t lv = NULL; |
99 |
static keylist_t list = NULL; |
100 |
gpg_keycache_t kc; |
101 |
gpgme_key_t *rset; |
102 |
gpgme_error_t err; |
103 |
gpgme_ctx_t ctx; |
104 |
passphrase_cb_s pwd; |
105 |
char *signer = NULL; |
106 |
size_t n; |
107 |
int force_trust = 0; |
108 |
|
109 |
switch (msg) { |
110 |
case WM_INITDIALOG: |
111 |
SetWindowText (dlg, _("Sign & Encrypt")); |
112 |
SetDlgItemText (dlg, IDCANCEL, _("&Cancel")); |
113 |
kc = keycache_get_ctx (KEYCACHE_PUB); |
114 |
lv = keylist_load (GetDlgItem (dlg, IDC_SIGNENC_KEYLIST), |
115 |
kc, NULL, KEYLIST_ENCRYPT_MIN, KEY_SORT_USERID); |
116 |
seclist_init (dlg, IDC_SIGNENC_SECLIST, KEYLIST_FLAG_SHORT, &list); |
117 |
center_window (dlg, NULL); |
118 |
EnableWindow (GetDlgItem (dlg, IDC_SIGNENC_SECLIST), FALSE); |
119 |
SetDlgItemText (dlg, IDC_SIGNENC_SELKEY, _("Select key for signing")); |
120 |
SetDlgItemText (dlg, IDC_SIGNENC_SECLISTINF, _("Signing key:")); |
121 |
SetForegroundWindow (dlg); |
122 |
return TRUE; |
123 |
|
124 |
case WM_DESTROY: |
125 |
seclist_destroy (&list); |
126 |
if (lv) { |
127 |
keylist_delete (lv); |
128 |
lv = NULL; |
129 |
} |
130 |
return FALSE; |
131 |
|
132 |
case WM_NOTIFY: |
133 |
NMHDR *notify; |
134 |
notify = (NMHDR *)lparam; |
135 |
if (notify && notify->code == NM_DBLCLK |
136 |
&& notify->idFrom == IDC_SIGNENC_KEYLIST) |
137 |
PostMessage (dlg, WM_COMMAND, MAKEWPARAM (IDOK, 0), 0); |
138 |
return TRUE; |
139 |
|
140 |
case WM_COMMAND: |
141 |
if (HIWORD (wparam) == BN_CLICKED |
142 |
&& LOWORD (wparam) == IDC_SIGNENC_SELKEY) { |
143 |
int enable = IsDlgButtonChecked (dlg, IDC_SIGNENC_SELKEY); |
144 |
EnableWindow (GetDlgItem (dlg, IDC_SIGNENC_SECLIST), enable? TRUE : FALSE); |
145 |
} |
146 |
switch (LOWORD (wparam)) { |
147 |
case IDOK: |
148 |
rset = keylist_get_recipients (lv, &force_trust, &n); |
149 |
if (!n) { |
150 |
msg_box (dlg, _("You must select at least one key."), |
151 |
_("Sign & Encrypt"), MB_ERR); |
152 |
return FALSE; |
153 |
} |
154 |
if (IsDlgButtonChecked (dlg, IDC_SIGNENC_SELKEY)) { |
155 |
gpgme_key_t key; |
156 |
const char *s; |
157 |
|
158 |
if (seclist_select_key (dlg, IDC_SIGNENC_SECLIST, &key)) { |
159 |
msg_box (dlg, _("No key was selected."), _("Signing"), MB_ERR); |
160 |
safe_free (rset); |
161 |
return FALSE; |
162 |
} |
163 |
s = key->subkeys->keyid; |
164 |
if (s) |
165 |
signer = m_strdup(s+8); |
166 |
} |
167 |
else { |
168 |
signer = get_gnupg_default_key (); |
169 |
if (!signer) { |
170 |
msg_box (dlg, _("Could not get default key."), _("Signing"), MB_ERR); |
171 |
safe_free (rset); |
172 |
return FALSE; |
173 |
} |
174 |
} |
175 |
|
176 |
err = gpgme_new (&ctx); |
177 |
if (err) |
178 |
BUG (NULL); |
179 |
set_gpg_passphrase_cb (&pwd, ctx, GPG_CMD_SIGN, dlg, _("Sign & Encrypt")); |
180 |
err = gpg_clip_sign_encrypt (ctx, signer, rset, force_trust); |
181 |
release_gpg_passphrase_cb (&pwd); |
182 |
safe_free (rset); |
183 |
free_if_alloc (signer); |
184 |
gpgme_release (ctx); |
185 |
if (gpgme_err_code (err) == GPG_ERR_BAD_PASSPHRASE) |
186 |
agent_del_cache (pwd.keyid); |
187 |
if (err) |
188 |
msg_box (dlg, gpgme_strerror (err), _("Sign & Encrypt"), MB_ERR); |
189 |
else { |
190 |
show_msg (dlg, 1500, _("GnuPG Status: Finished")); |
191 |
EndDialog (dlg, TRUE); |
192 |
} |
193 |
return TRUE; |
194 |
|
195 |
case IDCANCEL: |
196 |
EndDialog (dlg, FALSE); |
197 |
return FALSE; |
198 |
} |
199 |
break; |
200 |
} |
201 |
|
202 |
return FALSE; |
203 |
} |