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

Diff of /trunk/Src/wptClipSignDlg.cpp

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

revision 25 by twoaday, Wed Oct 12 10:04:26 2005 UTC revision 77 by twoaday, Mon Nov 14 15:01:01 2005 UTC
# Line 1  Line 1 
1  /* wptClipSignDlg.cpp - WinPT clipboard sign dialog  /* wptClipSignDlg.cpp - WinPT clipboard sign dialog
2   *      Copyright (C) 2000, 2001, 2002, 2003, 2005 Timo Schulz   *      Copyright (C) 2000, 2001, 2002, 2003, 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   */   */
   
 #include <windows.h>  
 #include <commctrl.h>  
   
 #include "../resource.h"  
 #include "wptTypes.h"  
 #include "wptAgent.h"  
 #include "wptNLS.h"  
 #include "wptGPG.h"  
 #include "wptCommonCtl.h"  
 #include "wptRegistry.h"  
 #include "wptKeylist.h"  
 #include "wptErrors.h"  
 #include "wptW32API.h"  
 #include "wptVersion.h"  
 #include "wptContext.h" /* for passphrase_s */  
 #include "wptDlgs.h"  
   
 /* Sign the clipboard contents with the key @keyid and wrap  
    text lines to @wraplen (0 disable line wrapping).  
    Return value: 0 on success. */  
 gpgme_error_t  
 gpg_clip_sign (gpgme_ctx_t ctx, const char *keyid, int wraplen)  
 {  
     gpgme_error_t err;  
     gpgme_data_t plain = NULL;  
     gpgme_data_t sig = NULL;  
     gpgme_key_t key = NULL;  
       
     if (!keyid)  
         return gpg_error (GPG_ERR_INV_ARG);  
       
     gpgme_set_armor (ctx, 1);  
       
     err = gpg_data_new_from_clipboard (&plain, wraplen);  
     if (err)  
         return err;  
   
     get_pubkey (keyid, &key);  
     if (key)  
         err = gpgme_signers_add (ctx, key);  
     else {  
         err = gpg_error (GPG_ERR_NO_PUBKEY);  
         goto leave;  
     }  
     err = gpgme_data_new (&sig);  
     if (err)  
         goto leave;      
     err = gpgme_op_sign (ctx, plain, sig, GPGME_SIG_MODE_CLEAR);  
     if (err)  
         goto leave;  
       
     gpg_data_release_and_set_clipboard (sig, 1);  
     sig = NULL;      
   
 leave:  
     if (plain)  
         gpgme_data_release (plain);  
     if (sig)  
         gpgme_data_release (sig);  
     return err;  
 }  
   
   
 /* This function is used when only one secret key is available.  
  * it doesn't make sense to offer a dialog for this case.  
  */  
 void  
 one_key_proc (HWND dlg)  
 {  
     char * signer;  
     gpgme_ctx_t ctx;  
     gpgme_error_t err;  
     passphrase_cb_s pwd;  
     int rc = 0;  
     int n = reg_prefs.word_wrap;  
       
     signer = get_gnupg_default_key ();  
     if (!signer) {  
         msg_box (dlg, _("Could not get default key."), _("Signing"), MB_ERR);  
         return;  
     }  
       
     err = gpgme_new (&ctx);  
     if (err)  
         BUG (dlg);  
   
     set_gpg_passphrase_cb (&pwd, ctx, GPG_CMD_SIGN, dlg, _("Signing"));  
     err = gpg_clip_sign (ctx, signer, n );  
     memset (pwd.pwd, 0, sizeof pwd.pwd);  
     if (gpgme_err_code (err) == GPG_ERR_BAD_PASSPHRASE)  
         agent_del_cache (pwd.keyid);  
     if (err)  
         msg_box (dlg, gpgme_strerror (err), _("Signing"), MB_ERR);  
     else  
         show_msg (dlg, 1500, _("GnuPG Status: Finished"));  
     gpgme_release (ctx);  
     free_if_alloc (signer);  
 }  
   
   
 /* Dialog box procedure for clipboard signing. */  
 BOOL CALLBACK  
 clip_sign_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)  
 {        
     static listview_ctrl_t lv = NULL;  
     gpg_keycache_t kc, sec_kc;  
     gpgme_ctx_t ctx;  
     gpgme_error_t err;  
     passphrase_cb_s pwd;  
     int lv_idx = 0;  
     int rc = 0, no_signer = 0;  
     char * signer = NULL;  
       
     switch( msg ) {  
     case WM_INITDIALOG:  
     #ifndef LANG_DE  
         SetWindowText( dlg, _("Signing") );  
     #endif  
         kc = keycache_get_ctx (KEYCACHE_PUB);  
         if (!kc)  
             BUG( NULL );  
         sec_kc = keycache_get_ctx (KEYCACHE_PRV);  
         if (!sec_kc)  
             BUG (dlg);  
         if (gpg_keycache_get_size (sec_kc) == 1) {  
             one_key_proc (dlg);  
             EndDialog (dlg, TRUE);  
             return FALSE;  
         }  
         lv = keylist_load (GetDlgItem (dlg, IDC_SIGN_KEYLIST), kc, sec_kc,  
                            KEYLIST_SIGN, KEY_SORT_USERID);  
         center_window (dlg, NULL);  
         SetForegroundWindow (dlg);  
         set_active_window (dlg);  
         return FALSE;    
           
     case WM_DESTROY:  
         reset_active_window ();  
         if (lv) {  
             keylist_delete (lv);  
             lv = NULL;  
         }  
         return FALSE;  
           
     case WM_NOTIFY:  
         NMHDR * notify;  
         notify = (NMHDR *)lparam;  
         if( notify && notify->code == NM_DBLCLK  
             && notify->idFrom == IDC_SIGN_KEYLIST )  
             PostMessage( dlg, WM_COMMAND, MAKEWPARAM(IDOK, 0), NULL );  
         return TRUE;  
           
     case WM_SYSCOMMAND:  
         if( LOWORD (wparam) == SC_CLOSE )  
             EndDialog(dlg, TRUE);  
         return FALSE;  
           
     case WM_COMMAND:  
         switch (LOWORD (wparam)) {  
         case IDOK:  
             signer = get_gnupg_default_key ();  
             if (!signer) {  
                 msg_box( dlg, _("Could not get default key."), _("Signing"), MB_ERR );  
                 return FALSE;  
             }  
             if (listview_count_items (lv, 0) == 1) {  
                 listview_get_item_text (lv, 0, 1, signer, sizeof signer-1);  
                 no_signer = 0;  
             }  
             else if ((lv_idx = listview_get_curr_pos (lv)) == -1) {  
                 rc = log_box (_("Signing"), MB_YESNO,  
                               _("No key was chosen.\nUse the GPG default key '%s'?"),  
                               signer);  
                 if (rc == IDNO) {  
                     free_if_alloc (signer);  
                     return FALSE;  
                 }  
                 no_signer = 1;  
             }  
             if (!no_signer) {  
                 free_if_alloc (signer);  
                 signer = new char[32+1];  
                 if (!signer)  
                     BUG (NULL);  
                 listview_get_item_text (lv, lv_idx, 1, signer, 32);  
             }  
             err = gpgme_new (&ctx);  
             if (err)  
                 BUG (dlg);  
             set_gpg_passphrase_cb (&pwd, ctx, GPG_CMD_SIGN, dlg, _("Signing"));  
             err = gpg_clip_sign (ctx, signer, reg_prefs.word_wrap);  
             free_if_alloc (signer);  
             release_gpg_passphrase_cb (&pwd);  
   
             if (pwd.cancel && gpgme_err_code(err) == GPG_ERR_BAD_PASSPHRASE) {  
                 /* The user hit the cancel button or bad passphrase */  
                 gpgme_release (ctx);  
                 EndDialog (dlg, TRUE);  
                 return TRUE;  
             }  
             if (err) {  
                 msg_box (dlg, gpgme_strerror (err), _("Signing"), MB_ERR);  
                 gpgme_release (ctx);  
                 return FALSE;  
             }  
             else                      
                 show_msg( dlg, 1500, _("GnuPG Status: Finished") );  
             gpgme_release (ctx);  
             EndDialog (dlg, TRUE);  
             return TRUE;  
               
         case IDCANCEL:  
             EndDialog (dlg, FALSE);  
             return FALSE;  
         }  
         break;  
     }  
       
     return FALSE;  
 }  
