1 |
twoaday |
2 |
/* wptSignEncDlg.cpp - Sign & encrypt dialog |
2 |
twoaday |
12 |
* Copyright (C) 2000-2005 Timo Schulz |
3 |
twoaday |
2 |
* |
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 |
|
|
|
21 |
|
|
#include <windows.h> |
22 |
|
|
#include <commctrl.h> |
23 |
|
|
|
24 |
|
|
#include "../resource.h" |
25 |
|
|
#include "wptErrors.h" |
26 |
|
|
#include "wptAgent.h" |
27 |
twoaday |
32 |
#include "wptCrypto.h" |
28 |
twoaday |
2 |
#include "wptGPG.h" |
29 |
|
|
#include "wptCommonCtl.h" |
30 |
|
|
#include "wptKeylist.h" |
31 |
|
|
#include "wptTypes.h" |
32 |
|
|
#include "wptNLS.h" |
33 |
|
|
#include "wptContext.h" /* for passphrase_s */ |
34 |
|
|
#include "wptDlgs.h" |
35 |
|
|
#include "wptW32API.h" |
36 |
|
|
#include "wptKeylist.h" |
37 |
|
|
#include "wptVersion.h" |
38 |
|
|
#include "wptGPG.h" |
39 |
|
|
#include "wptRegistry.h" |
40 |
|
|
#include "wptUTF8.h" |
41 |
|
|
|
42 |
|
|
|
43 |
twoaday |
23 |
/* Encrypt the clipboard data with the recipients from @rset and |
44 |
|
|
additionally sign the data before @signer as the keyID. |
45 |
|
|
Return value: 0 on success. */ |
46 |
|
|
gpgme_error_t |
47 |
twoaday |
24 |
gpg_clip_sign_encrypt (gpgme_ctx_t ctx, const char *signer, |
48 |
twoaday |
23 |
gpgme_key_t *rset, int opts) |
49 |
|
|
{ |
50 |
|
|
gpgme_error_t err; |
51 |
|
|
gpgme_data_t plain = NULL; |
52 |
|
|
gpgme_data_t ciph = NULL; |
53 |
|
|
gpgme_key_t key = NULL; |
54 |
|
|
|
55 |
|
|
if (!signer) |
56 |
|
|
return gpg_error (GPG_ERR_INV_ARG); |
57 |
|
|
if (get_pubkey (signer, &key)) |
58 |
|
|
return gpg_error (GPG_ERR_NO_PUBKEY); |
59 |
|
|
|
60 |
|
|
gpgme_set_armor (ctx, 1); |
61 |
|
|
|
62 |
|
|
err = gpg_data_new_from_clipboard (&plain, 0); |
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_and_set_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 |
twoaday |
2 |
BOOL CALLBACK |
92 |
twoaday |
12 |
clip_signenc_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
93 |
twoaday |
2 |
{ |
94 |
|
|
static listview_ctrl_t lv = NULL; |
95 |
|
|
static keylist_t list = NULL; |
96 |
|
|
static int enable = 0; |
97 |
twoaday |
25 |
gpg_keycache_t kc; |
98 |
twoaday |
23 |
gpgme_key_t *rset; |
99 |
twoaday |
2 |
gpgme_error_t err; |
100 |
twoaday |
24 |
gpgme_ctx_t ctx; |
101 |
twoaday |
2 |
passphrase_cb_s pwd; |
102 |
twoaday |
24 |
char *signer = NULL; |
103 |
twoaday |
2 |
int force_trust = 0, opt = 0; |
104 |
|
|
int kmode = reg_prefs.keylist_mode? KEYLIST_ENCRYPT_MIN : KEYLIST_ENCRYPT; |
105 |
twoaday |
23 |
int n; |
106 |
twoaday |
2 |
|
107 |
|
|
switch( msg ) { |
108 |
|
|
case WM_INITDIALOG: |
109 |
twoaday |
12 |
enable = 0; |
110 |
twoaday |
23 |
#ifndef LANG_DE |
111 |
twoaday |
2 |
SetWindowText( dlg, _("Sign & Encrypt") ); |
112 |
twoaday |
23 |
#endif |
113 |
twoaday |
2 |
kc = keycache_get_ctx( KEYCACHE_PUB ); |
114 |
|
|
if( !kc ) |
115 |
|
|
BUG( NULL ); |
116 |
|
|
lv = keylist_load( GetDlgItem( dlg, IDC_SIGNENC_KEYLIST ), |
117 |
twoaday |
23 |
kc, NULL, kmode, KEY_SORT_USERID); |
118 |
twoaday |
2 |
seclist_init( dlg, IDC_SIGNENC_SECLIST, KEYLIST_FLAG_SHORT, &list ); |
119 |
twoaday |
23 |
center_window (dlg, NULL); |
120 |
twoaday |
12 |
set_active_window (dlg); |
121 |
|
|
EnableWindow (GetDlgItem (dlg, IDC_SIGNENC_SECLIST), FALSE); |
122 |
twoaday |
2 |
SetDlgItemText (dlg, IDC_SIGNENC_SELKEY, _("Select key for signing")); |
123 |
|
|
SetDlgItemText (dlg, IDC_SIGNENC_SECLISTINF, _("Signing key:")); |
124 |
twoaday |
12 |
SetForegroundWindow (dlg); |
125 |
twoaday |
2 |
return TRUE; |
126 |
|
|
|
127 |
|
|
case WM_DESTROY: |
128 |
twoaday |
23 |
seclist_destroy (&list); |
129 |
|
|
reset_active_window (); |
130 |
|
|
if (lv) { |
131 |
twoaday |
2 |
keylist_delete( lv ); |
132 |
|
|
lv = NULL; |
133 |
|
|
} |
134 |
|
|
return FALSE; |
135 |
|
|
|
136 |
|
|
case WM_NOTIFY: |
137 |
|
|
NMHDR * notify; |
138 |
|
|
notify = (NMHDR *)lparam; |
139 |
|
|
if( notify && notify->code == NM_DBLCLK |
140 |
|
|
&& notify->idFrom == IDC_SIGNENC_KEYLIST ) |
141 |
|
|
PostMessage( dlg, WM_COMMAND, MAKEWPARAM(IDOK, 0), NULL ); |
142 |
|
|
return TRUE; |
143 |
|
|
|
144 |
|
|
case WM_SYSCOMMAND: |
145 |
|
|
if( LOWORD (wparam) == SC_CLOSE ) |
146 |
|
|
EndDialog( dlg, TRUE ); |
147 |
|
|
return FALSE; |
148 |
|
|
|
149 |
|
|
case WM_COMMAND: |
150 |
|
|
/* fixme: the enable seems to have a sync problem */ |
151 |
|
|
if( HIWORD( wparam ) == BN_CLICKED |
152 |
|
|
&& LOWORD( wparam ) == IDC_SIGNENC_SELKEY ) { |
153 |
|
|
enable ^= 1; |
154 |
|
|
EnableWindow( GetDlgItem( dlg, IDC_SIGNENC_SECLIST ), enable? TRUE : FALSE ); |
155 |
|
|
} |
156 |
|
|
switch( LOWORD( wparam ) ) { |
157 |
|
|
case IDOK: |
158 |
twoaday |
23 |
rset = keylist_get_recipients( lv, &force_trust, &n ); |
159 |
|
|
if (!n) { |
160 |
twoaday |
2 |
msg_box( dlg, _("You must select at least one key."), _("Sign & Encrypt"), MB_ERR ); |
161 |
|
|
return FALSE; |
162 |
|
|
} |
163 |
|
|
if( IsDlgButtonChecked( dlg, IDC_SIGNENC_SELKEY ) ) { |
164 |
|
|
gpgme_key_t key; |
165 |
|
|
const char * s; |
166 |
|
|
|
167 |
|
|
if( seclist_select_key( dlg, IDC_SIGNENC_SECLIST, &key ) ) { |
168 |
|
|
msg_box( dlg, _("No key was selected."), _("Signing"), MB_ERR ); |
169 |
|
|
return FALSE; |
170 |
|
|
} |
171 |
twoaday |
23 |
s = key->subkeys->keyid; |
172 |
|
|
if (s) |
173 |
|
|
signer = m_strdup(s+8); |
174 |
twoaday |
2 |
} |
175 |
|
|
else { |
176 |
twoaday |
23 |
signer = get_gnupg_default_key (); |
177 |
|
|
if (!signer) { |
178 |
|
|
msg_box (dlg, _("Could not get default key."), _("Signing"), MB_ERR); |
179 |
twoaday |
2 |
return FALSE; |
180 |
|
|
} |
181 |
|
|
} |
182 |
twoaday |
24 |
|
183 |
|
|
err = gpgme_new (&ctx); |
184 |
|
|
if (err) |
185 |
|
|
BUG (NULL); |
186 |
|
|
set_gpg_passphrase_cb (&pwd, ctx, GPG_CMD_SIGN, dlg, _("Sign & Encrypt")); |
187 |
|
|
err = gpg_clip_sign_encrypt (ctx, signer, rset, force_trust); |
188 |
twoaday |
23 |
release_gpg_passphrase_cb (&pwd); |
189 |
|
|
free (rset); |
190 |
|
|
free_if_alloc (signer); |
191 |
twoaday |
24 |
gpgme_release (ctx); |
192 |
|
|
if (gpgme_err_code (err) == GPG_ERR_BAD_PASSPHRASE) |
193 |
twoaday |
23 |
agent_del_cache (pwd.keyid); |
194 |
|
|
if (err) { |
195 |
|
|
msg_box (dlg, gpgme_strerror (err), _("Sign & Encrypt"), MB_ERR ); |
196 |
twoaday |
2 |
return FALSE; |
197 |
|
|
} |
198 |
twoaday |
23 |
show_msg (dlg, 1500, _("GnuPG Status: Finished")); |
199 |
|
|
EndDialog (dlg, TRUE); |
200 |
twoaday |
2 |
return TRUE; |
201 |
|
|
|
202 |
|
|
case IDCANCEL: |
203 |
|
|
EndDialog( dlg, FALSE ); |
204 |
|
|
return FALSE; |
205 |
|
|
} |
206 |
|
|
break; |
207 |
|
|
} |
208 |
|
|
|
209 |
|
|
return FALSE; |
210 |
twoaday |
23 |
} |