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

Diff of /trunk/Src/wptClipDecryptDlg.cpp

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

revision 32 by twoaday, Mon Oct 24 08:03:48 2005 UTC revision 36 by werner, Thu Oct 27 15:25:13 2005 UTC
# Line 1  Line 1 
1  /* wptClipDecryptDlg.cpp - Clipboard decrypt dialog  /* wptClipDecryptDlg.cpp - Clipboard decrypt dialog
2   *      Copyright (C) 2000-2005 Timo Schulz   *      Copyright (C) 2000-2005 Timo Schulz
3   *      Copyright (C) 2005 g10 Code GmbH   *      Copyright (C) 2005 g10 Code GmbH
4   *   *
5   * This file is part of WinPT.   * This file is part of WinPT.
6   *   *
7   * WinPT is free software; you can redistribute it and/or modify   * WinPT is free software; you can redistribute it and/or modify
8   * it under the terms of the GNU General Public License as published by   * it under the terms of the GNU General Public License as published by
9   * the Free Software Foundation; either version 2 of the License, or   * the Free Software Foundation; either version 2 of the License, or
10   * (at your option) any later version.   * (at your option) any later version.
11   *   *
12   * WinPT is distributed in the hope that it will be useful,   * WinPT is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Public License for more details.   * GNU General Public License for more details.
16   *   *
17   * You should have received a copy of the GNU General Public License   * You should have received a copy of the GNU General Public License
18   * along with WinPT; if not, write to the Free Software Foundation,   * along with WinPT; if not, write to the Free Software Foundation,
19   * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA   * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
20   */   */
21    
22  #include <windows.h>  #ifdef HAVE_CONFIG_H
23    #include <config.h>
24  #include "wptTypes.h"  #endif
25  #include "wptW32API.h"  
26  #include "wptAgent.h"  #include <windows.h>
27  #include "wptNLS.h"  #include <windows.h>
28  #include "wptGPG.h"  
29  #include "wptVersion.h"  #include "wptTypes.h"
30  #include "wptErrors.h"  #include "wptW32API.h"
31  #include "wptCommonCtl.h"  #include "wptAgent.h"
32  #include "wptContext.h"  #include "wptNLS.h"
33  #include "wptDlgs.h"  #include "wptGPG.h"
34  #include "wptKeylist.h"  #include "wptVersion.h"
35  #include "wptFileManager.h"  #include "wptErrors.h"
36  #include "../resource.h"  #include "wptCommonCtl.h"
37    #include "wptContext.h"
38  bool secret_key_available (gpgme_recipient_t rset);  #include "wptDlgs.h"
39    #include "wptKeylist.h"
40  /* Return the primary user-ID of the key with the keyid @keyid.  #include "wptFileManager.h"
41     Caller must free string. */  #include "../resource.h"
42  char*  
43  get_key_userid (const char *keyid)  bool secret_key_available (gpgme_recipient_t rset);
44  {  
45      gpgme_key_t key;  /* Return the primary user-ID of the key with the keyid @keyid.
46      const char *s;     Caller must free string. */
47      char *p;  char*
48    get_key_userid (const char *keyid)
49      if (get_pubkey (keyid, &key))  {
50          return m_strdup (_("user ID not found"));      gpgme_key_t key;
51      s = key->uids->uid;      const char *s;
52      if (!s)      char *p;
53          s = _("user ID not found");  
54      p = new char[strlen (s) + 4 + 8];      if (get_pubkey (keyid, &key))
55      if (!p)          return m_strdup (_("user ID not found"));
56          BUG (NULL);      s = key->uids->uid;
57      sprintf (p, "\n    \"%s\"", s);      if (!s)
58      return p;          s = _("user ID not found");
59  }      p = new char[strlen (s) + 4 + 8];
60        if (!p)
61            BUG (NULL);
62  /* Decrypt the clipboard contents and on success      sprintf (p, "\n    \"%s\"", s);
63     replace the data with the plaintext.      return p;
64     Return value: 0 on success. */  }
65  gpgme_error_t  
66  gpgme_op_clip_decrypt (gpgme_ctx_t ctx)  
67  {  /* Decrypt the clipboard contents and on success
68      gpgme_error_t err;     replace the data with the plaintext.
69      gpgme_data_t ciph = NULL;     Return value: 0 on success. */
70      gpgme_data_t plain = NULL;  gpgme_error_t
71        gpgme_op_clip_decrypt (gpgme_ctx_t ctx)
72      err = gpg_data_new_from_clipboard (&ciph, 0);  {
73      if (err)      gpgme_error_t err;
74          return err;      gpgme_data_t ciph = NULL;
75        gpgme_data_t plain = NULL;
76      err = gpgme_data_new (&plain);      
77      if (err) {      err = gpg_data_new_from_clipboard (&ciph, 0);
78          gpgme_data_release (ciph);      if (err)
79          return err;          return err;
80      }  
81        err = gpgme_data_new (&plain);
82      err = gpgme_op_decrypt (ctx, ciph, plain);      if (err) {
83            gpgme_data_release (ciph);
84      gpg_data_release_and_set_clipboard (plain, 0);          return err;
85      gpgme_data_release (ciph);      }
86      return err;  
87  }      err = gpgme_op_decrypt (ctx, ciph, plain);
88    
89        gpg_data_release_and_set_clipboard (plain, 0);
90  /* Convenient function to provide clipboard decryption.      gpgme_data_release (ciph);
91     @hwnd is the parent window used for showing messsages.      return err;
92     Return value: 0 on success. */  }
93  gpgme_error_t  
94  clip_decrypt_dlg (HWND hwnd)  
95  {  /* Convenient function to provide clipboard decryption.
96      gpgme_error_t err;     @hwnd is the parent window used for showing messsages.
97      gpgme_ctx_t ctx = NULL;     Return value: 0 on success. */
98      gpgme_signature_t sig = NULL;  gpgme_error_t
99      gpgme_decrypt_result_t res;  clip_decrypt_dlg (HWND hwnd)
100      gpgme_verify_result_t sigres;  {
101      passphrase_cb_s pwd;      gpgme_error_t err;
102      const char *created, *s;      gpgme_ctx_t ctx = NULL;
103      char keyid[16+1] = {0};      gpgme_signature_t sig = NULL;
104      int novalid = 0;      gpgme_decrypt_result_t res;
105        gpgme_verify_result_t sigres;
106      /* allow to verify data generated by 'gpg -a --sign foo' */      passphrase_cb_s pwd;
107      if (fm_assume_onepass_sig (NULL) == 0) {      const char *created, *s;
108          /* XXX: addtitional steps needed? */      char keyid[16+1] = {0};
109      }      int novalid = 0;
110    
111      err = gpgme_new (&ctx);      /* allow to verify data generated by 'gpg -a --sign foo' */
112      if (err)      if (fm_assume_onepass_sig (NULL) == 0) {
113          BUG (NULL);          /* XXX: addtitional steps needed? */
114      set_gpg_passphrase_cb (&pwd, ctx, GPG_CMD_DECRYPT, hwnd, _("Decryption"));      }
115      err = gpgme_op_clip_decrypt (ctx);  
116      if (pwd.cancel)      err = gpgme_new (&ctx);
117          goto leave;      if (err)
118      if (gpgme_err_code (err) ==  GPG_ERR_BAD_PASSPHRASE)          BUG (NULL);
119          agent_del_cache (pwd.keyid);      set_gpg_passphrase_cb (&pwd, ctx, GPG_CMD_DECRYPT, hwnd, _("Decryption"));
120        err = gpgme_op_clip_decrypt (ctx);
121      res = gpgme_op_decrypt_result (ctx);      if (pwd.cancel)
122      if (err && res->recipients && !secret_key_available (res->recipients)) {          goto leave;
123          gpgme_recipient_t r = res->recipients;      if (gpgme_err_code (err) ==  GPG_ERR_BAD_PASSPHRASE)
124          char *p = get_key_userid (r->keyid+8);          agent_del_cache (pwd.keyid);
125            
126          log_box (_("Decryption"), MB_ERR,      res = gpgme_op_decrypt_result (ctx);
127                   _("Encrypted with %s key, ID %s.%s\n"      if (err && res->recipients && !secret_key_available (res->recipients)) {
128                     "Decryption failed: secret key not available."),          gpgme_recipient_t r = res->recipients;
129                     get_key_pubalgo (r->pubkey_algo), r->keyid+8, p);          char *p = get_key_userid (r->keyid+8);
130          free_if_alloc (p);          
131          goto leave;          log_box (_("Decryption"), MB_ERR,
132      }                   _("Encrypted with %s key, ID %s.%s\n"
133      else if (err) {                     "Decryption failed: secret key not available."),
134          int pgp_type;                     get_key_pubalgo (r->pubkey_algo), r->keyid+8, p);
135          gpg_clip_get_pgptype (&pgp_type);          free_if_alloc (p);
136          if (gpgme_err_code (err) == GPG_ERR_NO_DATA && (pgp_type & PGP_MESSAGE))          goto leave;
137              msg_box (hwnd, _("Broken OpenPGP message (maybe: quoted printable "      }
138                               "character in armor)."), _("Decryption"), MB_INFO);      else if (err) {
139          else          int pgp_type;
140              msg_box (hwnd, gpgme_strerror (err), _("Decryption"), MB_ERR);          gpg_clip_get_pgptype (&pgp_type);
141          goto leave;          if (gpgme_err_code (err) == GPG_ERR_NO_DATA && (pgp_type & PGP_MESSAGE))
142      }              msg_box (hwnd, _("Broken OpenPGP message (maybe: quoted printable "
143                                 "character in armor)."), _("Decryption"), MB_INFO);
144      /* Too paranoid??          else
145      if (flags & GPGME_OPFLAG_BADMDC) {              msg_box (hwnd, gpgme_strerror (err), _("Decryption"), MB_ERR);
146          const char *s;          goto leave;
147          s = _("WARNING: encrypted message has been manipulated!\n"      }
148              "\n"  
149              "Do *NOT* trust any text or data output from this file!\n"      /* Too paranoid??
150              "It is likely, the data was corrupted during the transport\n"      if (flags & GPGME_OPFLAG_BADMDC) {
151              "but it might be also possible that this is part of an attack.");          const char *s;
152          msg_box (hwnd, s, _("*** IMPORTANT ***"), MB_INFO);          s = _("WARNING: encrypted message has been manipulated!\n"
153      }*/              "\n"
154                    "Do *NOT* trust any text or data output from this file!\n"
155                "It is likely, the data was corrupted during the transport\n"
156      show_msg (GetDesktopWindow (), 1500, _("GnuPG Status: Finished"));              "but it might be also possible that this is part of an attack.");
157            msg_box (hwnd, s, _("*** IMPORTANT ***"), MB_INFO);
158      sigres = gpgme_op_verify_result (ctx);      }*/
159      if (sigres && sigres->signatures) {      
160          gpgme_key_t key;  
161          const char * val;      show_msg (GetDesktopWindow (), 1500, _("GnuPG Status: Finished"));
162          char keyid[16+1];  
163        sigres = gpgme_op_verify_result (ctx);
164          sig = sigres->signatures;      if (sigres && sigres->signatures) {
165          if (!sig->fpr)          gpgme_key_t key;
166              val = "????????????????";          const char * val;
167          else          char keyid[16+1];
168              val = strlen (sig->fpr) == 40? sig->fpr+32 : sig->fpr + 24;  
169          _snprintf (keyid, sizeof (keyid)-1, "%s", val+8);          sig = sigres->signatures;
170            if (!sig->fpr)
171          get_pubkey (keyid, &key);              val = "????????????????";
172          if (key->owner_trust == GPGME_VALIDITY_FULL ||          else
173              key->owner_trust == GPGME_VALIDITY_ULTIMATE)              val = strlen (sig->fpr) == 40? sig->fpr+32 : sig->fpr + 24;
174              s = _("Signature Status: Created with a fully trusted key");          _snprintf (keyid, sizeof (keyid)-1, "%s", val+8);
175          else if (key->owner_trust == GPGME_VALIDITY_MARGINAL)  
176              s = _("Signature Status: Created with a marginal trusted key");          get_pubkey (keyid, &key);
177          else if (key->owner_trust == GPGME_VALIDITY_NEVER) {          if (key->owner_trust == GPGME_VALIDITY_FULL ||
178              novalid = 1;              key->owner_trust == GPGME_VALIDITY_ULTIMATE)
179              s =  _("Signature Status: Created with an UNTRUSTED key");              s = _("Signature Status: Created with a fully trusted key");
180          }          else if (key->owner_trust == GPGME_VALIDITY_MARGINAL)
181          else              s = _("Signature Status: Created with a marginal trusted key");
182              s = _("Signature Status: Created with an undefined trusted key");          else if (key->owner_trust == GPGME_VALIDITY_NEVER) {
183          created = strtimestamp (sig->timestamp);              novalid = 1;
184                s =  _("Signature Status: Created with an UNTRUSTED key");
185          if (key)          }
186              val = key->uids->uid;          else
187          else              s = _("Signature Status: Created with an undefined trusted key");
188              val = _("user ID not found");          created = strtimestamp (sig->timestamp);
189          log_box (_("WinPT Verify"), MB_OK,  
190                   _("%s\n"          if (key)
191                     "%s\n"              val = key->uids->uid;
192                     "Signature made %s\n"          else
193                     "From \"%s\" using key ID 0x%s"              val = _("user ID not found");
194                     "%s %s"),          log_box (_("WinPT Verify"), MB_OK,
195                     s, get_gpg_sigstat (sig->summary), created,                   _("%s\n"
196                     val, keyid,                     "%s\n"
197                     novalid? "\nPrimary key fingerprint: " : "",                     "Signature made %s\n"
198                     novalid? get_key_fpr (key) : "");                     "From \"%s\" using key ID 0x%s"
199      }                     "%s %s"),
200                           s, get_gpg_sigstat (sig->summary), created,
201  leave:                     val, keyid,
202      release_gpg_passphrase_cb (&pwd);                     novalid? "\nPrimary key fingerprint: " : "",
203      gpgme_release (ctx);                     novalid? get_key_fpr (key) : "");
204      return err;      }
205  }      
206    leave:
207        release_gpg_passphrase_cb (&pwd);
208        gpgme_release (ctx);
209        return err;
210    }

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26