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 |
* |
* |
24 |
#include "../resource.h" |
#include "../resource.h" |
25 |
#include "wptErrors.h" |
#include "wptErrors.h" |
26 |
#include "wptAgent.h" |
#include "wptAgent.h" |
27 |
|
#include "wptCrypto.h" |
28 |
#include "wptGPG.h" |
#include "wptGPG.h" |
29 |
#include "wptCommonCtl.h" |
#include "wptCommonCtl.h" |
30 |
#include "wptKeylist.h" |
#include "wptKeylist.h" |
40 |
#include "wptUTF8.h" |
#include "wptUTF8.h" |
41 |
|
|
42 |
|
|
43 |
|
/* 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 |
|
gpg_clip_sign_encrypt (gpgme_ctx_t ctx, const char *signer, |
48 |
|
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 |
BOOL CALLBACK |
BOOL CALLBACK |
92 |
clip_signenc_dlg_proc( HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam ) |
clip_signenc_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
93 |
{ |
{ |
94 |
static listview_ctrl_t lv = NULL; |
static listview_ctrl_t lv = NULL; |
95 |
static keylist_t list = NULL; |
static keylist_t list = NULL; |
96 |
static int enable = 0; |
gpg_keycache_t kc; |
97 |
gpgme_ctx_t ctx; |
gpgme_key_t *rset; |
|
gpgme_keycache_t kc; |
|
|
gpgme_recipients_t rset; |
|
98 |
gpgme_error_t err; |
gpgme_error_t err; |
99 |
|
gpgme_ctx_t ctx; |
100 |
passphrase_cb_s pwd; |
passphrase_cb_s pwd; |
101 |
char * signer = NULL; |
char *signer = NULL; |
102 |
int force_trust = 0, opt = 0; |
int force_trust = 0, opt = 0; |
103 |
int kmode = reg_prefs.keylist_mode? KEYLIST_ENCRYPT_MIN : KEYLIST_ENCRYPT; |
int kmode = reg_prefs.keylist_mode? KEYLIST_ENCRYPT_MIN : KEYLIST_ENCRYPT; |
104 |
|
int n; |
105 |
|
|
106 |
switch( msg ) { |
switch( msg ) { |
107 |
case WM_INITDIALOG: |
case WM_INITDIALOG: |
108 |
#ifndef LANG_DE |
SetWindowText (dlg, _("Sign & Encrypt")); |
109 |
SetWindowText( dlg, _("Sign & Encrypt") ); |
|
|
#endif |
|
110 |
kc = keycache_get_ctx( KEYCACHE_PUB ); |
kc = keycache_get_ctx( KEYCACHE_PUB ); |
111 |
if( !kc ) |
if( !kc ) |
112 |
BUG( NULL ); |
BUG( NULL ); |
113 |
lv = keylist_load( GetDlgItem( dlg, IDC_SIGNENC_KEYLIST ), |
lv = keylist_load( GetDlgItem( dlg, IDC_SIGNENC_KEYLIST ), |
114 |
kc, NULL, kmode, GPGME_ATTR_USERID ); |
kc, NULL, kmode, KEY_SORT_USERID); |
115 |
seclist_init( dlg, IDC_SIGNENC_SECLIST, KEYLIST_FLAG_SHORT, &list ); |
seclist_init( dlg, IDC_SIGNENC_SECLIST, KEYLIST_FLAG_SHORT, &list ); |
116 |
center_window( dlg ); |
center_window (dlg, NULL); |
117 |
set_active_window( dlg ); |
set_active_window (dlg); |
118 |
EnableWindow( GetDlgItem( dlg, IDC_SIGNENC_SECLIST ), FALSE ); |
EnableWindow (GetDlgItem (dlg, IDC_SIGNENC_SECLIST), FALSE); |
119 |
SetDlgItemText (dlg, IDC_SIGNENC_SELKEY, _("Select key for signing")); |
SetDlgItemText (dlg, IDC_SIGNENC_SELKEY, _("Select key for signing")); |
120 |
SetDlgItemText (dlg, IDC_SIGNENC_SECLISTINF, _("Signing key:")); |
SetDlgItemText (dlg, IDC_SIGNENC_SECLISTINF, _("Signing key:")); |
121 |
SetForegroundWindow( dlg ); |
SetForegroundWindow (dlg); |
122 |
return TRUE; |
return TRUE; |
123 |
|
|
124 |
case WM_DESTROY: |
case WM_DESTROY: |
125 |
seclist_destroy( &list ); |
seclist_destroy (&list); |
126 |
reset_active_window( ); |
reset_active_window (); |
127 |
if( lv ) { |
if (lv) { |
128 |
keylist_delete( lv ); |
keylist_delete (lv); |
129 |
lv = NULL; |
lv = NULL; |
130 |
} |
} |
|
memset( pwd.pwd, 0, sizeof pwd.pwd ); |
|
131 |
return FALSE; |
return FALSE; |
132 |
|
|
133 |
case WM_NOTIFY: |
case WM_NOTIFY: |
134 |
NMHDR * notify; |
NMHDR * notify; |
135 |
notify = (NMHDR *)lparam; |
notify = (NMHDR *)lparam; |
136 |
if( notify && notify->code == NM_DBLCLK |
if (notify && notify->code == NM_DBLCLK |
137 |
&& notify->idFrom == IDC_SIGNENC_KEYLIST ) |
&& notify->idFrom == IDC_SIGNENC_KEYLIST) |
138 |
PostMessage( dlg, WM_COMMAND, MAKEWPARAM(IDOK, 0), NULL ); |
PostMessage (dlg, WM_COMMAND, MAKEWPARAM (IDOK, 0), NULL); |
139 |
return TRUE; |
return TRUE; |
140 |
|
|
141 |
case WM_SYSCOMMAND: |
case WM_SYSCOMMAND: |
144 |
return FALSE; |
return FALSE; |
145 |
|
|
146 |
case WM_COMMAND: |
case WM_COMMAND: |
147 |
/* fixme: the enable seems to have a sync problem */ |
if (HIWORD (wparam) == BN_CLICKED |
148 |
if( HIWORD( wparam ) == BN_CLICKED |
&& LOWORD (wparam) == IDC_SIGNENC_SELKEY) { |
149 |
&& LOWORD( wparam ) == IDC_SIGNENC_SELKEY ) { |
int enable = IsDlgButtonChecked (dlg, IDC_SIGNENC_SELKEY); |
150 |
enable ^= 1; |
EnableWindow (GetDlgItem (dlg, IDC_SIGNENC_SECLIST), enable? TRUE : FALSE); |
|
EnableWindow( GetDlgItem( dlg, IDC_SIGNENC_SECLIST ), enable? TRUE : FALSE ); |
|
151 |
} |
} |
152 |
switch( LOWORD( wparam ) ) { |
switch( LOWORD( wparam ) ) { |
153 |
case IDOK: |
case IDOK: |
154 |
rset = keylist_get_recipients( lv, &force_trust, NULL ); |
rset = keylist_get_recipients( lv, &force_trust, &n ); |
155 |
if( !gpgme_recipients_count( rset ) ) { |
if (!n) { |
156 |
msg_box( dlg, _("You must select at least one key."), _("Sign & Encrypt"), MB_ERR ); |
msg_box( dlg, _("You must select at least one key."), _("Sign & Encrypt"), MB_ERR ); |
|
gpgme_recipients_release( rset ); |
|
157 |
return FALSE; |
return FALSE; |
158 |
} |
} |
159 |
if( IsDlgButtonChecked( dlg, IDC_SIGNENC_SELKEY ) ) { |
if( IsDlgButtonChecked( dlg, IDC_SIGNENC_SELKEY ) ) { |
164 |
msg_box( dlg, _("No key was selected."), _("Signing"), MB_ERR ); |
msg_box( dlg, _("No key was selected."), _("Signing"), MB_ERR ); |
165 |
return FALSE; |
return FALSE; |
166 |
} |
} |
167 |
s = gpgme_key_get_string_attr( key, GPGME_ATTR_KEYID, NULL, 0 ); |
s = key->subkeys->keyid; |
168 |
if( s ) |
if (s) |
169 |
signer = m_strdup( s+8 ); |
signer = m_strdup(s+8); |
170 |
} |
} |
171 |
else { |
else { |
172 |
signer = get_gnupg_default_key( ); |
signer = get_gnupg_default_key (); |
173 |
if( !signer ) { |
if (!signer) { |
174 |
msg_box( dlg, _("Could not get default key."), _("Signing"), MB_ERR ); |
msg_box (dlg, _("Could not get default key."), _("Signing"), MB_ERR); |
175 |
return FALSE; |
return FALSE; |
176 |
} |
} |
177 |
} |
} |
178 |
memset( &pwd, 0, sizeof pwd ); |
|
179 |
err = gpgme_new( &ctx ); |
err = gpgme_new (&ctx); |
180 |
if( err ) |
if (err) |
181 |
BUG( dlg ); |
BUG (NULL); |
182 |
gpgme_enable_logging( ctx ); |
set_gpg_passphrase_cb (&pwd, ctx, GPG_CMD_SIGN, dlg, _("Sign & Encrypt")); |
183 |
set_gpg_passphrase_cb( ctx, &pwd, GPG_CMD_SIGN, dlg, _("Sign & Encrypt") ); |
err = gpg_clip_sign_encrypt (ctx, signer, rset, force_trust); |
184 |
if( force_trust ) |
release_gpg_passphrase_cb (&pwd); |
185 |
opt |= GPGME_CTRL_FORCETRUST; |
free (rset); |
186 |
err = gpgme_op_clip_sign_encrypt( ctx, rset, signer, opt ); |
free_if_alloc (signer); |
187 |
memset( pwd.pwd, 0, sizeof pwd.pwd ); |
gpgme_release (ctx); |
188 |
gpgme_recipients_release( rset ); |
if (gpgme_err_code (err) == GPG_ERR_BAD_PASSPHRASE) |
189 |
free_if_alloc( signer ); |
agent_del_cache (pwd.keyid); |
190 |
if( err == GPGME_Bad_Passphrase ) |
if (err) { |
191 |
agent_del_cache( pwd.keyid ); |
msg_box (dlg, gpgme_strerror (err), _("Sign & Encrypt"), MB_ERR ); |
|
if( err ) { |
|
|
gpgme_show_error( dlg, err, ctx, _("Sign & Encrypt"), MB_ERR ); |
|
|
gpgme_release( ctx ); |
|
192 |
return FALSE; |
return FALSE; |
193 |
} |
} |
194 |
gpgme_release( ctx ); |
show_msg (dlg, 1500, _("GnuPG Status: Finished")); |
195 |
show_msg( dlg, 1500, _("GnuPG Status: Finished") ); |
EndDialog (dlg, TRUE); |
|
EndDialog( dlg, TRUE ); |
|
196 |
return TRUE; |
return TRUE; |
197 |
|
|
198 |
case IDCANCEL: |
case IDCANCEL: |
203 |
} |
} |
204 |
|
|
205 |
return FALSE; |
return FALSE; |
206 |
} /* clip_signenc_dlg_proc */ |
} |