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