1 |
/* wptSignEncDlg.cpp - Sign & encrypt dialog |
2 |
* Copyright (C) 2000-2006, 2009 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 |
#ifdef HAVE_CONFIG_H |
17 |
#include <config.h> |
18 |
#endif |
19 |
|
20 |
#include <windows.h> |
21 |
#include <commctrl.h> |
22 |
|
23 |
#include "resource.h" |
24 |
#include "wptErrors.h" |
25 |
#include "wptAgent.h" |
26 |
#include "wptCrypto.h" |
27 |
#include "wptGPG.h" |
28 |
#include "wptCommonCtl.h" |
29 |
#include "wptKeylist.h" |
30 |
#include "wptTypes.h" |
31 |
#include "wptNLS.h" |
32 |
#include "wptContext.h" /* for passphrase_s */ |
33 |
#include "wptDlgs.h" |
34 |
#include "wptW32API.h" |
35 |
#include "wptKeylist.h" |
36 |
#include "wptVersion.h" |
37 |
#include "wptGPG.h" |
38 |
#include "wptRegistry.h" |
39 |
#include "wptUTF8.h" |
40 |
|
41 |
|
42 |
/* Encrypt the clipboard data with the recipients from @rset and |
43 |
additionally sign the data before @signer as the keyID. |
44 |
Return value: 0 on success. */ |
45 |
gpgme_error_t |
46 |
gpg_clip_sign_encrypt (gpgme_ctx_t ctx, const char *signer, |
47 |
gpgme_key_t *rset, int opts) |
48 |
{ |
49 |
gpgme_error_t err; |
50 |
gpgme_data_t plain = NULL; |
51 |
gpgme_data_t ciph = NULL; |
52 |
gpgme_key_t key = NULL; |
53 |
|
54 |
if (!signer) |
55 |
return gpg_error (GPG_ERR_INV_ARG); |
56 |
if (get_pubkey (signer, &key)) |
57 |
return gpg_error (GPG_ERR_NO_PUBKEY); |
58 |
|
59 |
gpgme_set_armor (ctx, 1); |
60 |
gpgme_set_textmode (ctx, 1); |
61 |
|
62 |
err = gpg_data_utf8_new_from_clipboard (&plain, 0, NULL); |
63 |
if (err) |
64 |
goto leave; |
65 |
err = gpgme_data_new (&ciph); |
66 |
if (err) |
67 |
goto leave; |
68 |
err = gpgme_signers_add (ctx, key); |
69 |
if (err) |
70 |
goto leave; |
71 |
|
72 |
err = gpgme_op_encrypt_sign (ctx, rset, |
73 |
opts? GPGME_ENCRYPT_ALWAYS_TRUST : (gpgme_encrypt_flags_t)0, |
74 |
plain, ciph); |
75 |
if (err) |
76 |
goto leave; |
77 |
|
78 |
gpg_data_release_to_clipboard (ciph, 1); |
79 |
ciph = NULL; |
80 |
|
81 |
leave: |
82 |
if (plain) |
83 |
gpgme_data_release (plain); |
84 |
if (ciph) |
85 |
gpgme_data_release (ciph); |
86 |
return err; |
87 |
} |
88 |
|
89 |
|
90 |
/* Dialog procedure for clipboard sign + encrypt. */ |
91 |
BOOL CALLBACK |
92 |
clip_signenc_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
93 |
{ |
94 |
static keylist_ctrl_t kl = NULL; |
95 |
static keylist_t list = NULL; |
96 |
static int keys_sortby = 0; |
97 |
gpg_keycache_t kc; |
98 |
gpgme_key_t *rset; |
99 |
gpgme_error_t err; |
100 |
gpgme_ctx_t ctx; |
101 |
passphrase_cb_s pwd; |
102 |
char *signer = NULL; |
103 |
size_t n; |
104 |
int force_trust = 0; |
105 |
|
106 |
switch (msg) { |
107 |
case WM_INITDIALOG: |
108 |
SetWindowText (dlg, _("Sign & Encrypt")); |
109 |
SetDlgItemText (dlg, IDCANCEL, _("&Cancel")); |
110 |
kc = keycache_get_ctx (KEYCACHE_PUB); |
111 |
kl = keylist_load (GetDlgItem (dlg, IDC_SIGNENC_KEYLIST), |
112 |
kc, NULL, KEYLIST_ENCRYPT_MIN, KEY_SORT_USERID); |
113 |
seclist_init (dlg, IDC_SIGNENC_SECLIST, KEYLIST_FLAG_SHORT, &list); |
114 |
center_window (dlg, NULL); |
115 |
EnableWindow (GetDlgItem (dlg, IDC_SIGNENC_SECLIST), FALSE); |
116 |
SetDlgItemText (dlg, IDC_SIGNENC_SELKEY, _("Select key for signing")); |
117 |
SetDlgItemText (dlg, IDC_SIGNENC_SECLISTINF, _("Signing key:")); |
118 |
SetForegroundWindow (dlg); |
119 |
return TRUE; |
120 |
|
121 |
case WM_DESTROY: |
122 |
seclist_destroy (&list); |
123 |
if (kl) { |
124 |
keylist_delete (kl); |
125 |
kl = NULL; |
126 |
} |
127 |
return FALSE; |
128 |
|
129 |
case WM_NOTIFY: |
130 |
int ret; |
131 |
ret = keylist_listview_notify (dlg, kl->keys, |
132 |
IDC_SIGNENC_KEYLIST, lparam); |
133 |
if (ret != 0) { |
134 |
SetWindowLong (dlg, DWL_MSGRESULT, ret); |
135 |
return TRUE; |
136 |
} |
137 |
|
138 |
NMHDR *notify; |
139 |
notify = (NMHDR *)lparam; |
140 |
if (notify && notify->code == NM_DBLCLK |
141 |
&& notify->idFrom == IDC_SIGNENC_KEYLIST) |
142 |
PostMessage (dlg, WM_COMMAND, MAKEWPARAM (IDOK, 0), 0); |
143 |
|
144 |
if (notify && notify->code == LVN_COLUMNCLICK && |
145 |
notify->idFrom == IDC_SIGNENC_KEYLIST) { |
146 |
NMLISTVIEW *p = (LPNMLISTVIEW) lparam; |
147 |
int sortby = 0; |
148 |
|
149 |
switch (p->iSubItem) { |
150 |
case 0: sortby = KEY_SORT_USERID; break; |
151 |
case 1: sortby = KEY_SORT_KEYID; break; |
152 |
case 2: sortby = KEY_SORT_LEN; break; |
153 |
case 4: sortby = KEY_SORT_VALIDITY; break; |
154 |
default: sortby = KEY_SORT_USERID; break; |
155 |
} |
156 |
if ((keys_sortby & ~KEYLIST_SORT_DESC) == sortby) |
157 |
keys_sortby ^= KEYLIST_SORT_DESC; |
158 |
else |
159 |
keys_sortby = sortby; |
160 |
keylist_sort (kl, keys_sortby); |
161 |
} |
162 |
return TRUE; |
163 |
|
164 |
case WM_COMMAND: |
165 |
if (HIWORD (wparam) == BN_CLICKED |
166 |
&& LOWORD (wparam) == IDC_SIGNENC_SELKEY) { |
167 |
int enable = IsDlgButtonChecked (dlg, IDC_SIGNENC_SELKEY); |
168 |
EnableWindow (GetDlgItem (dlg, IDC_SIGNENC_SECLIST), enable? TRUE : FALSE); |
169 |
} |
170 |
switch (LOWORD (wparam)) { |
171 |
case IDOK: |
172 |
rset = keylist_get_recipients (kl, &force_trust, &n); |
173 |
if (!n) { |
174 |
msg_box (dlg, _("You must select at least one key."), |
175 |
_("Sign & Encrypt"), MB_ERR); |
176 |
return FALSE; |
177 |
} |
178 |
if (IsDlgButtonChecked (dlg, IDC_SIGNENC_SELKEY)) { |
179 |
gpgme_key_t key; |
180 |
const char *s; |
181 |
|
182 |
if (seclist_select_key (dlg, IDC_SIGNENC_SECLIST, &key)) { |
183 |
msg_box (dlg, _("No key was selected."), _("Signing"), MB_ERR); |
184 |
safe_free (rset); |
185 |
return FALSE; |
186 |
} |
187 |
s = key->subkeys->keyid; |
188 |
if (s) |
189 |
signer = m_strdup(s+8); |
190 |
} |
191 |
else { |
192 |
signer = get_gnupg_default_key (); |
193 |
if (!signer) { |
194 |
msg_box (dlg, _("Could not get default key."), _("Signing"), MB_ERR); |
195 |
safe_free (rset); |
196 |
return FALSE; |
197 |
} |
198 |
} |
199 |
|
200 |
err = gpgme_new (&ctx); |
201 |
if (err) |
202 |
BUG (NULL); |
203 |
set_gpg_passphrase_cb (&pwd, ctx, GPG_CMD_SIGN, dlg, _("Sign & Encrypt")); |
204 |
err = gpg_clip_sign_encrypt (ctx, signer, rset, force_trust); |
205 |
release_gpg_passphrase_cb (&pwd); |
206 |
safe_free (rset); |
207 |
free_if_alloc (signer); |
208 |
gpgme_release (ctx); |
209 |
if (gpgme_err_code (err) == GPG_ERR_BAD_PASSPHRASE) |
210 |
agent_del_cache (pwd.keyid); |
211 |
if (err) |
212 |
msg_box (dlg, gpgme_strerror (err), _("Sign & Encrypt"), MB_ERR); |
213 |
else { |
214 |
show_msg (dlg, 1500, _("GnuPG Status: Finished")); |
215 |
EndDialog (dlg, TRUE); |
216 |
} |
217 |
return TRUE; |
218 |
|
219 |
case IDCANCEL: |
220 |
EndDialog (dlg, FALSE); |
221 |
return FALSE; |
222 |
} |
223 |
break; |
224 |
} |
225 |
|
226 |
return FALSE; |
227 |
} |