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

Legend:
Removed from v.2  
changed lines
  Added in v.42

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26