1 |
/* wptSymEnc.cpp - Symmetric encryption support |
/* wptSymEnc.cpp - Symmetric encryption support |
2 |
* Copyright (C) 2002, 2003, 2004, 2005 Timo Schulz |
* Copyright (C) 2002, 2003, 2004, 2005 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 |
* You should have received a copy of the GNU General Public License |
17 |
* along with WinPT; if not, write to the Free Software Foundation, |
* along with WinPT; if not, write to the Free Software Foundation, |
18 |
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
19 |
*/ |
*/ |
20 |
|
|
21 |
#include <windows.h> |
#ifdef HAVE_CONFIG_H |
22 |
|
#include <config.h> |
23 |
#include "wptGPG.h" |
#endif |
24 |
#include "wptCommonCtl.h" |
|
25 |
#include "wptContext.h" |
#include <windows.h> |
26 |
#include "wptDlgs.h" |
|
27 |
#include "wptNLS.h" |
#include "wptGPG.h" |
28 |
#include "wptUTF8.h" |
#include "wptCommonCtl.h" |
29 |
#include "wptTypes.h" |
#include "wptContext.h" |
30 |
#include "wptErrors.h" |
#include "wptDlgs.h" |
31 |
|
#include "wptNLS.h" |
32 |
|
#include "wptUTF8.h" |
33 |
/* Simple passphrase callback. The hook pointer is the passphrase itself. |
#include "wptTypes.h" |
34 |
Return value: 0 on success. */ |
#include "wptErrors.h" |
35 |
gpgme_error_t |
|
36 |
sym_passphrase_cb (void *hook, const char *hint, const char *pass_inf, |
|
37 |
int prev_was_bad, int fd) |
/* Simple passphrase callback. The hook pointer is the passphrase itself. |
38 |
{ |
Return value: 0 on success. */ |
39 |
const char *pass = (const char*)hook; |
gpgme_error_t |
40 |
HANDLE hd = (HANDLE)fd; |
sym_passphrase_cb (void *hook, const char *hint, const char *pass_inf, |
41 |
DWORD n; |
int prev_was_bad, int fd) |
42 |
|
{ |
43 |
if (!pass) |
const char *pass = (const char*)hook; |
44 |
return gpg_error (GPG_ERR_INV_ARG); |
HANDLE hd = (HANDLE)fd; |
45 |
|
DWORD n; |
46 |
if (!WriteFile (hd, pass, strlen (pass), &n, NULL)) |
|
47 |
log_debug ("sym_passphrase_cb: WriteFile() failed ec=%d\n", w32_errno); |
if (!pass) |
48 |
if (!WriteFile (hd, "\n", 1, &n, NULL)) |
return gpg_error (GPG_ERR_INV_ARG); |
49 |
log_debug ("sym_passphrase_cb: WriteFile() failed ec=%d\n", w32_errno); |
|
50 |
|
/* XXX: check for passphrase.enter. */ |
51 |
return 0; |
if (!WriteFile (hd, pass, strlen (pass), &n, NULL)) |
52 |
} |
log_debug ("sym_passphrase_cb: WriteFile() failed ec=%d\n", w32_errno); |
53 |
|
if (!WriteFile (hd, "\n", 1, &n, NULL)) |
54 |
|
log_debug ("sym_passphrase_cb: WriteFile() failed ec=%d\n", w32_errno); |
55 |
/* Perform symmetric encryption on the current clipboard data. |
|
56 |
Return 0 for success. */ |
return 0; |
57 |
int |
} |
58 |
gpg_encrypt_symmetric (void) |
|
59 |
{ |
|
60 |
gpgme_ctx_t ctx; |
/* Perform symmetric encryption on the current clipboard data. |
61 |
gpgme_data_t plain=NULL; |
Return 0 for success. */ |
62 |
gpgme_data_t ciph=NULL; |
int |
63 |
gpgme_error_t rc; |
gpg_encrypt_symmetric (void) |
64 |
char *pass = NULL; |
{ |
65 |
int cancel = 0; |
gpgme_ctx_t ctx; |
66 |
|
gpgme_data_t plain=NULL; |
67 |
pass = request_passphrase2 (_("Symmetric Encryption"), 0, &cancel); |
gpgme_data_t ciph=NULL; |
68 |
if (cancel) |
gpgme_error_t rc; |
69 |
return 0; |
char *pass = NULL; |
70 |
rc = gpgme_new (&ctx); |
int cancel = 0; |
71 |
if (rc) |
|
72 |
goto leave; |
pass = request_passphrase2 (_("Symmetric Encryption"), 0, &cancel); |
73 |
rc = gpg_data_new_from_clipboard (&plain, 0); |
if (cancel) |
74 |
if (rc) |
return 0; |
75 |
goto leave; |
rc = gpgme_new (&ctx); |
76 |
rc = gpgme_data_new (&ciph); |
if (rc) |
77 |
gpgme_set_armor (ctx, 1); |
goto leave; |
78 |
|
rc = gpg_data_new_from_clipboard (&plain, 0); |
79 |
gpgme_set_passphrase_cb (ctx, sym_passphrase_cb, pass); |
if (rc) |
80 |
|
goto leave; |
81 |
rc = gpgme_op_encrypt (ctx, NULL, (gpgme_encrypt_flags_t)0, plain, ciph); |
rc = gpgme_data_new (&ciph); |
82 |
if (rc) |
gpgme_set_armor (ctx, 1); |
83 |
msg_box (NULL, gpgme_strerror (rc), _("Symmetric Encryption"), MB_ERR); |
|
84 |
else { |
gpgme_set_passphrase_cb (ctx, sym_passphrase_cb, pass); |
85 |
show_msg (GetDesktopWindow (), 1500, _("GnuPG Status: Finished")); |
|
86 |
gpg_data_release_and_set_clipboard (ciph, 1); |
rc = gpgme_op_encrypt (ctx, NULL, (gpgme_encrypt_flags_t)0, plain, ciph); |
87 |
ciph = NULL; |
if (rc) |
88 |
} |
msg_box (NULL, gpgme_strerror (rc), _("Symmetric Encryption"), MB_ERR); |
89 |
|
else { |
90 |
leave: |
show_msg (GetDesktopWindow (), 1500, _("GnuPG Status: Finished")); |
91 |
if (plain) |
gpg_data_release_and_set_clipboard (ciph, 1); |
92 |
gpgme_data_release (plain); |
ciph = NULL; |
93 |
if (ciph) |
} |
94 |
gpgme_data_release (ciph); |
|
95 |
gpgme_release (ctx); |
leave: |
96 |
sfree_if_alloc (pass); |
if (plain) |
97 |
return rc? WPTERR_GENERAL : 0; |
gpgme_data_release (plain); |
98 |
} |
if (ciph) |
99 |
|
gpgme_data_release (ciph); |
100 |
|
gpgme_release (ctx); |
101 |
|
sfree_if_alloc (pass); |
102 |
|
return rc? WPTERR_GENERAL : 0; |
103 |
|
} |