21    
22    #ifdef HAVE_CONFIG_H
23    #include <config.h>
24    #endif
25    
26    #include <windows.h>
27    #include <commctrl.h>
28    
29    #include "resource.h"
30    #include "wptTypes.h"
31    #include "wptAgent.h"
32    #include "wptNLS.h"
33    #include "wptCrypto.h"
34    #include "wptGPG.h"
35    #include "wptCommonCtl.h"
36    #include "wptRegistry.h"
37    #include "wptKeylist.h"
38    #include "wptErrors.h"
39    #include "wptW32API.h"
40    #include "wptVersion.h"
41    #include "wptContext.h" /* for passphrase_s */
42    #include "wptDlgs.h"
43    
44    
45    /* Sign the clipboard contents with the key @keyid and wrap
46       text lines to @wraplen (0 disable line wrapping).
47       Return value: 0 on success. */
48    gpgme_error_t
49    gpg_clip_sign (gpgme_ctx_t ctx, const char *keyid, int wraplen)
50    {
51        gpgme_error_t err;
52        gpgme_data_t plain = NULL;
53        gpgme_data_t sig = NULL;
54        gpgme_key_t key = NULL;
55        
56        if (!keyid)
57            return gpg_error (GPG_ERR_INV_ARG);
58        
59        gpgme_set_armor (ctx, 1);
60        
61        err = gpg_data_new_from_clipboard (&plain, wraplen);
62        if (err)
63            return err;
64    
65        get_pubkey (keyid, &key);
66        if (key)
67            err = gpgme_signers_add (ctx, key);
68        else {
69            err = gpg_error (GPG_ERR_NO_PUBKEY);
70            goto leave;
71        }
72        err = gpgme_data_new (&sig);
73        if (err)
74            goto leave;    
75        err = gpgme_op_sign (ctx, plain, sig, GPGME_SIG_MODE_CLEAR);
76        if (err)
77            goto leave;
78        
79        gpg_data_release_and_set_clipboard (sig, 1);
80        sig = NULL;    
81    
82    leave:
83        if (plain)
84            gpgme_data_release (plain);
85        if (sig)
86            gpgme_data_release (sig);
87        return err;
88    }
89    
90    
91    /* This function is used when only one secret key is available.
92       it doesn't make sense to offer a dialog for this case. */
93    void
94    one_key_proc (HWND dlg)
95    {
96        char *signer;
97        gpgme_ctx_t ctx;
98        gpgme_error_t err;
99        passphrase_cb_s pwd;
100        int n = reg_prefs.word_wrap;
101        
102        signer = get_gnupg_default_key ();
103        if (!signer) {
104            msg_box (dlg, _("Could not get default key."), _("Signing"), MB_ERR);
105            return;
106        }
107        
108        err = gpgme_new (&ctx);
109        if (err)
110            BUG (dlg);
111    
112        set_gpg_passphrase_cb (&pwd, ctx, GPG_CMD_SIGN, dlg, _("Signing"));
113        err = gpg_clip_sign (ctx, signer, n);
114        if (gpgme_err_code (err) == GPG_ERR_BAD_PASSPHRASE)
115            agent_del_cache (pwd.keyid);
116        if (err)
117            msg_box (dlg, gpgme_strerror (err), _("Signing"), MB_ERR);
118        else
119            show_msg (dlg, 1500, _("GnuPG Status: Finished"));
120        gpgme_release (ctx);
121        free_if_alloc (signer);
122        release_gpg_passphrase_cb (&pwd);
123    }
124    
125    
126    /* Dialog box procedure for clipboard signing. */
127    BOOL CALLBACK
128    clip_sign_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
129    {      
130        static listview_ctrl_t lv = NULL;
131        gpg_keycache_t kc, sec_kc;
132        gpgme_ctx_t ctx;
133        gpgme_error_t err;
134        passphrase_cb_s pwd;
135        int lv_idx = 0;
136        int rc = 0, no_signer = 0;
137        char * signer = NULL;
138        
139        switch( msg ) {
140        case WM_INITDIALOG:
141            SetWindowText (dlg, _("Signing"));
142    
143            kc = keycache_get_ctx (KEYCACHE_PUB);
144            if (!kc)
145                BUG( NULL );
146            sec_kc = keycache_get_ctx (KEYCACHE_PRV);
147            if (!sec_kc)
148                BUG (dlg);
149            if (gpg_keycache_get_size (sec_kc) == 1) {
150                one_key_proc (dlg);
151                EndDialog (dlg, TRUE);
152                return FALSE;
153            }
154            lv = keylist_load (GetDlgItem (dlg, IDC_SIGN_KEYLIST), kc, sec_kc,
155                               KEYLIST_SIGN, KEY_SORT_USERID);
156            center_window (dlg, NULL);
157            SetForegroundWindow (dlg);
158            set_active_window (dlg);
159            return FALSE;  
160            
161        case WM_DESTROY:
162            reset_active_window ();
163            if (lv) {
164                keylist_delete (lv);
165                lv = NULL;
166            }
167            return FALSE;
168            
169        case WM_NOTIFY:
170            NMHDR * notify;
171            notify = (NMHDR *)lparam;
172            if( notify && notify->code == NM_DBLCLK
173                && notify->idFrom == IDC_SIGN_KEYLIST )
174                PostMessage (dlg, WM_COMMAND, MAKEWPARAM(IDOK, 0), 0);
175            return TRUE;
176            
177        case WM_SYSCOMMAND:
178            if( LOWORD (wparam) == SC_CLOSE )
179                EndDialog(dlg, TRUE);
180            return FALSE;
181            
182        case WM_COMMAND:
183            switch (LOWORD (wparam)) {
184            case IDOK:
185                signer = get_gnupg_default_key ();
186                if (!signer) {
187                    msg_box( dlg, _("Could not get default key."), _("Signing"), MB_ERR );
188                    return FALSE;
189                }
190                if (listview_count_items (lv, 0) == 1) {
191                    listview_get_item_text (lv, 0, 1, signer, sizeof signer-1);
192                    no_signer = 0;
193                }
194                else if ((lv_idx = listview_get_curr_pos (lv)) == -1) {
195                    rc = log_box (_("Signing"), MB_YESNO,
196                                  _("No key was chosen.\nUse the GPG default key '%s'?"),
197                                  signer);
198                    if (rc == IDNO) {
199                        free_if_alloc (signer);
200                        return FALSE;
201                    }
202                    no_signer = 1;
203                }
204                if (!no_signer) {
205                    free_if_alloc (signer);
206                    signer = new char[32+1];
207                    if (!signer)
208                        BUG (NULL);
209                    listview_get_item_text (lv, lv_idx, 1, signer, 32);
210                }
211                err = gpgme_new (&ctx);
212                if (err)
213                    BUG (dlg);
214                set_gpg_passphrase_cb (&pwd, ctx, GPG_CMD_SIGN, dlg, _("Signing"));
215                err = gpg_clip_sign (ctx, signer, reg_prefs.word_wrap);
216                free_if_alloc (signer);
217                release_gpg_passphrase_cb (&pwd);
218    
219                if (pwd.cancel && gpgme_err_code(err) == GPG_ERR_BAD_PASSPHRASE) {
220                    /* The user hit the cancel button or bad passphrase */
221                    gpgme_release (ctx);
222                    EndDialog (dlg, TRUE);
223                    return TRUE;
224                }
225                if (err) {
226                    msg_box (dlg, gpgme_strerror (err), _("Signing"), MB_ERR);
227                    gpgme_release (ctx);
228                    return FALSE;
229                }
230                else                    
231                    show_msg( dlg, 1500, _("GnuPG Status: Finished") );
232                gpgme_release (ctx);
233                EndDialog (dlg, TRUE);
234                return TRUE;
235                
236            case IDCANCEL:
237                EndDialog (dlg, FALSE);
238                return FALSE;
239            }
240            break;
241        }
242        
243        return FALSE;
244    }

Legend:
Removed from v.25  
changed lines
  Added in v.77

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26