1 |
/* wptClipSignDlg.cpp - WinPT clipboard sign dialog |
/* wptClipSignDlg.cpp - WinPT clipboard sign dialog |
2 |
* Copyright (C) 2000, 2001, 2002, 2003 Timo Schulz |
* Copyright (C) 2000, 2001, 2002, 2003, 2005 Timo Schulz |
3 |
|
* Copyright (C) 2005 g10 Code GmbH |
4 |
* |
* |
5 |
* This file is part of WinPT. |
* This file is part of WinPT. |
6 |
* |
* |
26 |
#include "wptTypes.h" |
#include "wptTypes.h" |
27 |
#include "wptAgent.h" |
#include "wptAgent.h" |
28 |
#include "wptNLS.h" |
#include "wptNLS.h" |
29 |
|
#include "wptCrypto.h" |
30 |
#include "wptGPG.h" |
#include "wptGPG.h" |
31 |
#include "wptCommonCtl.h" |
#include "wptCommonCtl.h" |
32 |
#include "wptRegistry.h" |
#include "wptRegistry.h" |
38 |
#include "wptDlgs.h" |
#include "wptDlgs.h" |
39 |
|
|
40 |
|
|
41 |
|
/* Sign the clipboard contents with the key @keyid and wrap |
42 |
|
text lines to @wraplen (0 disable line wrapping). |
43 |
|
Return value: 0 on success. */ |
44 |
|
gpgme_error_t |
45 |
|
gpg_clip_sign (gpgme_ctx_t ctx, const char *keyid, int wraplen) |
46 |
|
{ |
47 |
|
gpgme_error_t err; |
48 |
|
gpgme_data_t plain = NULL; |
49 |
|
gpgme_data_t sig = NULL; |
50 |
|
gpgme_key_t key = NULL; |
51 |
|
|
52 |
|
if (!keyid) |
53 |
|
return gpg_error (GPG_ERR_INV_ARG); |
54 |
|
|
55 |
|
gpgme_set_armor (ctx, 1); |
56 |
|
|
57 |
|
err = gpg_data_new_from_clipboard (&plain, wraplen); |
58 |
|
if (err) |
59 |
|
return err; |
60 |
|
|
61 |
|
get_pubkey (keyid, &key); |
62 |
|
if (key) |
63 |
|
err = gpgme_signers_add (ctx, key); |
64 |
|
else { |
65 |
|
err = gpg_error (GPG_ERR_NO_PUBKEY); |
66 |
|
goto leave; |
67 |
|
} |
68 |
|
err = gpgme_data_new (&sig); |
69 |
|
if (err) |
70 |
|
goto leave; |
71 |
|
err = gpgme_op_sign (ctx, plain, sig, GPGME_SIG_MODE_CLEAR); |
72 |
|
if (err) |
73 |
|
goto leave; |
74 |
|
|
75 |
|
gpg_data_release_and_set_clipboard (sig, 1); |
76 |
|
sig = NULL; |
77 |
|
|
78 |
|
leave: |
79 |
|
if (plain) |
80 |
|
gpgme_data_release (plain); |
81 |
|
if (sig) |
82 |
|
gpgme_data_release (sig); |
83 |
|
return err; |
84 |
|
} |
85 |
|
|
86 |
|
|
87 |
/* This function is used when only one secret key is available. |
/* This function is used when only one secret key is available. |
88 |
* it doesn't make sense to offer a dialog for this case. |
* it doesn't make sense to offer a dialog for this case. |
89 |
*/ |
*/ |
94 |
gpgme_ctx_t ctx; |
gpgme_ctx_t ctx; |
95 |
gpgme_error_t err; |
gpgme_error_t err; |
96 |
passphrase_cb_s pwd; |
passphrase_cb_s pwd; |
97 |
int rc = 0, n = reg_prefs.word_wrap; |
int rc = 0; |
98 |
|
int n = reg_prefs.word_wrap; |
99 |
|
|
100 |
signer = get_gnupg_default_key (); |
signer = get_gnupg_default_key (); |
101 |
if( !signer ) |
if (!signer) { |
102 |
{ |
msg_box (dlg, _("Could not get default key."), _("Signing"), MB_ERR); |
|
msg_box( dlg, _("Could not get default key."), _("Signing"), MB_ERR ); |
|
103 |
return; |
return; |
104 |
} |
} |
105 |
|
|
106 |
err = gpgme_new( &ctx ); |
err = gpgme_new (&ctx); |
107 |
if( err ) |
if (err) |
108 |
BUG( dlg ); |
BUG (dlg); |
109 |
gpgme_enable_logging( ctx ); |
|
110 |
set_gpg_passphrase_cb( ctx, &pwd, GPG_CMD_SIGN, dlg, _("Signing") ); |
set_gpg_passphrase_cb (&pwd, ctx, GPG_CMD_SIGN, dlg, _("Signing")); |
111 |
err = gpgme_op_clip_sign( ctx, GPGME_SIG_MODE_CLEAR, signer, n ); |
err = gpg_clip_sign (ctx, signer, n ); |
112 |
memset( pwd.pwd, 0, sizeof pwd.pwd ); |
wipememory (pwd.pwd, sizeof (pwd.pwd)); |
113 |
if( err == GPGME_Bad_Passphrase ) |
if (gpgme_err_code (err) == GPG_ERR_BAD_PASSPHRASE) |
114 |
agent_del_cache( pwd.keyid ); |
agent_del_cache (pwd.keyid); |
115 |
|
if (err) |
116 |
if( err ) |
msg_box (dlg, gpgme_strerror (err), _("Signing"), MB_ERR); |
|
gpgme_show_error( dlg, err, ctx, _("Signing"), MB_ERR ); |
|
117 |
else |
else |
118 |
show_msg( dlg, 1500, _("GnuPG Status: Finished") ); |
show_msg (dlg, 1500, _("GnuPG Status: Finished")); |
119 |
gpgme_release( ctx ); |
gpgme_release (ctx); |
120 |
free_if_alloc( signer ); |
free_if_alloc (signer); |
121 |
} /* one_key_proc */ |
} |
122 |
|
|
123 |
|
|
124 |
|
/* Dialog box procedure for clipboard signing. */ |
125 |
BOOL CALLBACK |
BOOL CALLBACK |
126 |
clip_sign_dlg_proc( HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam ) |
clip_sign_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
127 |
{ |
{ |
128 |
static listview_ctrl_t lv = NULL; |
static listview_ctrl_t lv = NULL; |
129 |
gpgme_keycache_t kc, sec_kc; |
gpg_keycache_t kc, sec_kc; |
130 |
gpgme_ctx_t ctx; |
gpgme_ctx_t ctx; |
131 |
gpgme_error_t err; |
gpgme_error_t err; |
132 |
passphrase_cb_s pwd; |
passphrase_cb_s pwd; |
136 |
|
|
137 |
switch( msg ) { |
switch( msg ) { |
138 |
case WM_INITDIALOG: |
case WM_INITDIALOG: |
139 |
#ifndef LANG_DE |
#ifndef LANG_DE |
140 |
SetWindowText( dlg, _("Signing") ); |
SetWindowText( dlg, _("Signing") ); |
141 |
#endif |
#endif |
142 |
kc = keycache_get_ctx( KEYCACHE_PUB ); |
kc = keycache_get_ctx (KEYCACHE_PUB); |
143 |
if( !kc ) |
if (!kc) |
144 |
BUG( NULL ); |
BUG( NULL ); |
145 |
sec_kc = keycache_get_ctx( KEYCACHE_PRV ); |
sec_kc = keycache_get_ctx (KEYCACHE_PRV); |
146 |
if( !sec_kc ) |
if (!sec_kc) |
147 |
BUG( dlg ); |
BUG (dlg); |
148 |
if( gpgme_keycache_count( sec_kc ) == 1 ) { |
if (gpg_keycache_get_size (sec_kc) == 1) { |
149 |
one_key_proc( dlg ); |
one_key_proc (dlg); |
150 |
EndDialog( dlg, TRUE ); |
EndDialog (dlg, TRUE); |
151 |
return FALSE; |
return FALSE; |
152 |
} |
} |
153 |
lv = keylist_load( GetDlgItem( dlg, IDC_SIGN_KEYLIST ), kc, sec_kc, |
lv = keylist_load (GetDlgItem (dlg, IDC_SIGN_KEYLIST), kc, sec_kc, |
154 |
KEYLIST_SIGN, GPGME_ATTR_USERID ); |
KEYLIST_SIGN, KEY_SORT_USERID); |
155 |
center_window( dlg ); |
center_window (dlg, NULL); |
156 |
SetForegroundWindow( dlg ); |
SetForegroundWindow (dlg); |
157 |
set_active_window( dlg ); |
set_active_window (dlg); |
158 |
return FALSE; |
return FALSE; |
159 |
|
|
160 |
case WM_DESTROY: |
case WM_DESTROY: |
161 |
reset_active_window(); |
reset_active_window (); |
162 |
if( lv ) { |
if (lv) { |
163 |
keylist_delete( lv ); |
keylist_delete (lv); |
164 |
lv = NULL; |
lv = NULL; |
165 |
} |
} |
|
memset( pwd.pwd, 0, sizeof pwd.pwd ); |
|
166 |
return FALSE; |
return FALSE; |
167 |
|
|
168 |
case WM_NOTIFY: |
case WM_NOTIFY: |
179 |
return FALSE; |
return FALSE; |
180 |
|
|
181 |
case WM_COMMAND: |
case WM_COMMAND: |
182 |
switch ( LOWORD( wparam ) ) |
switch (LOWORD (wparam)) { |
|
{ |
|
183 |
case IDOK: |
case IDOK: |
184 |
signer = get_gnupg_default_key (); |
signer = get_gnupg_default_key (); |
185 |
if( !signer ) |
if (!signer) { |
|
{ |
|
186 |
msg_box( dlg, _("Could not get default key."), _("Signing"), MB_ERR ); |
msg_box( dlg, _("Could not get default key."), _("Signing"), MB_ERR ); |
187 |
return FALSE; |
return FALSE; |
188 |
} |
} |
189 |
if( listview_count_items( lv, 0 ) == 1 ) |
if (listview_count_items (lv, 0) == 1) { |
190 |
{ |
listview_get_item_text (lv, 0, 1, signer, sizeof signer-1); |
|
listview_get_item_text( lv, 0, 1, signer, sizeof signer-1 ); |
|
191 |
no_signer = 0; |
no_signer = 0; |
192 |
} |
} |
193 |
else if( (lv_idx = listview_get_curr_pos( lv )) == -1 ) |
else if ((lv_idx = listview_get_curr_pos (lv)) == -1) { |
194 |
{ |
rc = log_box (_("Signing"), MB_YESNO, |
|
rc = log_box( _("Signing"), MB_YESNO, |
|
195 |
_("No key was chosen.\nUse the GPG default key '%s'?"), |
_("No key was chosen.\nUse the GPG default key '%s'?"), |
196 |
signer ); |
signer); |
197 |
if( rc == IDNO ) |
if (rc == IDNO) { |
|
{ |
|
198 |
free_if_alloc (signer); |
free_if_alloc (signer); |
199 |
return FALSE; |
return FALSE; |
200 |
} |
} |
201 |
no_signer = 1; |
no_signer = 1; |
202 |
} |
} |
203 |
if (!no_signer) |
if (!no_signer) { |
|
{ |
|
204 |
free_if_alloc (signer); |
free_if_alloc (signer); |
205 |
signer = new char[32+1]; |
signer = new char[32+1]; |
206 |
if (!signer) |
if (!signer) |
208 |
listview_get_item_text (lv, lv_idx, 1, signer, 32); |
listview_get_item_text (lv, lv_idx, 1, signer, 32); |
209 |
} |
} |
210 |
err = gpgme_new (&ctx); |
err = gpgme_new (&ctx); |
211 |
if( err ) |
if (err) |
212 |
BUG( dlg ); |
BUG (dlg); |
213 |
gpgme_enable_logging( ctx ); |
set_gpg_passphrase_cb (&pwd, ctx, GPG_CMD_SIGN, dlg, _("Signing")); |
214 |
set_gpg_passphrase_cb( ctx, &pwd, GPG_CMD_SIGN, dlg, _("Signing") ); |
err = gpg_clip_sign (ctx, signer, reg_prefs.word_wrap); |
215 |
err = gpgme_op_clip_sign( ctx, GPGME_SIG_MODE_CLEAR, signer, |
free_if_alloc (signer); |
216 |
reg_prefs.word_wrap ); |
release_gpg_passphrase_cb (&pwd); |
217 |
free_if_alloc( signer ); |
|
218 |
memset( pwd.pwd, 0, sizeof pwd.pwd ); |
if (pwd.cancel && gpgme_err_code(err) == GPG_ERR_BAD_PASSPHRASE) { |
219 |
if( pwd.cancel && err == GPGME_Bad_Passphrase ) { /* The user hit the cancel button */ |
/* The user hit the cancel button or bad passphrase */ |
220 |
gpgme_release( ctx ); |
gpgme_release (ctx); |
221 |
EndDialog( dlg, TRUE ); |
EndDialog (dlg, TRUE); |
222 |
return TRUE; |
return TRUE; |
223 |
} |
} |
224 |
if( err ) { |
if (err) { |
225 |
gpgme_show_error( dlg, err, ctx, _("Signing"), MB_ERR ); |
msg_box (dlg, gpgme_strerror (err), _("Signing"), MB_ERR); |
226 |
gpgme_release( ctx ); |
gpgme_release (ctx); |
227 |
return FALSE; |
return FALSE; |
228 |
} |
} |
229 |
else |
else |
230 |
show_msg( dlg, 1500, _("GnuPG Status: Finished") ); |
show_msg( dlg, 1500, _("GnuPG Status: Finished") ); |
231 |
gpgme_release( ctx ); |
gpgme_release (ctx); |
232 |
EndDialog( dlg, TRUE ); |
EndDialog (dlg, TRUE); |
233 |
return TRUE; |
return TRUE; |
234 |
|
|
235 |
case IDCANCEL: |
case IDCANCEL: |
236 |
EndDialog( dlg, FALSE ); |
EndDialog (dlg, FALSE); |
237 |
return FALSE; |
return FALSE; |
238 |
} |
} |
239 |
break; |
break; |
240 |
} |
} |
241 |
|
|
242 |
return FALSE; |
return FALSE; |
243 |
} /* clip_sign_dlg_proc */ |
} |
244 |
|
|