/[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 179 by twoaday, Fri Feb 24 13:12:26 2006 UTC revision 231 by twoaday, Tue Jun 20 09:18:44 2006 UTC
# Line 1  Line 1 
1  /* wptClipDecryptDlg.cpp - Clipboard decrypt dialog  /* wptClipDecryptDlg.cpp - Clipboard decryption
2   *      Copyright (C) 2000-2005 Timo Schulz   *      Copyright (C) 2000-2006 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.
# Line 24  Line 24 
24  #endif  #endif
25    
26  #include <windows.h>  #include <windows.h>
27    #include <assert.h>
28    #include <time.h>
29    
30  #include "wptTypes.h"  #include "wptTypes.h"
31  #include "wptW32API.h"  #include "wptW32API.h"
# Line 36  Line 38 
38  #include "wptContext.h"  #include "wptContext.h"
39  #include "wptDlgs.h"  #include "wptDlgs.h"
40  #include "wptKeylist.h"  #include "wptKeylist.h"
 #include "wptFileManager.h"  
41  #include "wptUTF8.h"  #include "wptUTF8.h"
42  #include "resource.h"  #include "resource.h"
43    
44  bool secret_key_available (gpgme_recipient_t rset);  bool is_seckey_available (gpgme_recipient_t rset);
45    char* get_pka_status (gpgme_signature_t sig);
46    
47    
48  /* Return the primary user-ID of the key with the keyid @keyid.  /* Return the primary user-ID of the key with the keyid @keyid.
49     Caller must free string. */     Caller must free string. */
50  char*  char*
51  get_key_userid (const char *keyid)  get_key_userid (const char *keyid)
52  {  {
53      gpgme_key_t key;      winpt_key_s key;
54      char *p, *uid;      const char *fmt;
55        char *uid;
56    
57      if (get_pubkey (keyid, &key))      memset (&key, 0, sizeof (key));
58        if (winpt_get_pubkey (keyid, &key))
59          return m_strdup (_("user ID not found"));          return m_strdup (_("user ID not found"));
60      uid = utf8_to_wincp2 (key->uids->uid);      fmt = "\n    \"%s\"";
61        uid = new char[strlen (key.ext->uids->uid) + strlen (fmt)+ 2];
62      if (!uid)      if (!uid)
         uid = strdup (_("user ID not found"));  
     p = new char[strlen (uid) + 4 + 8];  
     if (!p)  
63          BUG (NULL);          BUG (NULL);
64      sprintf (p, "\n    \"%s\"", uid);      sprintf (uid, fmt, key.ext->uids->uid);
65      safe_free (uid);      winpt_release_pubkey (&key);
66      return p;      return uid;
67  }  }
68    
69    
# Line 92  gpgme_op_clip_decrypt (gpgme_ctx_t ctx) Line 95  gpgme_op_clip_decrypt (gpgme_ctx_t ctx)
95  }  }
96    
97    
98    /* Return humand readable ownertrust description for verification info. */
99    const char*
100    verify_get_key_ownertrust (gpgme_validity_t key_ot, int *novalid)
101    {
102        const char *s;
103    
104        if (key_ot == GPGME_VALIDITY_FULL ||
105            key_ot == GPGME_VALIDITY_ULTIMATE)
106            s = _("Signature status: created with a fully trusted key");    
107        else if (key_ot == GPGME_VALIDITY_MARGINAL)    
108            s = _("Signature status: created with a marginal trusted key");
109        else if (key_ot == GPGME_VALIDITY_NEVER) {
110            if (novalid) *novalid = 1;
111            s =  _("Signature status: created with an UNTRUSTED key");      
112        }
113        else
114            s = _("Signature status: created with an undefined trusted key");
115        return s;
116    }
117    
118    
119    /* Return a signature specific header and footer for the clipboard. */
120    void
121    verify_get_clip_info (gpgme_signature_t sig, char **r_header, char **r_footer)
122    {
123        struct winpt_key_s pk;
124        const char *head = _("*** PGP SIGNATURE VERIFICATION ***\r\n"
125                             "*** Signature made:    %s\r\n"
126                             "*** Signature verfied: %s\r\n"
127                             "*** %s\r\n"
128                             "*** Signature result:  %s\r\n"
129                             "*** Signer: %s (0x%s)\r\n"
130                             "*** BEGIN PGP DECRYPTED TEXT ***\r\n");
131        const char *foot = _("\r\n*** END PGP DECRYPTED TEXT ***");
132        const char *s, *made, *ver, *ot;
133        char *p;
134    
135        if (winpt_get_pubkey (sig->fpr, &pk))
136            BUG (0);
137    
138        ot = verify_get_key_ownertrust (pk.ctx->owner_trust, NULL);
139        made = strtimestamp (sig->timestamp);
140        ver = strtimestamp (time (NULL));
141        s = get_gpg_sigstat (sig->summary);
142        p = new char[strlen (head) + strlen (s) + strlen (made) +
143                     strlen (sig->fpr) + strlen (ot) + strlen (ver) +
144                     strlen (pk.ext->uids->uid) + 1];
145        if (!p)
146            BUG (0);
147        sprintf (p, head, made, ver, ot, s,
148                    pk.ext->uids->uid, get_keyid_from_fpr (sig->fpr));
149        *r_header = p;
150        *r_footer = m_strdup (foot);
151    }
152    
153    
154    /* Show a human readable description of the given signature @sig. */
155    void
156    verify_show_signature_state (gpgme_signature_t sig)
157    {
158        winpt_key_s key;
159        const char *keyid, *uid;
160        const char *s;
161        char *pka_info = NULL;
162        int novalid = 0;
163    
164        assert (sig->fpr != NULL);
165            
166        keyid = get_keyid_from_fpr (sig->fpr);
167        memset (&key, 0, sizeof (key));
168        if (!winpt_get_pubkey (keyid, &key)) {
169            s = verify_get_key_ownertrust (key.ctx->owner_trust, &novalid);
170            uid = key.ext->uids->uid;      
171        }
172        else {
173            s = "";
174            uid = _("user ID not found");
175        }
176    
177        pka_info = get_pka_status (sig);
178        log_box (_("Decrypt Verify"), novalid? MB_WARN : MB_OK,
179                 _("%s\n"
180                   "%s\n"
181                   "Signature made: %s\n"
182                   "From \"%s\" using key ID 0x%s"
183                   "%s %s\n%s"),
184                   s, get_gpg_sigstat (sig->summary),                  
185                   strtimestamp (sig->timestamp),
186                   uid, keyid,
187                   novalid? "\nPrimary key fingerprint: " : "",
188                   novalid? get_key_fpr (key.ctx) : "",
189                   pka_info? pka_info : ""
190                   );
191        free_if_alloc (pka_info);
192        winpt_release_pubkey (&key);
193    }
194    
195    
196  /* Convenient function to provide clipboard decryption.  /* Convenient function to provide clipboard decryption.
197     @hwnd is the parent window used for showing messsages.     @hwnd is the parent window used for showing messsages.
198     Return value: 0 on success. */     Return value: 0 on success. */
199  gpgme_error_t  gpgme_error_t
200  clip_decrypt_dlg (HWND hwnd)  clip_decrypt_dlg (HWND hwnd, int use_viewer)
201  {  {
202      gpgme_error_t err;      gpgme_error_t err;
203      gpgme_ctx_t ctx = NULL;      gpgme_ctx_t ctx = NULL;
     gpgme_signature_t sig = NULL;  
204      gpgme_decrypt_result_t res;      gpgme_decrypt_result_t res;
205      gpgme_verify_result_t sigres;      gpgme_verify_result_t sigres;
206      passphrase_cb_s pwd;      passphrase_cb_s pwd;
207      const char *s;      const char *s;    
     char *uid;  
208      int pgp_type = 0;      int pgp_type = 0;
     int novalid = 0;  
209    
210      /* allow to verify data generated by 'gpg -a --sign foo' */      /* allow to verify data generated by 'gpg -a --sign foo' */
211      if (fm_assume_onepass_sig (NULL) == 0) {      if (fm_assume_onepass_sig (NULL) == 1) {
212          /* XXX: addtitional steps needed? */          dialog_box_param (glob_hinst, (LPCSTR)IDD_WINPT_VERIFY, hwnd,
213                              clip_verify_dlg_proc, 0,
214                              _("Verify"), IDS_WINPT_VERIFY);
215            return 0;
216      }      }
217    
218      err = gpgme_new (&ctx);      err = gpgme_new (&ctx);
# Line 127  clip_decrypt_dlg (HWND hwnd) Line 228  clip_decrypt_dlg (HWND hwnd)
228          agent_del_cache (pwd.keyid);          agent_del_cache (pwd.keyid);
229    
230      res = gpgme_op_decrypt_result (ctx);      res = gpgme_op_decrypt_result (ctx);
231      if (err && res->recipients && !secret_key_available (res->recipients)) {      if (err && res->recipients && !is_seckey_available (res->recipients)) {
232          gpgme_recipient_t r = res->recipients;          gpgme_recipient_t r = res->recipients;
233          uid = get_key_userid (r->keyid+8);          char *u = get_key_userid (r->keyid+8);
234          log_box (_("Decryption"), MB_ERR,          log_box (_("Decryption"), MB_ERR,
235                   _("Encrypted with %s key, ID %s.%s\n"                   _("Encrypted with %s key, ID %s.%s\n"
236                     "Decryption failed: secret key not available."),                     "Decryption failed: secret key not available."),
237                     get_key_pubalgo (r->pubkey_algo), r->keyid+8, uid);                     get_key_pubalgo (r->pubkey_algo), r->keyid+8, u);
238          free_if_alloc (uid);          free_if_alloc (u);
239          goto leave;          goto leave;
240      }      }
241      else if (res->unsupported_algorithm) {      else if (res->unsupported_algorithm) {
# Line 164  clip_decrypt_dlg (HWND hwnd) Line 265  clip_decrypt_dlg (HWND hwnd)
265    
266      sigres = gpgme_op_verify_result (ctx);      sigres = gpgme_op_verify_result (ctx);
267      if (sigres && sigres->signatures) {      if (sigres && sigres->signatures) {
268          gpgme_key_t key=NULL;          if (!use_viewer)
269          const char *keyid;              verify_show_signature_state (sigres->signatures);
   
         sig = sigres->signatures;  
         if (!sig->fpr)  
             BUG (NULL);  
         if (strlen (sig->fpr) > 16)  
             keyid = strlen (sig->fpr) == 40? sig->fpr+24 : sig->fpr+16;  
270          else          else
271              keyid = sig->fpr;              DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_CLIPEDIT,
272                                hwnd, clip_edit_dlg_proc,
273          get_pubkey (keyid, &key);                              (LPARAM)sigres->signatures);
         if (key) {  
             if (key->owner_trust == GPGME_VALIDITY_FULL ||  
                 key->owner_trust == GPGME_VALIDITY_ULTIMATE)  
                 s = _("Signature Status: Created with a fully trusted key");  
             else if (key->owner_trust == GPGME_VALIDITY_MARGINAL)  
                 s = _("Signature Status: Created with a marginal trusted key");  
             else if (key->owner_trust == GPGME_VALIDITY_NEVER) {  
                 novalid = 1;  
                 s =  _("Signature Status: Created with an UNTRUSTED key");  
             }  
             else  
                 s = _("Signature Status: Created with an undefined trusted key");  
             uid = utf8_to_wincp2 (key->uids->uid);  
         }  
         else {  
             s = "";  
             uid = strdup (_("user ID not found"));  
         }  
         log_box (_("WinPT Verify"), MB_OK,  
                  _("%s\n"  
                    "%s\n"  
                    "Signature made: %s\n"  
                    "From \"%s\" using key ID 0x%s"  
                    "%s %s"),  
                    s, get_gpg_sigstat (sig->summary),  
                    strtimestamp (sig->timestamp),  
                    uid, keyid+8,  
                    novalid? "\nPrimary key fingerprint: " : "",  
                    novalid? get_key_fpr (key) : "");  
         safe_free (uid);  
274      }      }
275        
276  leave:  leave:
277      release_gpg_passphrase_cb (&pwd);      release_gpg_passphrase_cb (&pwd);
278      gpgme_release (ctx);      gpgme_release (ctx);

Legend:
Removed from v.179  
changed lines
  Added in v.231

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26