1 |
/* wptSymEnc.cpp - Symmetric encryption support |
/* wptSymEnc.cpp - Symmetric encryption support |
2 |
* Copyright (C) 2002, 2003, 2004, 2005 Timo Schulz |
* Copyright (C) 2002-2006 Timo Schulz |
3 |
* |
* |
4 |
* This file is part of WinPT. |
* This file is part of WinPT. |
5 |
* |
* |
6 |
* WinPT is free software; you can redistribute it and/or |
* WinPT is free software; you can redistribute it and/or |
7 |
* modify it under the terms of the GNU General Public License |
* modify it under the terms of the GNU General Public License |
8 |
* as published by the Free Software Foundation; either version 2 |
* as published by the Free Software Foundation; either version 2 |
9 |
* of the License, or (at your option) any later version. |
* of the License, or (at your option) any later version. |
10 |
* |
* |
11 |
* WinPT is distributed in the hope that it will be useful, |
* WinPT is distributed in the hope that it will be useful, |
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 |
* General Public License for more details. |
* General Public License for more details. |
15 |
* |
*/ |
16 |
* You should have received a copy of the GNU General Public License |
#ifdef HAVE_CONFIG_H |
17 |
* along with WinPT; if not, write to the Free Software Foundation, |
#include <config.h> |
18 |
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
#endif |
19 |
*/ |
|
20 |
|
#include <windows.h> |
21 |
#include <windows.h> |
|
22 |
|
#include "wptGPG.h" |
23 |
#include "wptGPG.h" |
#include "wptCommonCtl.h" |
24 |
#include "wptCommonCtl.h" |
#include "wptContext.h" |
25 |
#include "wptContext.h" |
#include "wptDlgs.h" |
26 |
#include "wptDlgs.h" |
#include "wptNLS.h" |
27 |
#include "wptNLS.h" |
#include "wptUTF8.h" |
28 |
#include "wptUTF8.h" |
#include "wptTypes.h" |
29 |
#include "wptTypes.h" |
#include "wptErrors.h" |
30 |
#include "wptErrors.h" |
|
31 |
|
|
32 |
|
/* Simple passphrase callback. The hook pointer is the passphrase itself. |
33 |
/* Simple passphrase callback. The hook pointer is the passphrase itself. |
Return value: 0 on success. */ |
34 |
Return value: 0 on success. */ |
gpgme_error_t |
35 |
gpgme_error_t |
sym_passphrase_cb (void *hook, const char *hint, const char *pass_inf, |
36 |
sym_passphrase_cb (void *hook, const char *hint, const char *pass_inf, |
int prev_was_bad, int fd) |
37 |
int prev_was_bad, int fd) |
{ |
38 |
{ |
const char *pass = (const char*)hook; |
39 |
const char *pass = (const char*)hook; |
HANDLE hd = (HANDLE)fd; |
40 |
HANDLE hd = (HANDLE)fd; |
DWORD n; |
41 |
DWORD n; |
|
42 |
|
if (!pass) |
43 |
if (!pass) |
return gpg_error (GPG_ERR_INV_ARG); |
44 |
return gpg_error (GPG_ERR_INV_ARG); |
|
45 |
|
if (!WriteFile (hd, pass, strlen (pass), &n, NULL)) |
46 |
if (!WriteFile (hd, pass, strlen (pass), &n, NULL)) |
log_debug ("sym_passphrase_cb: WriteFile() failed ec=%d\n", w32_errno); |
47 |
log_debug ("sym_passphrase_cb: WriteFile() failed ec=%d\n", w32_errno); |
if (!WriteFile (hd, "\n", 1, &n, NULL)) |
48 |
if (!WriteFile (hd, "\n", 1, &n, NULL)) |
log_debug ("sym_passphrase_cb: WriteFile() failed ec=%d\n", w32_errno); |
49 |
log_debug ("sym_passphrase_cb: WriteFile() failed ec=%d\n", w32_errno); |
|
50 |
|
return 0; |
51 |
return 0; |
} |
52 |
} |
|
53 |
|
|
54 |
|
/* Perform symmetric encryption on the current clipboard data. |
55 |
/* Perform symmetric encryption on the current clipboard data. |
Return 0 for success. */ |
56 |
Return 0 for success. */ |
gpgme_error_t |
57 |
int |
gpg_clip_sym_encrypt (void) |
58 |
gpg_encrypt_symmetric (void) |
{ |
59 |
{ |
gpgme_ctx_t ctx; |
60 |
gpgme_ctx_t ctx; |
gpgme_data_t plain=NULL; |
61 |
gpgme_data_t plain=NULL; |
gpgme_data_t ciph=NULL; |
62 |
gpgme_data_t ciph=NULL; |
gpgme_error_t rc; |
63 |
gpgme_error_t rc; |
char *pass = NULL; |
64 |
char *pass = NULL; |
int cancel = 0; |
65 |
int cancel = 0; |
|
66 |
|
pass = request_passphrase2 (_("Symmetric Encryption"), |
67 |
pass = request_passphrase2 (_("Symmetric Encryption"), 0, &cancel); |
PASSDLG_NOTEMPTY, &cancel); |
68 |
if (cancel) |
if (cancel) |
69 |
return 0; |
return 0; |
70 |
rc = gpgme_new (&ctx); |
rc = gpgme_new (&ctx); |
71 |
if (rc) |
if (rc) |
72 |
goto leave; |
goto leave; |
73 |
rc = gpg_data_new_from_clipboard (&plain, 0); |
rc = gpg_data_utf8_new_from_clipboard (&plain, 0, NULL); |
74 |
if (rc) |
if (rc) |
75 |
goto leave; |
goto leave; |
76 |
rc = gpgme_data_new (&ciph); |
rc = gpgme_data_new (&ciph); |
77 |
gpgme_set_armor (ctx, 1); |
if (rc) |
78 |
|
goto leave; |
79 |
gpgme_set_passphrase_cb (ctx, sym_passphrase_cb, pass); |
gpgme_set_armor (ctx, 1); |
80 |
|
gpgme_set_textmode (ctx, 1); |
81 |
rc = gpgme_op_encrypt (ctx, NULL, (gpgme_encrypt_flags_t)0, plain, ciph); |
|
82 |
if (rc) |
gpgme_set_passphrase_cb (ctx, sym_passphrase_cb, pass); |
83 |
msg_box (NULL, gpgme_strerror (rc), _("Symmetric Encryption"), MB_ERR); |
|
84 |
else { |
rc = gpgme_op_encrypt (ctx, NULL, (gpgme_encrypt_flags_t)0, plain, ciph); |
85 |
show_msg (GetDesktopWindow (), 1500, _("GnuPG Status: Finished")); |
if (rc) |
86 |
gpg_data_release_and_set_clipboard (ciph, 1); |
msg_box (NULL, gpgme_strerror (rc), _("Symmetric Encryption"), MB_ERR); |
87 |
ciph = NULL; |
else { |
88 |
} |
show_msg (GetDesktopWindow (), 1500, _("GnuPG Status: Finished")); |
89 |
|
gpg_data_release_to_clipboard (ciph, 1); |
90 |
leave: |
ciph = NULL; |
91 |
if (plain) |
} |
92 |
gpgme_data_release (plain); |
|
93 |
if (ciph) |
leave: |
94 |
gpgme_data_release (ciph); |
if (plain) |
95 |
gpgme_release (ctx); |
gpgme_data_release (plain); |
96 |
sfree_if_alloc (pass); |
if (ciph) |
97 |
return rc? WPTERR_GENERAL : 0; |
gpgme_data_release (ciph); |
98 |
} |
gpgme_release (ctx); |
99 |
|
sfree_if_alloc (pass); |
100 |
|
return rc; |
101 |
|
} |