1 |
/* wptFileManager.cpp - File Manager routines |
/* wptFileManager.cpp - File Manager routines |
2 |
* Copyright (C) 2001-2005 Timo Schulz |
* Copyright (C) 2001-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 |
* |
* |
43 |
#include "wptGPGZIP.h" |
#include "wptGPGZIP.h" |
44 |
#include "wptUTF8.h" |
#include "wptUTF8.h" |
45 |
#include "wptRegistry.h" |
#include "wptRegistry.h" |
46 |
|
#include "wptImport.h" |
47 |
|
|
48 |
#include "openpgp.h" |
#include "openpgp.h" |
49 |
|
|
50 |
int algo_from_list (gpgme_recipients_t rset, const char * keyid); |
void progress_cleanup (progress_filter_s *pfx); |
51 |
void progress_cleanup (progress_filter_s * pfx); |
|
52 |
|
char* gpg_keylist_to_pattern (gpgme_key_t *rset, int n); |
53 |
|
gpgme_error_t sym_passphrase_cb (void *hook, const char *hint, const char *pass_inf, |
54 |
|
int prev_was_bad, int fd); |
55 |
|
|
56 |
/*-- wptFileVerifyDlg.cpp --*/ |
/*-- wptFileVerifyDlg.cpp --*/ |
57 |
int file_verify_add_state (siglog_context_t c); |
int file_verify_add_state (file_sig_ctx_t c); |
58 |
void file_verify_use_event (void); |
void file_verify_use_event (void); |
59 |
void file_verify_wait (void); |
void file_verify_wait (void); |
60 |
|
|
62 |
".mp3", ".wav", ".mid", ".wma", |
".mp3", ".wav", ".mid", ".wma", |
63 |
".gif", ".jpg", ".png", ".jpeg", ".dib", 0}; |
".gif", ".jpg", ".png", ".jpeg", ".dib", 0}; |
64 |
|
|
65 |
|
|
66 |
char * |
char * |
67 |
fm_quote_file (const char * name) |
fm_quote_file (const char * name) |
68 |
{ |
{ |
86 |
int id; |
int id; |
87 |
|
|
88 |
if (file_exist_check (fname)) |
if (file_exist_check (fname)) |
89 |
return 1; |
return -1; |
90 |
id = log_box (_("File Manager"), MB_YESNO, |
id = log_box (_("File Manager"), MB_YESNO, |
91 |
_("\"%s\" already exists.\n" |
_("\"%s\" already exists.\n" |
92 |
"Replace existing file?"), fname); |
"Replace existing file?"), fname); |
93 |
return id == IDNO ? 0 : 1; |
return id == IDNO ? 0 : -1; |
94 |
} /* overwrite_file */ |
} /* overwrite_file */ |
95 |
|
|
96 |
|
|
169 |
|
|
170 |
|
|
171 |
const char* |
const char* |
172 |
file_get_extension (gpgme_ctx_t ctx, gpgme_sigmode_t sigmode) |
file_get_extension (gpgme_ctx_t ctx, gpgme_sig_mode_t sigmode) |
173 |
{ |
{ |
174 |
int use_armor = (int)gpgme_control (ctx, GPGME_CTRL_ARMOR, -1); |
int use_armor = gpgme_get_armor (ctx); |
175 |
|
|
176 |
if (use_armor || sigmode == GPGME_SIG_MODE_CLEAR) |
if (use_armor || sigmode == GPGME_SIG_MODE_CLEAR) |
177 |
return ".asc"; |
return ".asc"; |
227 |
BUG (0); |
BUG (0); |
228 |
memset (c, 0, sizeof * c); |
memset (c, 0, sizeof * c); |
229 |
rc = gpgme_new (&c->ctx); |
rc = gpgme_new (&c->ctx); |
|
if (!rc) |
|
|
rc = gpgme_recipients_new (&c->recp); |
|
230 |
if (rc) |
if (rc) |
231 |
BUG (0); |
BUG (0); |
232 |
gpgme_set_comment (c->ctx, "Generated by WinPT "PGM_VERSION); |
/* XXX rc = gpgme_recipients_new (&c->recp);*/ |
233 |
|
/* XXX gpgme_set_comment (c->ctx, "Generated by WinPT "PGM_VERSION); */ |
234 |
*ctx = c; |
*ctx = c; |
235 |
return 0; |
return 0; |
236 |
} /* fm_state_new */ |
} /* fm_state_new */ |
237 |
|
|
238 |
|
|
239 |
|
/* Release the FM state handle @c. */ |
240 |
void |
void |
241 |
fm_state_release (fm_state_t c) |
fm_state_release (fm_state_t c) |
242 |
{ |
{ |
243 |
if (c) |
if (!c) |
244 |
{ |
return; |
245 |
if (c->recp) |
if (c->recp) |
246 |
{ |
free (c->recp); |
247 |
gpgme_recipients_release (c->recp); |
if (c->ctx) { |
248 |
c->recp = NULL; |
gpgme_release (c->ctx); |
249 |
} |
c->ctx = NULL; |
|
if (c->ctx) |
|
|
{ |
|
|
gpgme_release (c->ctx); |
|
|
c->ctx = NULL; |
|
|
} |
|
|
free_if_alloc (c->opaque); |
|
|
free_if_alloc (c->output); |
|
|
delete c; c = NULL; |
|
250 |
} |
} |
251 |
} /* fm_state_release */ |
free_if_alloc (c->opaque); |
252 |
|
free_if_alloc (c->output); |
253 |
|
delete c; c = NULL; |
254 |
|
} |
255 |
|
|
256 |
static int |
static int |
257 |
fm_check_for_entry( listview_ctrl_t lv, const char *file ) |
fm_check_for_entry( listview_ctrl_t lv, const char *file ) |
318 |
} |
} |
319 |
|
|
320 |
|
|
321 |
|
/* Add the drag & drop files from @dd_files to the |
322 |
|
list view control @lv. */ |
323 |
int |
int |
324 |
fm_add_dropped_files (listview_ctrl_t lv, HDROP dd_files) |
fm_add_dropped_files (listview_ctrl_t lv, HDROP dd_files) |
325 |
{ |
{ |
326 |
char name[384+4]; |
char name[384+4]; |
327 |
int nfiles, rc, i; |
int nfiles, rc, i; |
328 |
|
|
329 |
memset( name, 0, sizeof (name) ); |
memset (name, 0, sizeof (name)); |
330 |
nfiles = DragQueryFile( dd_files, 0xFFFFFFFF, NULL, 0 ); |
nfiles = DragQueryFile (dd_files, 0xFFFFFFFF, NULL, 0); |
331 |
for (i = 0; i < nfiles; i++) { |
for (i = 0; i < nfiles; i++) { |
332 |
DragQueryFile (dd_files, i, name, sizeof (name) -1); |
DragQueryFile (dd_files, i, name, sizeof (name) -1); |
333 |
if (is_directory (name)) |
if (is_directory (name)) |
336 |
rc = fm_set_ftype (lv, name); |
rc = fm_set_ftype (lv, name); |
337 |
if (rc == -1) |
if (rc == -1) |
338 |
break; |
break; |
|
|
|
339 |
} |
} |
340 |
return rc; |
return rc; |
341 |
} /* fm_add_dropped_files */ |
} |
342 |
|
|
343 |
|
|
344 |
int |
int |
353 |
open.lStructSize = sizeof (OPENFILENAME); |
open.lStructSize = sizeof (OPENFILENAME); |
354 |
open.hInstance = glob_hinst; |
open.hInstance = glob_hinst; |
355 |
open.lpstrTitle = _("File Open"); |
open.lpstrTitle = _("File Open"); |
356 |
open.lpstrFilter = _("All Files (*.*)\0*.*"); |
open.lpstrFilter = _("All Files (*.*)\0*.*\0\0"); |
357 |
open.hwndOwner = dlg; |
open.hwndOwner = dlg; |
358 |
open.lpstrFile = file; |
open.lpstrFile = file; |
359 |
open.nMaxFile = sizeof (file) - 1; |
open.nMaxFile = sizeof (file) - 1; |
373 |
} |
} |
374 |
|
|
375 |
return rc; |
return rc; |
376 |
} /* fm_add_opened_files */ |
} |
377 |
|
|
378 |
|
|
379 |
|
/* Check the armor type of the file @fname and return |
380 |
|
a string representation of it. */ |
381 |
static const char * |
static const char * |
382 |
fm_check_armor_type (const char * fname) |
fm_check_armor_type (const char *fname) |
383 |
{ |
{ |
384 |
FILE * fp; |
FILE * fp; |
385 |
char header[768], * p; |
char header[768], * p; |
409 |
|
|
410 |
leave: |
leave: |
411 |
return "UNKNOWN"; |
return "UNKNOWN"; |
412 |
} /* fm_check_armor_type */ |
} |
413 |
|
|
414 |
|
|
415 |
int |
int |
423 |
|
|
424 |
if (!fname) |
if (!fname) |
425 |
{ |
{ |
426 |
gpgme_data_new_from_clipboard (&dat); |
gpg_data_new_from_clipboard (&dat, 0); |
427 |
gpgme_data_release_and_set_file (dat, "gpgme.tmp"); |
gpg_data_release_and_set_file (dat, "gpgme.tmp"); |
428 |
|
|
429 |
fp = gpg_iobuf_open ("gpgme.tmp"); |
fp = gpg_iobuf_open ("gpgme.tmp"); |
430 |
if (!fp) |
if (!fp) |
464 |
drv[i++] = '\0'; |
drv[i++] = '\0'; |
465 |
i = GetDriveType (drv); |
i = GetDriveType (drv); |
466 |
if (i == DRIVE_REMOVABLE) |
if (i == DRIVE_REMOVABLE) |
467 |
return 1; |
return -1; |
468 |
return 0; |
return 0; |
469 |
} |
} |
470 |
|
|
584 |
case FM_ENCRYPT: |
case FM_ENCRYPT: |
585 |
case FM_ENCRYPT_DIR: |
case FM_ENCRYPT_DIR: |
586 |
case FM_SIGNENCRYPT: |
case FM_SIGNENCRYPT: |
587 |
if (strcmp (status, "ENCRYPTED")) |
if (strcmp (status, "ENCRYPTED") |
588 |
|
&& strcmp (status, "SYMKEYENC")) |
589 |
rc = 1; |
rc = 1; |
590 |
break; |
break; |
591 |
|
|
698 |
return n; |
return n; |
699 |
} |
} |
700 |
|
|
|
|
|
701 |
int |
int |
702 |
fm_parse_files (listview_ctrl_t lv, HWND dlg, int cmd) |
fm_parse_files (listview_ctrl_t lv, HWND dlg, int cmd) |
703 |
{ |
{ |
727 |
if (rc) |
if (rc) |
728 |
BUG (0); |
BUG (0); |
729 |
ctx->dlg = dlg; |
ctx->dlg = dlg; |
730 |
|
|
731 |
|
// XXX: for file operations the progress dialog will be |
732 |
|
// reloaded somewhere and thus a 'dummy' dialog remains |
733 |
|
|
734 |
|
/* we use it here to make sure that pfx_cleanup will not use |
735 |
|
any weird values. */ |
736 |
memset (&pfx, 0, sizeof (pfx)); |
memset (&pfx, 0, sizeof (pfx)); |
737 |
gpgme_set_progress_cb (ctx->ctx, progress_callback, &pfx); |
ctx->prog_cb = NULL; |
738 |
|
if (cmd != FM_VERIFY && cmd != FM_SIGN /*&& reg_prefs.fm.progress > 0*/) { |
739 |
|
pfx.hwnd = dlg; |
740 |
|
/*gpgme_set_progress_cb (ctx->ctx, progress_callback, &pfx);*/ |
741 |
|
/*ctx->prog_cb = &pfx;*/ |
742 |
|
} |
743 |
|
|
744 |
/* Commands we need before we can perform the main command */ |
/* Commands we need before we can perform the main command */ |
745 |
switch (fm_cmd) { |
switch (fm_cmd) { |
774 |
if( !listview_get_item_state( lv, i ) ) |
if( !listview_get_item_state( lv, i ) ) |
775 |
continue; |
continue; |
776 |
listview_get_item_text( lv, i, 0, status, sizeof (status) -1 ); |
listview_get_item_text( lv, i, 0, status, sizeof (status) -1 ); |
777 |
if( !strcmp( status, "ENCRYPTED" ) && fm_cmd == FM_DECRYPT ) |
if (!strcmp( status, "ENCRYPTED" ) && fm_cmd == FM_DECRYPT) |
778 |
n++; |
n++; |
779 |
if( !strcmp( status, "UNKNOWN" ) && fm_cmd == FM_SIGN ) |
if (!strcmp( status, "UNKNOWN" ) && fm_cmd == FM_SIGN) |
780 |
n++; |
n++; |
781 |
if (fm_cmd == FM_WIPE) { |
if (fm_cmd == FM_WIPE) |
|
if (!confirm.rset) |
|
|
gpgme_recipients_new (&confirm.rset); |
|
|
listview_get_item_text (lv, i, 1, fname, sizeof (fname)-1); |
|
|
gpgme_recipients_add_name (confirm.rset, fname); |
|
782 |
ndel++; |
ndel++; |
|
} |
|
783 |
} |
} |
784 |
|
|
785 |
if( n > 1 ) |
if (n > 1 && fm_cmd != FM_SYMENC) |
786 |
ctx->cache_cb = 1; |
ctx->cache_cb = 1; |
787 |
|
|
788 |
if( fm_cmd == FM_WIPE && ndel > 0 ) { |
if (fm_cmd == FM_WIPE && ndel > 0) { |
789 |
|
memset (&confirm, 0, sizeof confirm); |
790 |
|
confirm.lv_files = lv; |
791 |
DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_FILES_SECDEL, ctx->dlg, |
DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_FILES_SECDEL, ctx->dlg, |
792 |
file_secdel_confirm_dlg_proc, (LPARAM)&confirm); |
file_secdel_confirm_dlg_proc, (LPARAM)&confirm); |
793 |
if (!confirm.yes) |
if (!confirm.yes) |
816 |
case FM_DECRYPT: rc = fm_decrypt( ctx, fname ); break; |
case FM_DECRYPT: rc = fm_decrypt( ctx, fname ); break; |
817 |
case FM_SIGN: rc = fm_sign( ctx, fname ); break; |
case FM_SIGN: rc = fm_sign( ctx, fname ); break; |
818 |
case FM_SYMENC: rc = fm_sym_encrypt( ctx, fname );break; |
case FM_SYMENC: rc = fm_sym_encrypt( ctx, fname );break; |
819 |
case FM_VERIFY: rc = fm_verify( ctx, sig_detached, fname );break; |
case FM_VERIFY: rc = fm_verify (ctx, sig_detached, fname);break; |
820 |
case FM_IMPORT: |
case FM_IMPORT: |
821 |
free_if_alloc (ctx->opaque); |
free_if_alloc (ctx->opaque); |
822 |
ctx->opaque = m_strdup (fname); |
ctx->opaque = m_strdup (fname); |
838 |
progress_cleanup (&pfx2); |
progress_cleanup (&pfx2); |
839 |
} |
} |
840 |
if (ctx->cache_cb) { |
if (ctx->cache_cb) { |
841 |
memset (ctx->pass_cb.pwd, 0, sizeof (ctx->pass_cb)); |
release_gpg_passphrase_cb (&ctx->pass_cb); |
842 |
ctx->cache_cb = 0; /* make sure it's only used for this session! */ |
ctx->cache_cb = 0; /* make sure it's only used for this session! */ |
843 |
} |
} |
844 |
|
|
854 |
leave: |
leave: |
855 |
if (!rc) |
if (!rc) |
856 |
fm_state_release (ctx); |
fm_state_release (ctx); |
|
if (confirm.rset) |
|
|
gpgme_recipients_release (confirm.rset); |
|
857 |
progress_cleanup (&pfx); |
progress_cleanup (&pfx); |
858 |
return rc; |
return rc; |
859 |
} /* fm_parse_files */ |
} /* fm_parse_files */ |
860 |
|
|
861 |
|
|
862 |
|
/* Wipe the given file @name with the delete mode |
863 |
|
from the configuration. |
864 |
|
Return value: 0 on success. */ |
865 |
int |
int |
866 |
fm_wipe (const char * name) |
fm_wipe (const char *name) |
867 |
{ |
{ |
868 |
int rc; |
int rc; |
869 |
|
|
872 |
rc = secure_unlink (name, reg_prefs.wipe_mode); |
rc = secure_unlink (name, reg_prefs.wipe_mode); |
873 |
SetCursor (LoadCursor (NULL, IDC_ARROW)); |
SetCursor (LoadCursor (NULL, IDC_ARROW)); |
874 |
return rc; |
return rc; |
875 |
} /* fm_wipe */ |
} |
876 |
|
|
877 |
|
|
878 |
|
/* Dump out the given PGP packets from file @name in a dialog. */ |
879 |
int |
int |
880 |
fm_list( const char * name, HWND dlg ) |
fm_list (const char *name, HWND dlg) |
881 |
{ |
{ |
882 |
dialog_box_param( glob_hinst, (LPCTSTR)IDD_WINPT_FILE_STAT, dlg, |
dialog_box_param( glob_hinst, (LPCTSTR)IDD_WINPT_FILE_STAT, dlg, |
883 |
file_stat_dlg_proc, (LPARAM)name, _("File Status"), |
file_stat_dlg_proc, (LPARAM)name, _("File Status"), |
884 |
IDS_WINPT_FILE_STAT ); |
IDS_WINPT_FILE_STAT ); |
885 |
return 0; |
return 0; |
886 |
} /* fm_list */ |
} |
887 |
|
|
888 |
|
|
889 |
static int |
static int |
890 |
ask_filename (fm_state_t c, const char * msg, char ** dst) |
ask_filename (fm_state_t c, const char *msg, char **dst) |
891 |
{ |
{ |
892 |
const char * s; |
const char * s; |
893 |
|
|
895 |
if (!s) |
if (!s) |
896 |
return WPTERR_GENERAL; |
return WPTERR_GENERAL; |
897 |
|
|
898 |
free_if_alloc (*dst); |
if (dst != NULL) |
899 |
|
free_if_alloc (*dst); |
900 |
free_if_alloc (c->output); |
free_if_alloc (c->output); |
901 |
c->output = m_strdup (s); |
c->output = m_strdup (s); |
902 |
if (!c->output) |
if (!c->output) |
903 |
BUG (0); |
BUG (0); |
904 |
*dst = fm_quote_file (s); |
if (dst) |
905 |
|
*dst = fm_quote_file (s); |
906 |
return 0; |
return 0; |
907 |
} |
} |
908 |
|
|
909 |
|
|
910 |
int |
int |
911 |
fm_encrypt (fm_state_t c, const char * name, int sign) |
fm_encrypt (fm_state_t c, const char *name, int sign) |
912 |
{ |
{ |
913 |
gpgme_error_t err; |
gpgme_error_t err; |
914 |
gpgme_key_t key = NULL; |
gpgme_key_t key = NULL; |
915 |
gpgme_ctx_t ctx = c->ctx; |
gpgme_ctx_t ctx = c->ctx; |
916 |
char * src = NULL, * dst = NULL; |
file_data_t in=NULL, out=NULL; |
917 |
char * keyid = NULL, ext[5]; |
char *keyid = NULL, ext[5]; |
918 |
int no_compr = 0; |
int no_compr = 0; |
919 |
int rc = 0; |
int rc = 0; |
920 |
|
|
|
src = fm_quote_file (name); |
|
921 |
c->output = new char[strlen (name) + 5 + 1]; |
c->output = new char[strlen (name) + 5 + 1]; |
922 |
if (!c->output) |
if (!c->output) |
923 |
BUG (0); |
BUG (0); |
924 |
strcpy (ext, file_get_extension (ctx, c->sigmode)); |
strcpy (ext, file_get_extension (ctx, c->sigmode)); |
925 |
strcpy (c->output, name ); |
strcpy (c->output, name); |
926 |
strcat (c->output, ext ); |
strcat (c->output, ext); |
|
dst = fm_quote_file (c->output); |
|
927 |
|
|
928 |
if (!overwrite_file (c->output)) |
if (!overwrite_file (c->output)) { |
929 |
{ |
rc = ask_filename (c, _("Enter filename for encrypted file"), NULL); |
|
rc = ask_filename (c, _("Enter filename for encrypted file"), &dst); |
|
930 |
if (rc) |
if (rc) |
931 |
goto leave; |
goto leave; |
932 |
} |
} |
933 |
|
|
934 |
|
err = gpg_file_data_new (name, 1, &in); |
935 |
|
if (err) |
936 |
|
goto leave; |
937 |
|
err = gpg_file_data_new (c->output, 0, &out); |
938 |
|
if (err) |
939 |
|
goto leave; |
940 |
|
|
941 |
|
/* |
942 |
|
if (c->prog_cb) { |
943 |
|
c->prog_cb->what = name; |
944 |
|
gpg_file_data_set_cb (in, c->prog_cb); |
945 |
|
} |
946 |
|
*/ |
947 |
|
|
948 |
|
/* XXX |
949 |
no_compr = is_multi_media (name); |
no_compr = is_multi_media (name); |
950 |
gpgme_control (ctx, GPGME_CTRL_NO_COMPR, no_compr); |
gpgme_control (ctx, GPGME_CTRL_NO_COMPR, no_compr); |
951 |
|
*/ |
952 |
|
|
953 |
if (sign) { |
if (sign) { |
954 |
if (gpgme_signers_enum (ctx, 0) == NULL) { |
if (gpgme_signers_enum (ctx, 0) == NULL) { |
955 |
keyid = get_gnupg_default_key (); |
keyid = get_gnupg_default_key (); |
956 |
if (!keyid) { |
if (!keyid) { |
957 |
msg_box (c->dlg, _("Could not get default secret key."), |
msg_box (c->dlg, _("Could not get default secret key."), |
958 |
_("Signing"), MB_ERR); |
_("Signing"), MB_ERR); |
959 |
rc = WPTERR_GENERAL; |
rc = WPTERR_GENERAL; |
960 |
goto leave; |
goto leave; |
964 |
gpgme_signers_add (ctx, key); |
gpgme_signers_add (ctx, key); |
965 |
} |
} |
966 |
else { |
else { |
967 |
const char * s; |
gpgme_key_t key = gpgme_signers_enum (ctx, 0); |
968 |
s = (char *)gpgme_key_get_string_attr (gpgme_signers_enum (ctx, 0), |
if (key && key->subkeys) { |
969 |
GPGME_ATTR_KEYID, NULL, 0); |
keyid = m_strdup (key->subkeys->keyid); |
970 |
keyid = m_strdup (s); |
if (!keyid) |
971 |
|
BUG (NULL); |
972 |
|
} |
973 |
} |
} |
974 |
if (!c->init_cb || !c->cache_cb) { |
if (!c->init_cb || !c->cache_cb) { |
975 |
set_gpg_passphrase_cb (c->ctx, &c->pass_cb, GPG_CMD_SIGN, |
set_gpg_passphrase_cb (&c->pass_cb, c->ctx, GPG_CMD_SIGN, |
976 |
c->dlg, _("Signing")); |
c->dlg, _("Signing")); |
977 |
c->init_cb = 1; |
c->init_cb = 1; |
978 |
} |
} |
979 |
err = gpgme_op_file_sign_encrypt (ctx, c->recp, src, dst); |
err = gpgme_op_encrypt_sign (ctx, c->recp, GPGME_ENCRYPT_ALWAYS_TRUST, |
980 |
|
in->dat, out->dat); |
981 |
if (!c->cache_cb) |
if (!c->cache_cb) |
982 |
memset (c->pass_cb.pwd, 0, sizeof (c->pass_cb.pwd)); |
release_gpg_passphrase_cb (&c->pass_cb); |
983 |
if (c->pass_cb.cancel) { |
if (c->pass_cb.cancel) { |
984 |
rc = WPTERR_GENERAL; |
rc = WPTERR_GENERAL; |
985 |
goto leave; |
goto leave; |
986 |
} |
} |
987 |
if (err) { |
if (err) { |
988 |
msg_box (c->dlg, gpgme_strerror (err), _("Sign"), MB_ERR); |
msg_box (c->dlg, gpgme_strerror (err), _("Sign"), MB_ERR); |
989 |
if (err == GPGME_Bad_Passphrase) |
if (gpgme_err_code (err) == GPG_ERR_BAD_PASSPHRASE) |
990 |
agent_del_cache (keyid); |
agent_del_cache (keyid); |
991 |
rc = WPTERR_GENERAL; |
rc = WPTERR_GENERAL; |
992 |
goto leave; |
goto leave; |
993 |
} |
} |
|
gpgme_key_release (key); |
|
994 |
} |
} |
995 |
else { |
else { |
996 |
err = gpgme_op_file_encrypt (ctx, c->recp, src, dst); |
err = gpgme_op_encrypt (ctx, c->recp, GPGME_ENCRYPT_ALWAYS_TRUST, |
997 |
|
in->dat, out->dat); |
998 |
if (err) { |
if (err) { |
999 |
msg_box (c->dlg, gpgme_strerror (err), _("Encrypt"), MB_ERR); |
msg_box (c->dlg, gpgme_strerror (err), _("Encrypt"), MB_ERR); |
1000 |
rc = WPTERR_GENERAL; |
rc = WPTERR_GENERAL; |
1005 |
secure_unlink (name, WIPE_MODE_SIMPLE); |
secure_unlink (name, WIPE_MODE_SIMPLE); |
1006 |
|
|
1007 |
leave: |
leave: |
1008 |
|
if (in) |
1009 |
|
gpg_file_data_release (in); |
1010 |
|
if (out) |
1011 |
|
gpg_file_data_release (out); |
1012 |
free_if_alloc (keyid); |
free_if_alloc (keyid); |
|
free_if_alloc (dst); |
|
|
free_if_alloc (src); |
|
1013 |
return rc; |
return rc; |
1014 |
} /* fm_encrypt */ |
} |
1015 |
|
|
1016 |
|
|
1017 |
int |
int |
1018 |
fm_sym_encrypt (fm_state_t c, const char * name) |
fm_sym_encrypt (fm_state_t c, const char * name) |
1019 |
{ |
{ |
|
int rc = 0, cancel = 0; |
|
|
char * src = NULL, * dst = NULL; |
|
|
char ext[5], * pass; |
|
1020 |
gpgme_ctx_t ctx = c->ctx; |
gpgme_ctx_t ctx = c->ctx; |
1021 |
gpgme_error_t err; |
gpgme_error_t err; |
1022 |
|
file_data_t in=NULL, out=NULL; |
1023 |
|
int rc = 0, cancel = 0; |
1024 |
|
char * src = NULL, * dst = NULL; |
1025 |
|
char ext[5], * pass; |
1026 |
|
|
1027 |
pass = request_passphrase2 (_("Symmetric"), &cancel); |
pass = request_passphrase2 (_("Symmetric"), 0, &cancel); |
1028 |
if (cancel) |
if (cancel) |
1029 |
return 0; |
return 0; |
1030 |
|
|
1031 |
gpgme_control (ctx, GPGME_CTRL_CIPHER, -1); |
/* XXX gpgme_control (ctx, GPGME_CTRL_CIPHER, -1);*/ |
|
src = fm_quote_file (name); |
|
1032 |
c->output = new char[strlen (name) + 5 + 1]; |
c->output = new char[strlen (name) + 5 + 1]; |
1033 |
if (!c->output) |
if (!c->output) |
1034 |
BUG (0); |
BUG (0); |
1035 |
strcpy (ext, file_get_extension (ctx, c->sigmode)); |
strcpy (ext, file_get_extension (ctx, c->sigmode)); |
1036 |
strcpy (c->output, name); |
strcpy (c->output, name); |
1037 |
strcat (c->output, ext); |
strcat (c->output, ext); |
|
dst = fm_quote_file (c->output); |
|
1038 |
|
|
1039 |
if (overwrite_file (c->output) == 0) { |
if (overwrite_file (c->output) == 0) { |
1040 |
rc = WPTERR_GENERAL; |
rc = WPTERR_GENERAL; |
1041 |
goto leave; |
goto leave; |
1042 |
} |
} |
1043 |
gpgme_set_passphrase (ctx, pass); |
|
1044 |
err = gpgme_op_file_encrypt (ctx, NULL, src, dst); |
gpgme_set_passphrase_cb (ctx, sym_passphrase_cb, pass); |
1045 |
|
|
1046 |
|
err = gpg_file_data_new (name, 1, &in); |
1047 |
|
if (err) |
1048 |
|
goto leave; |
1049 |
|
err = gpg_file_data_new (c->output, 0, &out); |
1050 |
|
if (err) |
1051 |
|
goto leave; |
1052 |
|
|
1053 |
|
err = gpgme_op_encrypt (ctx, NULL, GPGME_ENCRYPT_ALWAYS_TRUST, in->dat, out->dat); |
1054 |
if (err) { |
if (err) { |
1055 |
msg_box (c->dlg, gpgme_strerror (err), _("Symmetric"), MB_ERR); |
msg_box (c->dlg, gpgme_strerror (err), _("Symmetric"), MB_ERR); |
1056 |
rc = WPTERR_GENERAL; |
rc = WPTERR_GENERAL; |
1062 |
} |
} |
1063 |
|
|
1064 |
leave: |
leave: |
1065 |
free_if_alloc (src); |
if (in) |
1066 |
free_if_alloc (dst); |
gpg_file_data_release (in); |
1067 |
|
if (out) |
1068 |
|
gpg_file_data_release (out); |
1069 |
sfree_if_alloc (pass); |
sfree_if_alloc (pass); |
1070 |
return rc; |
return rc; |
1071 |
} /* fm_sym_encrypt */ |
} /* fm_sym_encrypt */ |
1072 |
|
|
1073 |
|
|
1074 |
static gpgme_error_t |
/* Show the human readable verify result from @sigres. */ |
1075 |
fm_list_keys( const char * name, gpgme_recipients_t *r_keys ) |
static void |
1076 |
|
show_verify_result (gpgme_verify_result_t sigres) |
1077 |
{ |
{ |
1078 |
return gpgme_op_list_keys( NULL, name, r_keys ); |
gpgme_key_t key=NULL; |
1079 |
} /* fm_list_keys */ |
gpgme_signature_t sig=sigres->signatures; |
1080 |
|
const char *s, *keyid; |
1081 |
|
int sigok = 0; |
1082 |
|
int type; |
1083 |
|
char buf[384]; |
1084 |
|
|
1085 |
|
sig = sigres->signatures; |
1086 |
|
sigok = sig->summary & GPGME_SIGSUM_GREEN; |
1087 |
|
s = sigok? _("Good signature") : _("BAD signature"); |
1088 |
|
type = sigok? MB_OK: MB_ICONWARNING|MB_OK; |
1089 |
|
keyid = sig->fpr; |
1090 |
|
if (!keyid) |
1091 |
|
return; |
1092 |
|
|
1093 |
|
keyid = strlen (sig->fpr) == 40? sig->fpr+32 : sig->fpr + 24; |
1094 |
|
get_pubkey (sig->fpr, &key); |
1095 |
|
_snprintf (buf, sizeof (buf)-1, "Signature made %s using %s key ID %s\n" |
1096 |
|
"%s from \"%s\"", |
1097 |
|
strtimestamp (sig->timestamp), get_key_pubalgo (sig->pubkey_algo), |
1098 |
|
keyid, s, key? key->uids->uid : _("user ID not found")); |
1099 |
|
msg_box (NULL, buf, _("Decrypt Verify"), type); |
1100 |
|
} |
1101 |
|
|
1102 |
|
|
1103 |
|
/* Check the recipients if we have at least one secret key. */ |
1104 |
|
bool |
1105 |
|
secret_key_available (gpgme_recipient_t rset) |
1106 |
|
{ |
1107 |
|
gpgme_recipient_t r; |
1108 |
|
gpgme_key_t key; |
1109 |
|
|
1110 |
|
for (r=rset; r; r = r->next) { |
1111 |
|
if (gpg_err_code (r->status) == GPG_ERR_NO_SECKEY) |
1112 |
|
continue; |
1113 |
|
else { |
1114 |
|
/* extra check to make sure the key is available right now. */ |
1115 |
|
if (!get_seckey (r->keyid, &key)) |
1116 |
|
return true; |
1117 |
|
} |
1118 |
|
} |
1119 |
|
return false; |
1120 |
|
} |
1121 |
|
|
1122 |
|
|
1123 |
|
/* Decrypt the file @name. */ |
1124 |
int |
int |
1125 |
fm_decrypt (fm_state_t c, const char * name) |
fm_decrypt (fm_state_t c, const char *name) |
1126 |
{ |
{ |
1127 |
gpgme_error_t err; |
gpgme_error_t err; |
1128 |
gpgme_ctx_t ctx = c->ctx; |
gpgme_ctx_t ctx = c->ctx; |
1129 |
gpgme_recipients_t keys = NULL; |
gpgme_decrypt_result_t res; |
1130 |
gpgme_sig_t sig = NULL; |
gpgme_verify_result_t sigres; |
1131 |
gpgme_op_flags_t flags; |
file_data_t in =NULL, out=NULL; |
1132 |
char * src = NULL, * dst = NULL, keyid[17]; |
int is_signed = 0; |
|
int is_signed = 0, sigok = 0; |
|
1133 |
int rc = 0; |
int rc = 0; |
1134 |
|
|
1135 |
if (!c->init_cb || !c->cache_cb) { |
if (!c->init_cb || !c->cache_cb) { |
1136 |
set_gpg_passphrase_cb (c->ctx, &c->pass_cb, GPG_CMD_DECRYPT, |
set_gpg_passphrase_cb (&c->pass_cb, c->ctx, GPG_CMD_DECRYPT, |
1137 |
c->dlg, _("Decryption")); |
c->dlg, _("Decryption")); |
1138 |
c->init_cb = 1; |
c->init_cb = 1; |
1139 |
} |
} |
1140 |
|
|
|
src = fm_quote_file (name); |
|
1141 |
c->output = m_strdup (name); |
c->output = m_strdup (name); |
1142 |
if (!c->output) |
if (!c->output) |
1143 |
BUG (0); |
BUG (0); |
1144 |
if (is_openpgp_ext (c->output)) |
if (is_openpgp_ext (c->output)) |
1145 |
c->output[strlen (c->output)-4] = '\0'; |
c->output[strlen (c->output)-4] = '\0'; |
1146 |
else { |
else { |
1147 |
const char *s = get_filename_dlg (c->dlg, FILE_SAVE, _("Choose Filename for Output"), |
const char *s = get_filesave_dlg (c->dlg, _("Choose Filename for Output"), |
1148 |
NULL, NULL); |
NULL, NULL); |
1149 |
if( s ) { |
if (s) { |
1150 |
free_if_alloc( c->output ); |
free_if_alloc (c->output); |
1151 |
c->output = m_strdup( s ); |
c->output = m_strdup (s); |
1152 |
if( !c->output ) |
if (!c->output) |
1153 |
BUG( NULL ); |
BUG (NULL); |
1154 |
} |
} |
1155 |
} |
} |
|
dst = fm_quote_file( c->output ); |
|
|
|
|
|
err = fm_list_keys( src, &keys ); |
|
|
if( err ) |
|
|
goto leave; |
|
|
c->pass_cb.enc_to = keys; |
|
1156 |
|
|
1157 |
if (overwrite_file (c->output) == 0) { |
if (overwrite_file (c->output) == 0) { |
1158 |
rc = ask_filename (c, _("Please enter filename for plaintext file"), &dst); |
rc = ask_filename (c, _("Please enter filename for plaintext file"), NULL); |
1159 |
if (rc) |
if (rc) |
1160 |
goto leave; |
goto leave; |
1161 |
} |
} |
1162 |
remove_crit_file_attrs( c->output, 0 ); |
|
1163 |
err = gpgme_op_file_decrypt( ctx, src, dst ); |
remove_crit_file_attrs (c->output, 0); |
1164 |
if( !c->cache_cb ) |
|
1165 |
memset( c->pass_cb.pwd, 0, sizeof (c->pass_cb.pwd) ); |
err = gpg_file_data_new (name, 1, &in); |
1166 |
if( c->pass_cb.cancel ) { |
if (err) |
1167 |
|
goto leave; |
1168 |
|
err = gpg_file_data_new (c->output, 0, &out); |
1169 |
|
if (err) |
1170 |
|
goto leave; |
1171 |
|
|
1172 |
|
err = gpgme_op_decrypt_verify (ctx, in->dat, out->dat); |
1173 |
|
if (!c->cache_cb) |
1174 |
|
release_gpg_passphrase_cb (&c->pass_cb); |
1175 |
|
if (c->pass_cb.cancel) { |
1176 |
rc = WPTERR_GENERAL; |
rc = WPTERR_GENERAL; |
1177 |
goto leave; |
goto leave; |
1178 |
} |
} |
1179 |
gpgme_decrypt_get_status( ctx, keyid, &flags ); |
|
1180 |
if( err == GPGME_No_Seckey && (flags & GPGME_OPFLAG_NOSECKEY) ) { |
res = gpgme_op_decrypt_result (ctx); |
1181 |
char * p = get_key_userid( keyid+8 ); |
if (res && res->recipients && !secret_key_available (res->recipients)) { |
1182 |
int pkalgo = algo_from_list( keys, keyid ); |
const char *keyid = res->recipients->keyid; |
1183 |
|
char *p = get_key_userid (keyid+8); |
1184 |
|
gpgme_pubkey_algo_t pkalgo = res->recipients->pubkey_algo; |
1185 |
|
|
1186 |
log_box( _("Decryption"), MB_ERR, |
log_box( _("Decryption"), MB_ERR, |
1187 |
_("Encrypted with %s key, ID %s.%s\n" |
_("Encrypted with %s key, ID %s.%s\n" |
1188 |
"Decryption failed: secret key not available."), |
"Decryption failed: secret key not available."), |
1189 |
gpgme_key_expand_attr( GPGME_ATTR_ALGO, pkalgo ), |
get_key_pubalgo (pkalgo), keyid+8, p); |
|
keyid+8, p ); |
|
1190 |
rc = WPTERR_GENERAL; |
rc = WPTERR_GENERAL; |
1191 |
free_if_alloc( p ); |
free_if_alloc (p); |
1192 |
goto leave; |
goto leave; |
1193 |
} |
} |
1194 |
else if( err ) { |
else if (err) { |
1195 |
msg_box( c->dlg, gpgme_strerror( err ), _("Decrypt"), MB_ERR ); |
msg_box (c->dlg, gpgme_strerror (err), _("Decrypt"), MB_ERR); |
1196 |
rc = WPTERR_GENERAL; |
rc = WPTERR_GENERAL; |
1197 |
goto leave; |
goto leave; |
1198 |
} |
} |
1199 |
if( file_exist_check( c->output ) ) { |
if (file_exist_check (c->output)) { |
1200 |
msg_box( c->dlg, _("Decryption failed"), _("Decrypt"), MB_ERR ); |
log_box ("Decrypt", MB_ERR, _("Decryption failed.\n%s: does not exist."), c->output); |
1201 |
rc = WPTERR_GENERAL; |
rc = WPTERR_GENERAL; |
1202 |
} |
} |
1203 |
|
|
1204 |
gpgme_decrypt_get_sig_ctx( ctx, &sig ); |
sigres = gpgme_op_verify_result (ctx); |
1205 |
|
if (sigres && sigres->signatures) |
1206 |
sigok = gpgme_sig_get_ulong_attr( sig, 0, GPGME_ATTR_VALIDITY ) == GPGME_SIG_STAT_GOOD; |
show_verify_result (sigres); |
1207 |
if( sig ) { |
|
|
const char *id, *s = sigok? _("Good signature") : _("BAD signature"); |
|
|
int type = sigok? MB_OK: MB_ICONWARNING|MB_OK; |
|
|
gpgme_key_t key; |
|
|
const char * keyid = gpgme_sig_get_string_attr( sig, GPGME_ATTR_KEYID ); |
|
|
if( !keyid ) |
|
|
keyid = "DEADBEEFDEADBEEF"; |
|
|
if( get_pubkey( keyid+8, &key ) ) |
|
|
log_box( _("Verify"), type, _("%s using keyID 0x%s"), s, keyid+8 ); |
|
|
else { |
|
|
id = gpgme_sig_get_string_attr( sig, GPGME_ATTR_USERID ); |
|
|
log_box( _("Verify"), type, "%s using keyID 0x%08X from %s", |
|
|
s, keyid, id? id : _("Invalid User ID") ); |
|
|
} |
|
|
} |
|
1208 |
|
|
1209 |
leave: |
leave: |
1210 |
free_if_alloc( dst ); |
if (in) |
1211 |
free_if_alloc( src ); |
gpg_file_data_release (in); |
1212 |
gpgme_sig_release( sig ); |
if (out) |
1213 |
gpgme_recipients_release( keys ); |
gpg_file_data_release (out); |
1214 |
return rc; |
return rc; |
1215 |
} /* fm_decrypt */ |
} |
1216 |
|
|
1217 |
|
|
1218 |
int |
int |
1221 |
int rc = 0; |
int rc = 0; |
1222 |
gpgme_ctx_t ctx = c->ctx; |
gpgme_ctx_t ctx = c->ctx; |
1223 |
gpgme_error_t err; |
gpgme_error_t err; |
1224 |
char *src = NULL, *dst = NULL; |
file_data_t in=NULL, out=NULL; |
1225 |
char ext[5]; |
char ext[5]; |
1226 |
|
|
1227 |
if( !c->init_cb || !c->cache_cb ) { |
if (!c->init_cb || !c->cache_cb) { |
1228 |
set_gpg_passphrase_cb( c->ctx, &c->pass_cb, GPG_CMD_SIGN, c->dlg, _("Signing") ); |
set_gpg_passphrase_cb (&c->pass_cb, c->ctx, GPG_CMD_SIGN, c->dlg, _("Signing") ); |
1229 |
c->init_cb = 1; |
c->init_cb = 1; |
1230 |
} |
} |
1231 |
|
|
1232 |
src = fm_quote_file( name ); |
free_if_alloc (c->output); |
1233 |
free_if_alloc( c->output ); |
c->output = new char[strlen (name) + 5 + 1]; |
1234 |
c->output = new char[strlen( name ) + 5 + 1]; |
if( !c->output) |
|
if( !c->output ) |
|
1235 |
BUG( NULL ); |
BUG( NULL ); |
1236 |
strcpy( ext, file_get_extension( ctx, c->sigmode ) ); |
strcpy (ext, file_get_extension (ctx, c->sigmode)); |
1237 |
strcpy( c->output, name ); |
strcpy (c->output, name); |
1238 |
strcat( c->output, ext ); |
strcat (c->output, ext); |
|
dst = fm_quote_file( c->output ); |
|
1239 |
|
|
1240 |
if (!overwrite_file (c->output)) { |
if (!overwrite_file (c->output)) { |
1241 |
rc = ask_filename (c, _("Enter filename for signed file"), &dst); |
rc = ask_filename (c, _("Enter filename for signed file"), NULL); |
1242 |
if (rc) |
if (rc) |
1243 |
goto leave; |
goto leave; |
1244 |
} |
} |
1245 |
remove_crit_file_attrs( c->output, 0 ); |
remove_crit_file_attrs (c->output, 0); |
1246 |
err = gpgme_op_file_sign( ctx, c->sigmode, src, dst ); |
|
1247 |
|
err = gpg_file_data_new (name, 1, &in); |
1248 |
|
if (err) |
1249 |
|
goto leave; |
1250 |
|
err = gpg_file_data_new (c->output, 0, &out); |
1251 |
|
if (err) |
1252 |
|
goto leave; |
1253 |
|
|
1254 |
|
err = gpgme_op_sign (ctx, in->dat, out->dat, c->sigmode); |
1255 |
if( !c->cache_cb ) |
if( !c->cache_cb ) |
1256 |
memset( c->pass_cb.pwd, 0, sizeof (c->pass_cb.pwd) ); |
release_gpg_passphrase_cb (&c->pass_cb); |
1257 |
if( c->pass_cb.cancel ) { |
if( c->pass_cb.cancel ) { |
1258 |
rc = WPTERR_GENERAL; |
rc = WPTERR_GENERAL; |
1259 |
goto leave; |
goto leave; |
1265 |
} |
} |
1266 |
|
|
1267 |
leave: |
leave: |
1268 |
free_if_alloc( src ); |
if (in) |
1269 |
free_if_alloc( dst ); |
gpg_file_data_release (in); |
1270 |
|
if (out) |
1271 |
|
gpg_file_data_release (out); |
1272 |
return rc; |
return rc; |
1273 |
} /* fm_sign */ |
} |
1274 |
|
|
1275 |
|
|
1276 |
static int |
static int |
1277 |
fm_add_sig_stat( siglog_context_t log ) |
fm_add_sig_stat (file_sig_ctx_t log) |
1278 |
{ |
{ |
1279 |
gpgme_key_t key; |
gpgme_key_t key; |
1280 |
const char *uid, *keyid, * kid; |
const char *kid; |
1281 |
int not_found = 0; |
int not_found = 0; |
1282 |
|
|
1283 |
kid = gpgme_sig_get_string_attr( log->sig, GPGME_ATTR_KEYID ); |
kid = log->sig->fpr; |
1284 |
if( !kid ) |
if (!kid) |
1285 |
kid = "DEADBEEFDEADBEEF"; |
BUG (NULL); |
1286 |
if( strlen( kid ) == 16 ) |
if (strlen (kid) == 40) |
1287 |
keyid = kid + 8; |
kid += 32; |
1288 |
else if( strlen( kid ) == 32 ) |
else if (strlen (kid) == 32) |
1289 |
keyid = kid; |
kid += 24; |
1290 |
else if( strlen( kid ) == 40 ) |
if (get_pubkey (kid, &key)) |
1291 |
keyid = kid + 24; |
log->use_uid = 0; |
1292 |
if( get_pubkey( keyid, &key ) ) |
else { |
1293 |
not_found = 1; |
log->user_id = key->uids->uid; |
1294 |
log->use_uid = 0; |
log->use_uid = 1; |
|
if( !not_found ) { |
|
|
uid = gpgme_key_get_string_attr( key, GPGME_ATTR_USERID, NULL, 0 ); |
|
|
log->user_id = uid; |
|
|
log->use_uid = 1; |
|
1295 |
} |
} |
1296 |
file_verify_add_state( log ); |
file_verify_add_state (log); |
|
|
|
1297 |
return 0; |
return 0; |
1298 |
} /* fm_add_sig_stat */ |
} |
1299 |
|
|
1300 |
|
|
1301 |
static int |
static int |
1366 |
} |
} |
1367 |
|
|
1368 |
|
|
1369 |
int |
/* Extract automatically the output file name from @name. |
1370 |
fm_verify( fm_state_t c, int detached, const char *name ) |
If @detached is 1, a detached sig is assumed. */ |
1371 |
|
static int |
1372 |
|
get_output_file (fm_state_t c, const char *name, int detached) |
1373 |
{ |
{ |
1374 |
gpgme_ctx_t ctx = c->ctx; |
const char *file = NULL; |
1375 |
gpgme_error_t err; |
const char *title; |
1376 |
gpgme_sig_t sig; |
char fname[384]; |
1377 |
struct siglog_context_s log; |
|
1378 |
char * src = NULL; |
if (detached) |
1379 |
int rc = 0; |
title = _("Select Data File"); |
1380 |
size_t i; |
else |
1381 |
|
title = _("Selected Output File"); |
1382 |
if( detached ) { |
|
1383 |
const char *file = NULL; |
if (strstr (name, ".sig") || strstr (name, ".asc") || strstr (name, ".gpg")) { |
1384 |
if( strstr( name, ".sig" ) || strstr( name, ".asc" ) ) { |
_snprintf (fname, sizeof (fname) - 1, "%s", name); |
1385 |
char fname[512]; |
fname[strlen (fname) - 4] = '\0'; |
1386 |
_snprintf( fname, sizeof (fname) - 1, "%s", name ); |
if (file_exist_check (fname) == 0 && detached) |
1387 |
fname[strlen( fname ) - 4] = '\0'; |
file = fname; |
1388 |
if( file_exist_check( fname ) == 0 ) |
else if (!detached) { |
1389 |
|
/* If the signature is clear or normal, make sure we do not |
1390 |
|
overwrite the original file if it exists. */ |
1391 |
|
if (file_exist_check (fname) == 0 && !overwrite_file (fname)) { |
1392 |
|
file = get_filesave_dlg (c->dlg, title, NULL, NULL); |
1393 |
|
if (!file) |
1394 |
|
return WPTERR_GENERAL; |
1395 |
|
} |
1396 |
|
else |
1397 |
file = fname; |
file = fname; |
|
} |
|
|
if( !file ) |
|
|
file = get_filename_dlg( c->dlg, FILE_OPEN, _("Select Data File"), NULL, NULL ); |
|
|
if( file ) { |
|
|
free_if_alloc( c->output ); |
|
|
c->output = m_strdup( file ); |
|
1398 |
} |
} |
1399 |
else { |
} |
1400 |
msg_box( c->dlg, _("Invalid file name. Exit"), _("Verify"), MB_ERR ); |
if (!file) |
1401 |
return WPTERR_GENERAL; |
file = get_fileopen_dlg (c->dlg, title, NULL, NULL); |
1402 |
} |
if (file) { |
1403 |
c->sigmode = GPGME_SIG_MODE_DETACH; |
free_if_alloc (c->output); |
1404 |
|
c->output = m_strdup (file); |
1405 |
|
if (!c->output) |
1406 |
|
BUG (NULL); |
1407 |
} |
} |
1408 |
else { |
else { |
1409 |
if( strstr( name, ".asc" ) ) |
msg_box (c->dlg, _("Invalid file name. Exit"), _("Verify"), MB_ERR); |
1410 |
|
return WPTERR_GENERAL; |
1411 |
|
} |
1412 |
|
if (detached) |
1413 |
|
c->sigmode = GPGME_SIG_MODE_DETACH; |
1414 |
|
else { |
1415 |
|
if (strstr (name, ".asc")) |
1416 |
c->sigmode = GPGME_SIG_MODE_CLEAR; |
c->sigmode = GPGME_SIG_MODE_CLEAR; |
1417 |
else |
else |
1418 |
c->sigmode = GPGME_SIG_MODE_NORMAL; |
c->sigmode = GPGME_SIG_MODE_NORMAL; |
1419 |
} |
} |
1420 |
|
return 0; |
1421 |
|
} |
1422 |
|
|
1423 |
memset( &log, 0, sizeof (log) ); |
|
1424 |
strcpy( log.file, name ); |
/* Verify the signature from the file @name. If @detached 1, |
1425 |
file_verify_create_dlg(); |
it is assumed that a detached signature should be checked. */ |
1426 |
src = fm_quote_file( name ); |
int |
1427 |
|
fm_verify (fm_state_t c, int detached, const char *name) |
1428 |
err = gpgme_op_file_verify( ctx, c->sigmode, &sig, src, c->output ); |
{ |
1429 |
if( err == GPGME_Bad_Signature ) { |
gpgme_ctx_t ctx = c->ctx; |
1430 |
log.sig = sig; |
gpgme_error_t err; |
1431 |
fm_add_sig_stat( &log ); |
gpgme_signature_t s; |
1432 |
rc = WPTERR_GENERAL; |
gpgme_verify_result_t res; |
1433 |
|
struct file_sig_ctx_s log; |
1434 |
|
file_data_t in=NULL, out=NULL; |
1435 |
|
int rc = 0; |
1436 |
|
|
1437 |
|
if (strstr (name, ".sig")) |
1438 |
|
detached = 1; |
1439 |
|
|
1440 |
|
if (get_output_file (c, name, detached)) |
1441 |
|
return WPTERR_GENERAL; |
1442 |
|
|
1443 |
|
memset (&log, 0, sizeof (log)); |
1444 |
|
log.file = m_strdup (name); |
1445 |
|
if (!log.file) |
1446 |
|
BUG (NULL); |
1447 |
|
file_verify_create_dlg (); |
1448 |
|
|
1449 |
|
err = gpg_file_data_new (name, 1, &in); |
1450 |
|
if (err) |
1451 |
goto leave; |
goto leave; |
1452 |
} |
err = gpg_file_data_new (c->output, detached? 1 : 0, &out); |
1453 |
if( err ) { |
if (err) |
1454 |
msg_box( c->dlg, gpgme_strerror( err ), _("Verify"), MB_ERR ); |
goto leave; |
1455 |
|
|
1456 |
|
if (c->sigmode == GPGME_SIG_MODE_DETACH) |
1457 |
|
err = gpgme_op_verify (ctx, in->dat, out->dat, NULL); |
1458 |
|
else |
1459 |
|
err = gpgme_op_verify (ctx, in->dat, in->dat, out->dat); |
1460 |
|
if (err) { |
1461 |
|
msg_box (c->dlg, gpgme_strerror (err), _("Verify"), MB_ERR); |
1462 |
rc = WPTERR_GENERAL; |
rc = WPTERR_GENERAL; |
1463 |
goto leave; |
goto leave; |
1464 |
|
} |
1465 |
|
|
1466 |
|
res = gpgme_op_verify_result (ctx); |
1467 |
|
for (s=res->signatures; s; s=s->next) { |
1468 |
|
log.sig = s; |
1469 |
|
fm_add_sig_stat (&log); |
1470 |
} |
} |
1471 |
for( i = 0; i < gpgme_sig_get_ulong_attr( sig, 0, GPGME_ATTR_LEVEL ); i++ ) { |
if (!c->output) |
1472 |
gpgme_sig_t _sig; |
c->output = m_strdup (name); /* for later use */ |
|
_sig = (gpgme_sig_t)gpgme_sig_get_ulong_attr( sig, i, GPGME_ATTR_OPAQUE ); |
|
|
log.sig = _sig; |
|
|
fm_add_sig_stat( &log ); |
|
|
} |
|
|
if( !c->output ) |
|
|
c->output = m_strdup( name ); /* for later use */ |
|
1473 |
|
|
1474 |
leave: |
leave: |
1475 |
free_if_alloc( src ); |
if (in) |
1476 |
|
gpg_file_data_release (in); |
1477 |
|
if (out) |
1478 |
|
gpg_file_data_release (out); |
1479 |
|
if (log.file) |
1480 |
|
delete []log.file; |
1481 |
return rc; |
return rc; |
1482 |
} /* fm_verify */ |
} |
1483 |
|
|
1484 |
|
|
1485 |
int |
int |
1486 |
fm_import( fm_state_t c, const char *name ) |
fm_import (fm_state_t c, const char *name) |
1487 |
{ |
{ |
1488 |
gpgme_ctx_t ctx = c->ctx; |
gpgme_ctx_t ctx = c->ctx; |
1489 |
gpgme_error_t err; |
gpgme_error_t err; |
1490 |
char *src = NULL; |
gpgme_import_result_t res; |
1491 |
int import_res[14] = {0}; |
file_data_t keydata = NULL; |
1492 |
int rc = 0; |
int rc = 0; |
1493 |
|
|
1494 |
free_if_alloc( c->output ); |
free_if_alloc (c->output); |
1495 |
c->output = m_strdup( name ); |
c->output = m_strdup (name); |
1496 |
if( !c->output ) |
if (!c->output) |
1497 |
BUG( NULL ); |
BUG (NULL); |
1498 |
src = fm_quote_file( name ); |
|
1499 |
err = gpgme_op_file_import( ctx, NULL, src ); |
err = gpg_file_data_new (name, 1, &keydata); |
1500 |
if( err ) { |
if (err) |
1501 |
msg_box( c->dlg, gpgme_strerror( err ), _("Import"), MB_ERR ); |
goto leave; |
1502 |
rc = WPTERR_GENERAL; |
|
1503 |
|
err = gpgme_op_import (ctx, keydata->dat); |
1504 |
|
if (err) { |
1505 |
|
msg_box (c->dlg, gpgme_strerror (err), _("Import"), MB_ERR); |
1506 |
|
rc = WPTERR_GENERAL; |
1507 |
goto leave; |
goto leave; |
1508 |
} |
} |
1509 |
gpgme_get_import_status( ctx, import_res, NULL ); |
|
1510 |
print_import_status( import_res, c->implist_revcert ); |
res = gpgme_op_import_result (ctx); |
1511 |
if( import_res[GPGME_IMPSTAT_NOSELFSIG] > 0 ) { |
print_import_status (res); |
1512 |
msg_box( c->dlg, _("Key without a self signature was dectected!\n" |
if (res->no_user_id > 0) { |
1513 |
|
msg_box (c->dlg, _("Key without a self signature was dectected!\n" |
1514 |
"(This key is NOT usable for encryption, etc)\n" |
"(This key is NOT usable for encryption, etc)\n" |
1515 |
"\n" |
"\n" |
1516 |
"Cannot import these key(s)!"), _("Import"), MB_INFO ); |
"Cannot import these key(s)!"), _("Import"), MB_INFO); |
1517 |
} |
} |
1518 |
|
|
1519 |
leave: |
leave: |
1520 |
free_if_alloc( src ); |
if (keydata) |
1521 |
|
gpg_file_data_release (keydata); |
1522 |
return rc; |
return rc; |
1523 |
} /* fm_import */ |
} /* fm_import */ |
1524 |
|
|
1525 |
|
|
1526 |
|
/* Export the selected keys from the File Manager to a file. */ |
1527 |
int |
int |
1528 |
fm_export( fm_state_t c ) |
fm_export (fm_state_t c) |
1529 |
{ |
{ |
1530 |
int rc = 0, id = 0; |
int rc = 0, id = 0; |
1531 |
gpgme_ctx_t ctx = c->ctx; |
gpgme_ctx_t ctx = c->ctx; |
1532 |
gpgme_error_t err; |
gpgme_error_t err; |
1533 |
gpgme_recipients_t rset = c->recp; |
gpgme_key_t *rset = c->recp; |
1534 |
|
file_data_t keydata = NULL; |
1535 |
const char *name, *s = NULL; |
const char *name, *s = NULL; |
1536 |
char *p = NULL, *dst = NULL; |
char *p = NULL, *patt = NULL; |
|
void *recp; |
|
1537 |
|
|
1538 |
if( !gpgme_recipients_count( rset ) ) { |
if (!rset || !rset[0]) { |
1539 |
msg_box( c->dlg, _("No key was selected for export."), _("Export"), MB_ERR ); |
msg_box (c->dlg, _("No key was selected for export."), _("Export"), MB_ERR); |
1540 |
rc = WPTERR_GENERAL; |
rc = WPTERR_GENERAL; |
1541 |
goto leave; |
goto leave; |
1542 |
} |
} |
1543 |
|
|
1544 |
if( gpgme_recipients_count( rset ) == 1 ) { |
if (rset[1] == NULL) { /* count == 1*/ |
1545 |
err = gpgme_recipients_enum_open( rset, &recp ); |
gpgme_key_t k = rset[0]; |
1546 |
if( err ) |
const char *s = k->uids->name; |
1547 |
BUG( NULL ); |
p = new char[strlen (s)+1+8]; |
1548 |
s = gpgme_recipients_enum_read( rset, &recp ); |
if (!p) |
1549 |
gpgme_recipients_enum_close( rset, &recp ); |
BUG (NULL); |
1550 |
p = new char[strlen( s )+1+8]; |
strcpy (p, s ); |
1551 |
if( !p ) |
strcat (p, ".asc"); |
|
BUG( NULL ); |
|
|
strcpy( p, s ); |
|
|
strcat( p, ".asc" ); |
|
1552 |
} |
} |
1553 |
|
|
1554 |
name = get_filename_dlg( c->dlg, FILE_SAVE, _("Choose Name for Key File"), NULL, p? p : NULL ); |
name = get_filename_dlg (c->dlg, FILE_SAVE, _("Choose Name for Key File"), |
1555 |
|
NULL, p? p : NULL); |
1556 |
|
|
1557 |
if( !name ) |
if (!name) |
1558 |
name = "keys.gpg"; |
name = "keys.gpg"; |
1559 |
|
|
1560 |
dst = fm_quote_file( name ); |
patt = gpg_keylist_to_pattern (rset, c->n_recp); |
1561 |
err = gpgme_op_file_export( ctx, rset, dst ); |
|
1562 |
if( err ) { |
err = gpg_file_data_new (name, 0, &keydata); |
1563 |
msg_box( c->dlg, gpgme_strerror( err ), _("Export"), MB_ERR ); |
if (err) |
1564 |
rc = WPTERR_GENERAL; |
goto leave; |
1565 |
|
|
1566 |
|
err = gpgme_op_export (ctx, patt, 0, keydata->dat); |
1567 |
|
if (err) { |
1568 |
|
msg_box (c->dlg, gpgme_strerror (err), _("Export"), MB_ERR); |
1569 |
|
rc = WPTERR_GENERAL; |
1570 |
goto leave; |
goto leave; |
1571 |
} |
} |
1572 |
log_box( _("GnuPG status"), MB_OK, _("Finished (Output: %s)"), name ); |
log_box (_("GnuPG status"), MB_OK, _("Finished (Output: %s)"), name); |
1573 |
|
|
1574 |
leave: |
leave: |
1575 |
free_if_alloc( dst ); |
if (keydata) |
1576 |
free_if_alloc( p ); |
gpg_file_data_release (keydata); |
1577 |
|
if (patt) |
1578 |
|
free (patt); |
1579 |
|
free_if_alloc (p); |
1580 |
|
|
1581 |
return rc; |
return rc; |
1582 |
} /* fm_export */ |
} |
1583 |
|
|
1584 |
|
|
1585 |
int |
int |
1598 |
ctx->cache_cb = 1; |
ctx->cache_cb = 1; |
1599 |
|
|
1600 |
p = cmdl; |
p = cmdl; |
1601 |
if( p && *p > 32 && !memistr( p, strlen( p ), "winpt.exe" ) |
if( p && *p > 32 && !stristr( p, "winpt.exe" ) |
1602 |
&& !strstr( p, "--" ) ) { |
&& !strstr( p, "--" ) ) { |
1603 |
count++; |
count++; |
1604 |
if( *p == '"' ) { /* need to remove quotes */ |
if (*p == '"') { /* need to remove quotes */ |
1605 |
fn = new char[strlen( p )]; |
fn = new char[strlen( p )]; |
1606 |
if( !fn ) |
if (!fn) |
1607 |
BUG( NULL ); |
BUG( NULL ); |
1608 |
memcpy( fn, p+1, strlen( p ) - 2 ); |
memcpy( fn, p+1, strlen( p ) - 2 ); |
1609 |
fn[strlen( p ) -2] = '\0'; |
fn[strlen( p ) -2] = '\0'; |
1617 |
log_box( _("File Manager"), MB_ERR, _("%s: no valid OpenPGP data found."), p ); |
log_box( _("File Manager"), MB_ERR, _("%s: no valid OpenPGP data found."), p ); |
1618 |
return count; |
return count; |
1619 |
} |
} |
1620 |
|
|
1621 |
switch( *s ) { |
switch( *s ) { |
1622 |
case 'E': fm_decrypt( ctx, fn ); break; |
case 'E': fm_decrypt (ctx, fn); break; |
1623 |
case 'P': fm_import( ctx, fn ); break; |
case 'P': fm_import (ctx, fn); break; |
1624 |
case 'S': |
case 'S': |
1625 |
file_verify_use_event( ); |
if (s[1] == 'Y') { |
1626 |
if( s[1] == 'I' ) { |
fm_decrypt (ctx, fn); |
1627 |
if( strlen( s ) == 13 && s[7] == 'D' ) |
break; |
1628 |
|
} |
1629 |
|
file_verify_use_event (); |
1630 |
|
if (s[1] == 'I') { |
1631 |
|
if (strlen (s) == 13 && s[7] == 'D') |
1632 |
detached = 1; |
detached = 1; |
1633 |
fm_verify( ctx, detached, fn ); |
fm_verify( ctx, detached, fn ); |
1634 |
} |
} |
1695 |
rc = -1; |
rc = -1; |
1696 |
goto leave; |
goto leave; |
1697 |
} |
} |
|
if( !overwrite_file( s ) ) { |
|
|
rc = -1; |
|
|
goto leave; |
|
|
} |
|
1698 |
|
|
1699 |
rc = pk_archiv_create( list, s ); |
rc = pk_archiv_create( list, s ); |
1700 |
if( rc ) |
if( rc ) |
1758 |
rc = listview_get_item_text (lv, -1, 1, buf, sizeof (buf)-1); |
rc = listview_get_item_text (lv, -1, 1, buf, sizeof (buf)-1); |
1759 |
if (rc == -1) |
if (rc == -1) |
1760 |
return 0; |
return 0; |
1761 |
mapi_send_ascfile (buf); |
/*mapi_send_ascfile (buf); XXX */ |
1762 |
return 0; |
return 0; |
1763 |
} |
} |