/[winpt]/trunk/Src/wptSymEnc.cpp
ViewVC logotype

Diff of /trunk/Src/wptSymEnc.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 23 by twoaday, Fri Sep 30 10:10:16 2005 UTC revision 36 by werner, Thu Oct 27 15:25:13 2005 UTC
# Line 1  Line 1 
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 <io.h>  #include <config.h>
23    #endif
24  #include "wptGPG.h"  
25  #include "wptCommonCtl.h"  #include <windows.h>
26  #include "wptContext.h"  #include <windows.h>
27  #include "wptDlgs.h"  
28  #include "wptNLS.h"  #include "wptGPG.h"
29  #include "wptUTF8.h"  #include "wptCommonCtl.h"
30  #include "wptTypes.h"  #include "wptContext.h"
31  #include "wptErrors.h"  #include "wptDlgs.h"
32    #include "wptNLS.h"
33    #include "wptUTF8.h"
34  /* Simple passphrase callback. The hook pointer is the passphrase itself.  #include "wptTypes.h"
35     Return value: 0 on success. */  #include "wptErrors.h"
36  gpgme_error_t  
37  sym_passphrase_cb (void *hook, const char *hint, const char *pass_inf,  
38                     int prev_was_bad, int fd)  /* Simple passphrase callback. The hook pointer is the passphrase itself.
39  {     Return value: 0 on success. */
40      const char *pass = (const char*)hook;  gpgme_error_t
41    sym_passphrase_cb (void *hook, const char *hint, const char *pass_inf,
42      if (!pass)                     int prev_was_bad, int fd)
43          return gpg_error (GPG_ERR_INV_ARG);  {
44      write (fd, pass, strlen (pass));      const char *pass = (const char*)hook;
45      write (fd, "\n", 1);      HANDLE hd = (HANDLE)fd;
46      return 0;      DWORD n;
47  }  
48        if (!pass)
49            return gpg_error (GPG_ERR_INV_ARG);
50  /* Perform symmetric encryption on the current clipboard data.  
51     Return 0 for success. */      /* XXX: check for passphrase.enter. */
52  int      if (!WriteFile (hd, pass, strlen (pass), &n, NULL))
53  gpg_encrypt_symmetric (void)          log_debug ("sym_passphrase_cb: WriteFile() failed ec=%d\n", w32_errno);
54  {      if (!WriteFile (hd, "\n", 1, &n, NULL))
55      gpgme_ctx_t ctx;          log_debug ("sym_passphrase_cb: WriteFile() failed ec=%d\n", w32_errno);
56      gpgme_data_t plain=NULL;      
57      gpgme_data_t ciph=NULL;      return 0;
58      gpgme_error_t rc;  }
59      char *pass = NULL;  
60      int cancel = 0;  
61        /* Perform symmetric encryption on the current clipboard data.
62      pass = request_passphrase2 (_("Symmetric Encryption"), 0, &cancel);     Return 0 for success. */
63      if (cancel)  int
64          return 0;  gpg_encrypt_symmetric (void)
65      rc = gpgme_new (&ctx);  {
66      if (rc)      gpgme_ctx_t ctx;
67          goto leave;      gpgme_data_t plain=NULL;
68      rc = gpg_data_new_from_clipboard (&plain, 0);      gpgme_data_t ciph=NULL;
69      if (rc)      gpgme_error_t rc;
70          goto leave;      char *pass = NULL;
71      rc = gpgme_data_new (&ciph);      int cancel = 0;
72      gpgme_set_armor (ctx, 1);      
73        pass = request_passphrase2 (_("Symmetric Encryption"), 0, &cancel);
74      gpgme_set_passphrase_cb (ctx, sym_passphrase_cb, pass);      if (cancel)
75            return 0;
76      rc = gpgme_op_encrypt (ctx, NULL, (gpgme_encrypt_flags_t)0, plain, ciph);          rc = gpgme_new (&ctx);
77      if (rc)      if (rc)
78          msg_box (NULL, gpgme_strerror (rc), _("Symmetric Encryption"), MB_ERR);          goto leave;
79      else {      rc = gpg_data_new_from_clipboard (&plain, 0);
80          show_msg (GetDesktopWindow (), 1500, _("GnuPG Status: Finished"));      if (rc)
81          gpg_data_release_and_set_clipboard (ciph, 0);          goto leave;
82          ciph = NULL;      rc = gpgme_data_new (&ciph);
83      }      gpgme_set_armor (ctx, 1);
84    
85  leave:      gpgme_set_passphrase_cb (ctx, sym_passphrase_cb, pass);
86      if (plain)  
87          gpgme_data_release (plain);      rc = gpgme_op_encrypt (ctx, NULL, (gpgme_encrypt_flags_t)0, plain, ciph);    
88      if (ciph)      if (rc)
89          gpgme_data_release (ciph);          msg_box (NULL, gpgme_strerror (rc), _("Symmetric Encryption"), MB_ERR);
90      gpgme_release (ctx);      else {
91      sfree_if_alloc (pass);          show_msg (GetDesktopWindow (), 1500, _("GnuPG Status: Finished"));
92      return rc? WPTERR_GENERAL : 0;          gpg_data_release_and_set_clipboard (ciph, 1);
93  }          ciph = NULL;
94        }
95    
96    leave:
97        if (plain)
98            gpgme_data_release (plain);
99        if (ciph)
100            gpgme_data_release (ciph);
101        gpgme_release (ctx);
102        sfree_if_alloc (pass);
103        return rc? WPTERR_GENERAL : 0;
104    }

Legend:
Removed from v.23  
changed lines
  Added in v.36

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26