1 |
twoaday |
2 |
/* wptSignEncDlg.cpp - Sign & encrypt dialog |
2 |
|
|
* Copyright (C) 2000-2004 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 |
|
|
|
21 |
|
|
#include <windows.h> |
22 |
|
|
#include <commctrl.h> |
23 |
|
|
|
24 |
|
|
#include "../resource.h" |
25 |
|
|
#include "wptErrors.h" |
26 |
|
|
#include "wptAgent.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 |
|
|
BOOL CALLBACK |
43 |
|
|
clip_signenc_dlg_proc( HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam ) |
44 |
|
|
{ |
45 |
|
|
static listview_ctrl_t lv = NULL; |
46 |
|
|
static keylist_t list = NULL; |
47 |
|
|
static int enable = 0; |
48 |
|
|
gpgme_ctx_t ctx; |
49 |
|
|
gpgme_keycache_t kc; |
50 |
|
|
gpgme_recipients_t rset; |
51 |
|
|
gpgme_error_t err; |
52 |
|
|
passphrase_cb_s pwd; |
53 |
|
|
char * signer = NULL; |
54 |
|
|
int force_trust = 0, opt = 0; |
55 |
|
|
int kmode = reg_prefs.keylist_mode? KEYLIST_ENCRYPT_MIN : KEYLIST_ENCRYPT; |
56 |
|
|
|
57 |
|
|
switch( msg ) { |
58 |
|
|
case WM_INITDIALOG: |
59 |
|
|
#ifndef LANG_DE |
60 |
|
|
SetWindowText( dlg, _("Sign & Encrypt") ); |
61 |
|
|
#endif |
62 |
|
|
kc = keycache_get_ctx( KEYCACHE_PUB ); |
63 |
|
|
if( !kc ) |
64 |
|
|
BUG( NULL ); |
65 |
|
|
lv = keylist_load( GetDlgItem( dlg, IDC_SIGNENC_KEYLIST ), |
66 |
|
|
kc, NULL, kmode, GPGME_ATTR_USERID ); |
67 |
|
|
seclist_init( dlg, IDC_SIGNENC_SECLIST, KEYLIST_FLAG_SHORT, &list ); |
68 |
|
|
center_window( dlg ); |
69 |
|
|
set_active_window( dlg ); |
70 |
|
|
EnableWindow( GetDlgItem( dlg, IDC_SIGNENC_SECLIST ), FALSE ); |
71 |
|
|
SetDlgItemText (dlg, IDC_SIGNENC_SELKEY, _("Select key for signing")); |
72 |
|
|
SetDlgItemText (dlg, IDC_SIGNENC_SECLISTINF, _("Signing key:")); |
73 |
|
|
SetForegroundWindow( dlg ); |
74 |
|
|
return TRUE; |
75 |
|
|
|
76 |
|
|
case WM_DESTROY: |
77 |
|
|
seclist_destroy( &list ); |
78 |
|
|
reset_active_window( ); |
79 |
|
|
if( lv ) { |
80 |
|
|
keylist_delete( lv ); |
81 |
|
|
lv = NULL; |
82 |
|
|
} |
83 |
|
|
memset( pwd.pwd, 0, sizeof pwd.pwd ); |
84 |
|
|
return FALSE; |
85 |
|
|
|
86 |
|
|
case WM_NOTIFY: |
87 |
|
|
NMHDR * notify; |
88 |
|
|
notify = (NMHDR *)lparam; |
89 |
|
|
if( notify && notify->code == NM_DBLCLK |
90 |
|
|
&& notify->idFrom == IDC_SIGNENC_KEYLIST ) |
91 |
|
|
PostMessage( dlg, WM_COMMAND, MAKEWPARAM(IDOK, 0), NULL ); |
92 |
|
|
return TRUE; |
93 |
|
|
|
94 |
|
|
case WM_SYSCOMMAND: |
95 |
|
|
if( LOWORD (wparam) == SC_CLOSE ) |
96 |
|
|
EndDialog( dlg, TRUE ); |
97 |
|
|
return FALSE; |
98 |
|
|
|
99 |
|
|
case WM_COMMAND: |
100 |
|
|
/* fixme: the enable seems to have a sync problem */ |
101 |
|
|
if( HIWORD( wparam ) == BN_CLICKED |
102 |
|
|
&& LOWORD( wparam ) == IDC_SIGNENC_SELKEY ) { |
103 |
|
|
enable ^= 1; |
104 |
|
|
EnableWindow( GetDlgItem( dlg, IDC_SIGNENC_SECLIST ), enable? TRUE : FALSE ); |
105 |
|
|
} |
106 |
|
|
switch( LOWORD( wparam ) ) { |
107 |
|
|
case IDOK: |
108 |
|
|
rset = keylist_get_recipients( lv, &force_trust, NULL ); |
109 |
|
|
if( !gpgme_recipients_count( rset ) ) { |
110 |
|
|
msg_box( dlg, _("You must select at least one key."), _("Sign & Encrypt"), MB_ERR ); |
111 |
|
|
gpgme_recipients_release( rset ); |
112 |
|
|
return FALSE; |
113 |
|
|
} |
114 |
|
|
if( IsDlgButtonChecked( dlg, IDC_SIGNENC_SELKEY ) ) { |
115 |
|
|
gpgme_key_t key; |
116 |
|
|
const char * s; |
117 |
|
|
|
118 |
|
|
if( seclist_select_key( dlg, IDC_SIGNENC_SECLIST, &key ) ) { |
119 |
|
|
msg_box( dlg, _("No key was selected."), _("Signing"), MB_ERR ); |
120 |
|
|
return FALSE; |
121 |
|
|
} |
122 |
|
|
s = gpgme_key_get_string_attr( key, GPGME_ATTR_KEYID, NULL, 0 ); |
123 |
|
|
if( s ) |
124 |
|
|
signer = m_strdup( s+8 ); |
125 |
|
|
} |
126 |
|
|
else { |
127 |
|
|
signer = get_gnupg_default_key( ); |
128 |
|
|
if( !signer ) { |
129 |
|
|
msg_box( dlg, _("Could not get default key."), _("Signing"), MB_ERR ); |
130 |
|
|
return FALSE; |
131 |
|
|
} |
132 |
|
|
} |
133 |
|
|
memset( &pwd, 0, sizeof pwd ); |
134 |
|
|
err = gpgme_new( &ctx ); |
135 |
|
|
if( err ) |
136 |
|
|
BUG( dlg ); |
137 |
|
|
gpgme_enable_logging( ctx ); |
138 |
|
|
set_gpg_passphrase_cb( ctx, &pwd, GPG_CMD_SIGN, dlg, _("Sign & Encrypt") ); |
139 |
|
|
if( force_trust ) |
140 |
|
|
opt |= GPGME_CTRL_FORCETRUST; |
141 |
|
|
err = gpgme_op_clip_sign_encrypt( ctx, rset, signer, opt ); |
142 |
|
|
memset( pwd.pwd, 0, sizeof pwd.pwd ); |
143 |
|
|
gpgme_recipients_release( rset ); |
144 |
|
|
free_if_alloc( signer ); |
145 |
|
|
if( err == GPGME_Bad_Passphrase ) |
146 |
|
|
agent_del_cache( pwd.keyid ); |
147 |
|
|
if( err ) { |
148 |
|
|
gpgme_show_error( dlg, err, ctx, _("Sign & Encrypt"), MB_ERR ); |
149 |
|
|
gpgme_release( ctx ); |
150 |
|
|
return FALSE; |
151 |
|
|
} |
152 |
|
|
gpgme_release( ctx ); |
153 |
|
|
show_msg( dlg, 1500, _("GnuPG Status: Finished") ); |
154 |
|
|
EndDialog( dlg, TRUE ); |
155 |
|
|
return TRUE; |
156 |
|
|
|
157 |
|
|
case IDCANCEL: |
158 |
|
|
EndDialog( dlg, FALSE ); |
159 |
|
|
return FALSE; |
160 |
|
|
} |
161 |
|
|
break; |
162 |
|
|
} |
163 |
|
|
|
164 |
|
|
return FALSE; |
165 |
|
|
} /* clip_signenc_dlg_proc */ |