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