/[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 208 by twoaday, Mon May 1 12:22:18 2006 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"  
28  #include "wptVersion.h"  #include "wptTypes.h"
29  #include "wptErrors.h"  #include "wptW32API.h"
30  #include "wptCommonCtl.h"  #include "wptAgent.h"
31  #include "wptContext.h"  #include "wptNLS.h"
32  #include "wptDlgs.h"  #include "wptGPG.h"
33  #include "wptKeylist.h"  #include "wptVersion.h"
34  #include "wptFileManager.h"  #include "wptErrors.h"
35  #include "../resource.h"  #include "wptCommonCtl.h"
36    #include "wptContext.h"
37  char *  #include "wptDlgs.h"
38  get_key_userid (const char * keyid)  #include "wptKeylist.h"
39  {  #include "wptUTF8.h"
40      gpgme_key_t key;  #include "resource.h"
41      const char * s;  
42      char * p;  bool secret_key_available (gpgme_recipient_t rset);
43    
44      if( get_pubkey( keyid, &key ) )  /* Return the primary user-ID of the key with the keyid @keyid.
45          return m_strdup( "" );     Caller must free string. */
46      s = gpgme_key_get_string_attr( key, GPGME_ATTR_USERID, NULL, 0 );  char*
47      if( !s )  get_key_userid (const char *keyid)
48          s = _("user ID not found");  {
49      p = new char[strlen( s ) + 4 + 8];      winpt_key_s key;
50      if( !p )      char *uid;
51          BUG( NULL );  
52      sprintf( p, "\n    \"%s\"", s );      memset (&key, 0, sizeof (key));
53      return p;      if (winpt_get_pubkey (keyid, &key))
54  } /* get_key_userid */          return m_strdup (_("user ID not found"));
55        uid = new char[strlen (key.ext->uids->uid) + 4 + 8];
56        if (!uid)
57  static gpgme_error_t          BUG (NULL);
58  list_recipients (gpgme_ctx_t ctx, gpgme_recipients_t *r_rset)      sprintf (uid, "\n    \"%s\"", key.ext->uids->uid);
59  {      return uid;
60      gpgme_error_t rc;  }
61      gpgme_data_t clipdat;  
62    
63      rc = gpgme_data_new_from_clipboard (&clipdat);  /* Decrypt the clipboard contents and on success
64      if( !rc )     replace the data with the plaintext.
65          rc = gpgme_op_list_keys (clipdat, NULL, r_rset);     Return value: 0 on success. */
66      gpgme_data_release( clipdat );  gpgme_error_t
67      return rc;  gpgme_op_clip_decrypt (gpgme_ctx_t ctx)
68  } /* list_recipients */  {
69        gpgme_error_t err;
70        gpgme_data_t ciph = NULL;
71  int      gpgme_data_t plain = NULL;
72  algo_from_list (gpgme_recipients_t rset, const char * keyid)      
73  {      err = gpg_data_new_from_clipboard (&ciph, 0);
74      void * ctx = NULL;      if (err)
75      const char * s;          return err;
76    
77      gpgme_recipients_enum_open( rset, &ctx );      err = gpgme_data_new (&plain);
78      while( (s = gpgme_recipients_enum_read( rset, &ctx )) ) {      if (err) {
79          if( !strcmp( s+1, keyid ) )                  gpgme_data_release (ciph);
80              return *s;          return err;
81      }      }
82      return 0;  
83  } /* algo_from_list */      err = gpgme_op_decrypt_verify (ctx, ciph, plain);
84    
85        gpg_data_release_and_set_clipboard (plain, 0);
86  int      gpgme_data_release (ciph);
87  clip_decrypt_dlg (HWND hwnd)      return err;
88  {  }
89      gpgme_error_t rc;  
90      gpgme_ctx_t ctx = NULL;  
91      gpgme_recipients_t keys = NULL;  /* Convenient function to provide clipboard decryption.
92      gpgme_sig_t sig = NULL;     @hwnd is the parent window used for showing messsages.
93      gpgme_op_flags_t flags;     Return value: 0 on success. */
94      passphrase_cb_s pwd;  gpgme_error_t
95      const char *created, *s;  clip_decrypt_dlg (HWND hwnd)
96      char keyid[17] = {0};  {
97      int novalid = 0;      gpgme_error_t err;
98      unsigned int pkalgo = 0;      gpgme_ctx_t ctx = NULL;
99      u32 t, sigstat;      gpgme_signature_t sig = NULL;
100            gpgme_decrypt_result_t res;
101      rc = gpgme_new (&ctx);      gpgme_verify_result_t sigres;
102      if (rc)      passphrase_cb_s pwd;
103          BUG (0);      const char *s;
104      gpgme_enable_logging (ctx);      const char *uid;
105        int pgp_type = 0;
106      /* allow to verify data generated by 'gpg -a --sign foo' */      int novalid = 0;
107      if (fm_assume_onepass_sig (NULL) == 0) {  
108          rc = list_recipients (ctx, &keys);      /* allow to verify data generated by 'gpg -a --sign foo' */
109          if (rc) {      if (fm_assume_onepass_sig (NULL) == 0) {
110              gpgme_show_error (hwnd, rc, ctx, _("Decryption"), MB_ERR);          /* XXX: addtitional steps needed? */
111              gpgme_release (ctx);      }
112              return rc;  
113          }      err = gpgme_new (&ctx);
114      }      if (err)
115            BUG (NULL);
116      set_gpg_passphrase_cb (ctx, &pwd, GPG_CMD_DECRYPT, hwnd, _("Decryption"));      set_gpg_passphrase_cb (&pwd, ctx, GPG_CMD_DECRYPT, hwnd, _("Decryption"));
117      pwd.enc_to = keys;      gpg_get_recipients (NULL, &pwd.recipients);
118      rc = gpgme_op_clip_decrypt (ctx);  
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 *u = get_key_userid (r->keyid+8);
129          log_box( _("Decryption"), MB_ERR,          log_box (_("Decryption"), MB_ERR,
130                   _("Encrypted with %s key, ID %s.%s\n"                   _("Encrypted with %s key, ID %s.%s\n"
131                     "Decryption failed: secret key not available."),                     "Decryption failed: secret key not available."),
132                     gpgme_key_expand_attr( GPGME_ATTR_ALGO, pkalgo ),                     get_key_pubalgo (r->pubkey_algo), r->keyid+8, u);
133                     keyid+8, p );          free_if_alloc (u);
134          free_if_alloc( p );          goto leave;
135          goto leave;      }
136      }      else if (res->unsupported_algorithm) {
137      else if (rc) {          log_box (_("Decryption"), MB_ERR, _("Unsupported algorithm: %s"),
138          gpgme_cliptype_t pgp_type;                   res->unsupported_algorithm);
139          gpgme_clip_get_pgptype (&pgp_type);      }
140          if (rc == GPGME_No_Data && (pgp_type & GPGME_CLIP_MESSAGE))      else if (err) {
141              msg_box (hwnd, _("Broken OpenPGP message (maybe: quoted printable character in armor)."), _("Decryption"), MB_INFO);          gpg_clip_get_pgptype (&pgp_type);
142          else          if (gpgme_err_code (err) == GPG_ERR_NO_DATA && (pgp_type & PGP_MESSAGE))
143              gpgme_show_error( hwnd, rc, ctx, _("Decryption"), MB_ERR );              msg_box (hwnd, _("Broken OpenPGP message (maybe: quoted printable "
144          goto leave;                               "character in armor)."), _("Decryption"), MB_INFO);
145      }          else
146                msg_box (hwnd, gpgme_strerror (err), _("Decryption"), MB_ERR);
147      if( flags & GPGME_OPFLAG_BADMDC ) {          goto leave;
148          const char *s;      }
149          s = _("WARNING: encrypted message has been manipulated!\n"  
150              "\n"      if (0) { /* XXX: Bad MDC */
151              "Do *NOT* trust any text or data output from this file!\n"          s = _("WARNING: encrypted message has been manipulated!\n"
152              "It is likely, the data was corrupted during the transport\n"              "\n"
153              "but it might be also possible that this is part of an attack.");              "Do *NOT* trust any text or data output from this file!\n"
154          msg_box( hwnd, s, _("*** IMPORTANT ***"), MB_INFO );              "It is likely, the data was corrupted during the transport\n"
155      }              "but it might be also possible that this is part of an attack.");
156                msg_box (hwnd, s, _("*** IMPORTANT ***"), MB_INFO);
157      show_msg( hwnd, 1500, _("GnuPG Status: Finished") );      }
158      gpgme_decrypt_get_sig_ctx( ctx, &sig );  
159      sigstat = gpgme_sig_get_ulong_attr( sig, 0, GPGME_ATTR_VALIDITY );      show_msg (GetDesktopWindow (), 1500, _("GnuPG Status: Finished"));
160      if( sig && sigstat != GPGME_SIG_STAT_ERROR ) {  
161          gpgme_key_t key;      sigres = gpgme_op_verify_result (ctx);
162          const char * val;      if (sigres && sigres->signatures) {
163          char keyid[16+1];          winpt_key_s key;
164                    const char *keyid;
165          val = gpgme_sig_get_string_attr( sig, GPGME_ATTR_KEYID );  
166          if( !val )          sig = sigres->signatures;
167              val = "DEADBEEFDEADBEEF";          if (!sig->fpr)
168          _snprintf( keyid, sizeof(keyid)-1, "%s", val+8 );              BUG (NULL);
169          sigstat = gpgme_sig_get_ulong_attr( sig, 0, GPGME_ATTR_VALIDITY );          keyid = get_keyid_from_fpr (sig->fpr);
170          if( get_pubkey( keyid, &key ) )          memset (&key, 0, sizeof (key));
171              goto leave;          if (!winpt_get_pubkey (keyid, &key)) {
172                if (key.ctx->owner_trust == GPGME_VALIDITY_FULL ||
173          t = gpgme_sig_get_ulong_attr( sig, 0, GPGME_ATTR_OTRUST );                  key.ctx->owner_trust == GPGME_VALIDITY_ULTIMATE)
174          if( t == GPGME_VALIDITY_FULL || t == GPGME_VALIDITY_ULTIMATE )                  s = _("Signature Status: Created with a fully trusted key");
175              s = _("Signature Status: Created with a fully trusted key");              else if (key.ctx->owner_trust == GPGME_VALIDITY_MARGINAL)
176          else if ( t == GPGME_VALIDITY_MARGINAL )                  s = _("Signature Status: Created with a marginal trusted key");
177              s = _("Signature Status: Created with a marginal trusted key");              else if (key.ctx->owner_trust == GPGME_VALIDITY_NEVER) {
178          else if ( t == GPGME_VALIDITY_UNKNOWN                  novalid = 1;
179                    || t == GPGME_VALIDITY_UNDEFINED                  s =  _("Signature Status: Created with an UNTRUSTED key");
180                    || t == GPGME_VALIDITY_NEVER ) {              }
181              novalid = 1;              else
182              s =  _("Signature Status: Created with an UNTRUSTED key");                  s = _("Signature Status: Created with an undefined trusted key");
183          }              uid = key.ext->uids->uid;
184          else          }
185              s = _("Signature Status: Created with an invalid key");          else {
186          t = gpgme_sig_get_ulong_attr( sig, 0, GPGME_ATTR_CREATED );              s = "";
187          created = strtimestamp( t );              uid = _("user ID not found");
188          t = gpgme_key_get_ulong_attr( key, GPGME_ATTR_VALIDITY, NULL, 0 );          }
189          val = gpgme_sig_get_string_attr( sig, GPGME_ATTR_USERID );          log_box (_("WinPT Verify"), MB_OK,
190          if( !val )                   _("%s\n"
191              val = _("Invalid User ID");                     "%s\n"
192          log_box( _("WinPT Verify"), MB_OK,                     "Signature made: %s\n"
193                   _("%s\n"                     "From \"%s\" using key ID 0x%s"
194                     "%s\n"                           "%s %s"),
195                     "Signature made %s\n"                     s, get_gpg_sigstat (sig->summary),
196                     "From \"%s\" using key ID 0x%s"                     strtimestamp (sig->timestamp),
197                     "%s %s"),                     uid, keyid,
198                     s, gpg_sigstat[sigstat % SIGSTAT_MASK], created,                     novalid? "\nPrimary key fingerprint: " : "",
199                     val, keyid,                     novalid? get_key_fpr (key.ctx) : "");
200                     novalid? "\nPrimary key fingerprint: " : "",      }
201                     novalid? get_key_fpr( key ) : ""      
202                     );  leave:
203      }      release_gpg_passphrase_cb (&pwd);
204            gpgme_release (ctx);
205  leave:      return err;
206      gpgme_release( ctx );  }
     gpgme_recipients_release( keys );  
     gpgme_sig_release( sig );  
     return rc;  
 } /* clip_decrypt_dlg */  

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26