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