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

Diff of /trunk/Src/wptClipSignEncDlg.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 24 by twoaday, Sat Oct 8 10:43:08 2005 UTC
# Line 1  Line 1 
1  /* wptSignEncDlg.cpp - Sign & encrypt dialog  /* wptSignEncDlg.cpp - Sign & encrypt dialog
2   *      Copyright (C) 2000-2004 Timo Schulz   *      Copyright (C) 2000-2005 Timo Schulz
3   *   *
4   * This file is part of WinPT.   * This file is part of WinPT.
5   *   *
# Line 39  Line 39 
39  #include "wptUTF8.h"  #include "wptUTF8.h"
40    
41    
42    /* Encrypt the clipboard data with the recipients from @rset and
43       additionally sign the data before @signer as the keyID.
44       Return value: 0 on success. */
45    gpgme_error_t
46    gpg_clip_sign_encrypt (gpgme_ctx_t ctx, const char *signer,
47                           gpgme_key_t *rset, int opts)
48    {
49        gpgme_error_t err;
50        gpgme_data_t plain = NULL;
51        gpgme_data_t ciph = NULL;
52        gpgme_key_t key = NULL;
53    
54        if (!signer)
55            return gpg_error (GPG_ERR_INV_ARG);
56        if (get_pubkey (signer, &key))
57            return gpg_error (GPG_ERR_NO_PUBKEY);
58    
59        gpgme_set_armor (ctx, 1);
60        
61        err = gpg_data_new_from_clipboard (&plain, 0);
62        if (err)
63            goto leave;  
64        err = gpgme_data_new (&ciph);
65        if (err)
66            goto leave;
67        err = gpgme_signers_add (ctx, key);
68        if (err)
69            goto leave;
70    
71        err = gpgme_op_encrypt_sign (ctx, rset,
72                                     opts? GPGME_ENCRYPT_ALWAYS_TRUST : (gpgme_encrypt_flags_t)0,
73                                     plain, ciph);
74        if (err)
75            goto leave;
76    
77        gpg_data_release_and_set_clipboard (ciph, 1);
78        ciph = NULL;
79    
80    leave:
81        if (plain)
82            gpgme_data_release (plain);
83        if (ciph)
84            gpgme_data_release (ciph);
85        gpgme_key_release (key);
86        return err;
87    }
88    
89    
90    /* Dialog procedure for clipboard sign + encrypt. */
91  BOOL CALLBACK  BOOL CALLBACK
92  clip_signenc_dlg_proc( HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam )  clip_signenc_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
93  {  {
94      static listview_ctrl_t lv = NULL;      static listview_ctrl_t lv = NULL;
95      static keylist_t list = NULL;      static keylist_t list = NULL;
96      static int enable = 0;      static int enable = 0;
     gpgme_ctx_t ctx;  
97      gpgme_keycache_t kc;      gpgme_keycache_t kc;
98      gpgme_recipients_t rset;      gpgme_key_t *rset;
99      gpgme_error_t err;      gpgme_error_t err;
100        gpgme_ctx_t ctx;
101      passphrase_cb_s pwd;      passphrase_cb_s pwd;
102      char * signer = NULL;      char *signer = NULL;
103      int force_trust = 0, opt = 0;      int force_trust = 0, opt = 0;
104      int kmode = reg_prefs.keylist_mode? KEYLIST_ENCRYPT_MIN : KEYLIST_ENCRYPT;      int kmode = reg_prefs.keylist_mode? KEYLIST_ENCRYPT_MIN : KEYLIST_ENCRYPT;
105        int n;
106            
107      switch( msg ) {      switch( msg ) {
108      case WM_INITDIALOG:      case WM_INITDIALOG:
109  #ifndef LANG_DE          enable = 0;
110        #ifndef LANG_DE
111          SetWindowText( dlg, _("Sign & Encrypt") );          SetWindowText( dlg, _("Sign & Encrypt") );
112  #endif      #endif
113          kc = keycache_get_ctx( KEYCACHE_PUB );          kc = keycache_get_ctx( KEYCACHE_PUB );
114          if( !kc )          if( !kc )
115              BUG( NULL );              BUG( NULL );
116          lv = keylist_load( GetDlgItem( dlg, IDC_SIGNENC_KEYLIST ),          lv = keylist_load( GetDlgItem( dlg, IDC_SIGNENC_KEYLIST ),
117                             kc, NULL, kmode, GPGME_ATTR_USERID );                             kc, NULL, kmode, KEY_SORT_USERID);
118          seclist_init( dlg, IDC_SIGNENC_SECLIST, KEYLIST_FLAG_SHORT, &list );          seclist_init( dlg, IDC_SIGNENC_SECLIST, KEYLIST_FLAG_SHORT, &list );
119          center_window( dlg );          center_window (dlg, NULL);
120          set_active_window( dlg );          set_active_window (dlg);
121          EnableWindow( GetDlgItem( dlg, IDC_SIGNENC_SECLIST ), FALSE );          EnableWindow (GetDlgItem (dlg, IDC_SIGNENC_SECLIST), FALSE);
122          SetDlgItemText (dlg, IDC_SIGNENC_SELKEY, _("Select key for signing"));          SetDlgItemText (dlg, IDC_SIGNENC_SELKEY, _("Select key for signing"));
123          SetDlgItemText (dlg, IDC_SIGNENC_SECLISTINF, _("Signing key:"));          SetDlgItemText (dlg, IDC_SIGNENC_SECLISTINF, _("Signing key:"));
124          SetForegroundWindow( dlg );          SetForegroundWindow (dlg);
125          return TRUE;          return TRUE;
126                    
127      case WM_DESTROY:      case WM_DESTROY:
128          seclist_destroy( &list );          seclist_destroy (&list);
129          reset_active_window( );          reset_active_window ();
130          if( lv ) {          if (lv) {
131              keylist_delete( lv );              keylist_delete( lv );
132              lv = NULL;              lv = NULL;
133          }          }
         memset( pwd.pwd, 0, sizeof pwd.pwd );  
134          return FALSE;          return FALSE;
135                    
136      case WM_NOTIFY:      case WM_NOTIFY:
# Line 105  clip_signenc_dlg_proc( HWND dlg, UINT ms Line 155  clip_signenc_dlg_proc( HWND dlg, UINT ms
155          }          }
156          switch( LOWORD( wparam ) ) {          switch( LOWORD( wparam ) ) {
157          case IDOK:          case IDOK:
158              rset = keylist_get_recipients( lv, &force_trust, NULL );              rset = keylist_get_recipients( lv, &force_trust, &n );
159              if( !gpgme_recipients_count( rset ) ) {              if (!n) {
160                  msg_box( dlg, _("You must select at least one key."), _("Sign & Encrypt"), MB_ERR );                  msg_box( dlg, _("You must select at least one key."), _("Sign & Encrypt"), MB_ERR );
                 gpgme_recipients_release( rset );  
161                  return FALSE;                  return FALSE;
162              }              }
163              if( IsDlgButtonChecked( dlg, IDC_SIGNENC_SELKEY ) ) {              if( IsDlgButtonChecked( dlg, IDC_SIGNENC_SELKEY ) ) {
# Line 119  clip_signenc_dlg_proc( HWND dlg, UINT ms Line 168  clip_signenc_dlg_proc( HWND dlg, UINT ms
168                      msg_box( dlg, _("No key was selected."), _("Signing"), MB_ERR );                      msg_box( dlg, _("No key was selected."), _("Signing"), MB_ERR );
169                      return FALSE;                      return FALSE;
170                  }                  }
171                  s = gpgme_key_get_string_attr( key, GPGME_ATTR_KEYID, NULL, 0 );                  s = key->subkeys->keyid;
172                  if( s )                  if (s)
173                      signer = m_strdup( s+8 );                      signer = m_strdup(s+8);
174              }              }
175              else {              else {
176                  signer = get_gnupg_default_key( );                  signer = get_gnupg_default_key ();
177                  if( !signer ) {                  if (!signer) {
178                      msg_box( dlg, _("Could not get default key."), _("Signing"), MB_ERR );                      msg_box (dlg, _("Could not get default key."), _("Signing"), MB_ERR);
179                      return FALSE;                      return FALSE;
180                  }                  }
181              }              }
182              memset( &pwd, 0, sizeof pwd );  
183              err = gpgme_new( &ctx );              err = gpgme_new (&ctx);
184              if( err )              if (err)
185                  BUG( dlg );                  BUG (NULL);
186              gpgme_enable_logging( ctx );              set_gpg_passphrase_cb (&pwd, ctx, GPG_CMD_SIGN, dlg, _("Sign & Encrypt"));
187              set_gpg_passphrase_cb( ctx, &pwd, GPG_CMD_SIGN, dlg, _("Sign & Encrypt") );              err = gpg_clip_sign_encrypt (ctx, signer, rset, force_trust);
188              if( force_trust )              release_gpg_passphrase_cb (&pwd);
189                  opt |= GPGME_CTRL_FORCETRUST;              free (rset);
190              err = gpgme_op_clip_sign_encrypt( ctx, rset, signer, opt );              free_if_alloc (signer);
191              memset( pwd.pwd, 0, sizeof pwd.pwd );              gpgme_release (ctx);
192              gpgme_recipients_release( rset );              if (gpgme_err_code (err) ==  GPG_ERR_BAD_PASSPHRASE)
193              free_if_alloc( signer );                  agent_del_cache (pwd.keyid);
194              if( err == GPGME_Bad_Passphrase )                  if (err) {
195                  agent_del_cache( pwd.keyid );                  msg_box (dlg, gpgme_strerror (err), _("Sign & Encrypt"), MB_ERR );
             if( err ) {  
                 gpgme_show_error( dlg, err, ctx, _("Sign & Encrypt"), MB_ERR );  
                 gpgme_release( ctx );  
196                  return FALSE;                  return FALSE;
197              }              }
198              gpgme_release( ctx );              show_msg (dlg, 1500, _("GnuPG Status: Finished"));
199              show_msg( dlg, 1500, _("GnuPG Status: Finished") );              EndDialog (dlg, TRUE);
             EndDialog( dlg, TRUE );  
200              return TRUE;              return TRUE;
201                            
202          case IDCANCEL:          case IDCANCEL:
# Line 162  clip_signenc_dlg_proc( HWND dlg, UINT ms Line 207  clip_signenc_dlg_proc( HWND dlg, UINT ms
207      }      }
208            
209      return FALSE;      return FALSE;
210  } /* clip_signenc_dlg_proc */  }

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26