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

Diff of /trunk/Src/wptKeysignDlg.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 278 by twoaday, Mon Jan 15 22:02:04 2007 UTC
# Line 1  Line 1 
1  /* wptKeysignDlg.cpp - Key signing dialog  /* wptKeysignDlg.cpp - Key signing dialog
2   *      Copyright (C) 2001-2005 Timo Schulz   *      Copyright (C) 2001-2006 Timo Schulz
3   *   *
4   * This file is part of WinPT.   * This file is part of WinPT.
5   *   *
6   * WinPT is free software; you can redistribute it and/or modify   * 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   * it under the terms of the GNU General Public License as published by
8   * the Free Software Foundation; either version 2 of the License, or   * the Free Software Foundation; either version 2 of the License, or
9   * (at your option) any later version.   * (at your option) any later version.
10   *   *
11   * WinPT is distributed in the hope that it will be useful,   * WinPT is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.   * GNU General Public License for more details.
15   *   */
16   * You should have received a copy of the GNU General Public License  #ifdef HAVE_CONFIG_H
17   * along with WinPT; if not, write to the Free Software Foundation,  #include <config.h>
18   * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA  #endif
  */  
   
 #include <windows.h>  
 #include <commctrl.h>  
   
 #include "../resource.h"  
 #include "wptGPG.h"  
 #include "wptNLS.h"  
 #include "wptW32API.h"  
 #include "wptVersion.h"  
 #include "wptTypes.h"  
 #include "wptErrors.h"  
 #include "wptCommonCtl.h"  
 #include "wptContext.h"  
 #include "wptDlgs.h"  
 #include "wptUTF8.h"  
 #include "wptRegistry.h"  
 #include "wptKeyList.h"  
 #include "wptKeyEdit.h"  
   
 static int sig_class_choice = 0;  
   
 /* Return a beautified printable fingerprint of @fpr. */  
 static const char*  
 get_printable_fpr (const char *fpr)  
 {  
     static char pfpr[64];      
     int pos = 0;  
     size_t i;  
   
     for (i = 0; i < strlen (fpr); i += 4) {  
         pfpr[pos++] = fpr[i];  
         pfpr[pos++] = fpr[i+1];  
         pfpr[pos++] = fpr[i+2];  
         pfpr[pos++] = fpr[i+3];  
         pfpr[pos++] = ' ';  
     }  
     return pfpr;  
 }  
   
   
 /* Return human friendly information about the key @key. */  
 static const char*  
 get_keyinfo (gpgme_key_t key)  
 {  
     static char buf[64+16];  
     struct winpt_key_s k;  
   
     memset (&k, 0, sizeof (k));  
     winpt_get_seckey (key->subkeys->keyid, &k);  
     _snprintf (buf, DIM (buf)-1-16, "%d-bit %s key, ID %s",  
         key->subkeys->length,  
         get_key_pubalgo (key->subkeys->pubkey_algo),  
         key->subkeys->keyid+8);  
     if (k.ext->gloflags.divert_to_card)  
         strcat (buf, " (Card)");  
     return buf;  
 }  
   
   
 /* Fill the secret key combo-box with all entries from the cache.  
    @dlg is the handle to the combo-box. @keyid show which key to skip.  
    Return value: 0 on success. */  
 static int  
 do_fill_seckeylist (HWND dlg, const char *keyid)  
 {  
     gpg_keycache_t sec;  
     gpgme_key_t pk;  
     const char * s;  
     char * uid, * p;      
     int i = 0, n=0;  
   
     sec = keycache_get_ctx (0);  
     if (!sec)  
         BUG (0);  
     gpg_keycache_rewind (sec);  
     while (!gpg_keycache_next_key (sec, 1, &pk)) {  
         if (!pk)  
             continue;  
         s = pk->subkeys->keyid;  
         if (!strcmp (s, keyid))  
             continue;  
         /* skip all ElGamal sign+encrypt keys */  
         if (pk->subkeys->pubkey_algo == GPGME_PK_ELG)  
             continue;  
         /* make sure the public key is okay not: revoked, expired or disabled. */  
         if (pk->expired ||pk->revoked || pk->disabled)  
             continue;  
         s = pk->uids->name;  
         if (!s)  
             continue;  
         uid = utf8_to_wincp (s, strlen (s));  
         p = new char[strlen (uid) + 64];  
         if (!p)  
             BUG (NULL);  
         _snprintf (p, strlen (uid) + 63, "%s (%s)", uid, get_keyinfo (pk));  
         SendDlgItemMessage (dlg, IDC_KEYSIGN_KEYLIST, CB_ADDSTRING, i, (LPARAM)(char *)p);  
         SendDlgItemMessage (dlg, IDC_KEYSIGN_KEYLIST, CB_SETITEMDATA, i++, (LPARAM)(DWORD)pk);  
         free_if_alloc (p);  
         free (uid);  
         n++;  
     }  
     SendDlgItemMessage (dlg, IDC_KEYSIGN_KEYLIST, CB_SETCURSEL, 0, 0);  
     if (!n)  
         return -1;  
     return 0;  
 }  
   
   
 /* Check if the selected key is protected and en- or disable the  
    passphrase control. */  
 static void  
 do_check_protection (HWND dlg)  
 {  
     int idx, protec;  
     gpgme_key_t key;  
     struct winpt_key_s k;  
   
     idx = SendDlgItemMessage( dlg, IDC_KEYSIGN_KEYLIST, CB_GETCURSEL, 0, 0 );  
     key = (gpgme_key_t)SendDlgItemMessage( dlg, IDC_KEYSIGN_KEYLIST, CB_GETITEMDATA, (WPARAM)idx, 0 );  
     if (key) {  
         winpt_get_seckey (key->subkeys->keyid, &k);  
         protec = k.is_protected;  
         if (!protec)  
             protec = k.ext->gloflags.divert_to_card;  
         EnableWindow (GetDlgItem (dlg, IDC_KEYSIGN_PASSPHRASE), protec? TRUE : FALSE);  
     }  
 }  
   
   
 /* Dialog box procedure to choose the signature class. */  
 BOOL CALLBACK  
 sig_class_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)  
 {  
     switch (msg) {  
     case WM_INITDIALOG:  
         SetWindowText (dlg, _("Choose Signature Class"));  
         SetDlgItemText (dlg, IDC_SIGCLASS_TITLEINF, _("How carefully have you verified the key you are about to sign actually belongs to the person? If you don't know what to anwser, use \"0\"."));  
         SetDlgItemText (dlg, IDC_SIGCLASS_CLASS0, _("(0) I will not answer (default)"));  
         SetDlgItemText (dlg, IDC_SIGCLASS_CLASS1, _("(1) I have not checked at all."));  
         SetDlgItemText (dlg, IDC_SIGCLASS_CLASS2, _("(2) I have done causal checking."));  
         SetDlgItemText (dlg, IDC_SIGCLASS_CLASS3, _("(3) I have done very careful checkings."));  
         CheckDlgButton (dlg, IDC_SIGCLASS_CLASS0, BST_CHECKED);  
         SetForegroundWindow (dlg);  
         center_window (dlg, NULL);  
         return TRUE;  
   
     case WM_COMMAND:  
         switch( LOWORD( wparam ) ) {  
         case IDOK:  
             if (IsDlgButtonChecked (dlg, IDC_SIGCLASS_CLASS0))  
                 sig_class_choice = 0;  
             else if (IsDlgButtonChecked (dlg, IDC_SIGCLASS_CLASS1))  
                 sig_class_choice = 1;  
             else if (IsDlgButtonChecked (dlg, IDC_SIGCLASS_CLASS2))  
                 sig_class_choice = 2;  
             else if (IsDlgButtonChecked (dlg, IDC_SIGCLASS_CLASS3))  
                 sig_class_choice = 3;  
             else  
                 sig_class_choice = 0;  
             EndDialog (dlg, TRUE);  
             return TRUE;  
         }  
         break;  
     }  
   
     return FALSE;  
 }  
   
   
 /* Return the humand friendly expiration date of @key. */  
 static const char*  
 get_expire_date (gpgme_key_t key)  
 {  
     static char tmp[64];  
   
     u32 u = key->subkeys->expires;  
     if (!u)  
         return "never";  
     return get_key_expire_date (u);  
 }  
   
   
 /* Dialog box procedure to sign a key. */  
 BOOL CALLBACK  
 keysign_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)  
 {  
     static winpt_key_t key;  
     GpgKeyEdit *ke;  
     gpgme_error_t err;  
     gpgme_key_t k;  
     SYSTEMTIME st;  
     HWND h;  
     char keymsg[4096], pwd[256], *uid = NULL;  
     const char *keyid, *s;  
     u32 created;  
     int type, expires=0, idx;  
       
     switch ( msg )  {  
     case WM_INITDIALOG:  
         if (lparam == NULL)  
             dlg_fatal_error (dlg, "Could not get dialog param.");  
         #ifndef LANG_DE  
         SetWindowText (dlg, _("Key Signing"));  
         #endif  
         key = (winpt_key_t) lparam;  
         created = key->ctx->subkeys->timestamp;  
         s = key->ctx->uids->uid;  
         if (s)  
             uid = utf8_to_wincp (s, strlen (s));  
         _snprintf (keymsg, sizeof keymsg -1,  
                    _("pub %d/%s  created: %s    expires: %s\n\n"  
                      "Primary key fingerprint: %s\n\n"  
                      "\t%s\n\n"  
                      "\nAre you really sure that you want to sign this key with YOUR key?\n"),  
                    key->ctx->subkeys->length,  
                    key->ctx->subkeys->keyid+8,  
                    get_key_created (key->ctx->subkeys->timestamp),  
                    get_expire_date (key->ctx),  
                    get_printable_fpr (key->ctx->subkeys->fpr),  
                    uid);  
         free (uid);  
         s = key->ctx->subkeys->keyid;  
         if (do_fill_seckeylist (dlg, s)) {  
             msg_box( dlg, _("No valid secret key found."), _("Key Signing"), MB_ERR );  
             EndDialog( dlg, FALSE );  
         }  
         SetDlgItemText (dlg, IDC_KEYSIGN_INFOS, keymsg);  
         #ifndef LANG_DE  
         SetDlgItemText (dlg, IDC_KEYSIGN_LOCAL, _("Sign local only (non exportable signature)"));  
         SetDlgItemText (dlg, IDC_KEYSIGN_EXPSIG, _("Signature expires on"));  
         SetDlgItemText (dlg, IDC_KEYSIGN_NREV, _("Sign non-revocably"));  
         #endif  
         CheckDlgButton (dlg, IDC_KEYSIGN_LOCAL, BST_CHECKED);  
         CheckDlgButton (dlg, IDC_KEYSIGN_EXPSIG, BST_UNCHECKED);  
         CheckDlgButton (dlg, IDC_KEYSIGN_ASKLEVEL, BST_UNCHECKED);  
         EnableWindow (GetDlgItem (dlg, IDC_KEYSIGN_EXPIRES), FALSE);  
         if (reg_prefs.expert == 0)  
             ShowWindow (GetDlgItem (dlg, IDC_KEYSIGN_NREV), SW_HIDE);  
         SetForegroundWindow( dlg );  
         h = GetDlgItem( dlg, IDC_KEYSIGN_PASSPHRASE );  
         SetFocus( h );  
         return FALSE;  
       
     case WM_DESTROY:  
         sig_class_choice = 0;  
         break;  
   
     case WM_SYSCOMMAND:  
         if( LOWORD( wparam ) == SC_CLOSE ) {  
             SetDlgItemText( dlg, IDC_KEYSIGN_PASSPHRASE, "" );  
             EndDialog( dlg, TRUE );  
         }  
         return FALSE;  
           
     case WM_COMMAND:  
         if( HIWORD( wparam ) == CBN_SELCHANGE ) {  
             do_check_protection( dlg );  
             break;  
         }  
         if (HIWORD (wparam) == BN_CLICKED && LOWORD (wparam) == IDC_KEYSIGN_EXPSIG) {  
             int enable = IsDlgButtonChecked (dlg, IDC_KEYSIGN_EXPSIG);  
             EnableWindow (GetDlgItem (dlg, IDC_KEYSIGN_EXPIRES), enable? TRUE : FALSE);  
         }  
   
         switch( LOWORD( wparam ) ) {  
         case IDOK:  
             if (IsDlgButtonChecked (dlg, IDC_KEYSIGN_ASKLEVEL))  
                 dialog_box_param (glob_hinst, (LPCSTR)IDD_WINPT_SIGCLASS, dlg,  
                                   sig_class_dlg_proc, (LPARAM)NULL,  
                                   _("Choose Signature Class"),  IDS_WINPT_SIGCLASS);  
             type = IsDlgButtonChecked (dlg, IDC_KEYSIGN_LOCAL);  
             if (type)  
                 type = GPG_EDITKEY_LSIGN;  
             else  
                 type = GPG_EDITKEY_SIGN;  
   
             if (reg_prefs.expert && IsDlgButtonChecked (dlg, IDC_KEYSIGN_NREV)) {  
                 type = GPG_EDITKEY_NRSIGN;  
                 if (type == GPG_EDITKEY_LSIGN)  
                     type = GPG_EDITKEY_NRLSIGN;  
             }  
             if (IsDlgButtonChecked (dlg, IDC_KEYSIGN_EXPSIG)) {  
                 expires = 1;  
                 DateTime_GetSystemtime (GetDlgItem (dlg, IDC_KEYSIGN_EXPIRES), &st);  
                 sprintf (keymsg, "%04d-%02d-%02d", st.wYear, st.wMonth, st.wDay);  
             }  
   
             /* XXX: check for --ask-cert-level and --ask-cert-expire in the gpg.conf  
                     if an advanced button is checked and offer to add it to the config  
                     file. */  
   
             GetDlgItemText( dlg, IDC_KEYSIGN_PASSPHRASE, pwd, DIM (pwd)-1);  
             keyid = key->ctx->subkeys->keyid;  
             if( !keyid ) {  
                 msg_box( dlg, _("Could not get Key ID from key."), _("Key Signing"), MB_ERR );  
                 return TRUE;  
             }  
             ke = new GpgKeyEdit (keyid);  
             if (!ke)  
                 BUG (NULL);  
             ke->setPassphrase (pwd);  
             idx = SendDlgItemMessage (dlg, IDC_KEYSIGN_KEYLIST, CB_GETCURSEL, 0, 0);  
             k = (gpgme_key_t)SendDlgItemMessage (dlg, IDC_KEYSIGN_KEYLIST,  
                                                  CB_GETITEMDATA, (WPARAM)idx, 0);  
             if (k)  
                 ke->setLocalUser (k);  
   
             err = ke->signKey (type, sig_class_choice, expires? keymsg : "0");  
             memset (&pwd, 0, sizeof pwd);            
             if (err) {  
                 delete ke;  
                 msg_box (dlg, gpgme_strerror (err), _("Key Signing"), MB_ERR);  
                 return TRUE;  
             }  
             if (ke->getResultValue () != 0)  
                 msg_box (dlg, _("This key is already signed by your key"), _("Key Signing"), MB_INFO);  
             else {  
                 status_box (dlg, _("Key successfully signed."), PGM_NAME);  
                 key->update = 1;  
             }  
             delete ke;  
             EndDialog (dlg, TRUE);  
             return TRUE;  
               
         case IDCANCEL:  
             EndDialog (dlg, FALSE);  
             return TRUE;  
         }  
         break;  
     }  
       
     return FALSE;  
 }  
19    
20    #include <windows.h>
21    #include <commctrl.h>
22    #include <time.h>
23    #include <assert.h>
24    
25    #include "resource.h"
26    #include "wptGPG.h"
27    #include "wptNLS.h"
28    #include "wptW32API.h"
29    #include "wptVersion.h"
30    #include "wptTypes.h"
31    #include "wptErrors.h"
32    #include "wptCommonCtl.h"
33    #include "wptContext.h"
34    #include "wptDlgs.h"
35    #include "wptUTF8.h"
36    #include "wptRegistry.h"
37    #include "wptKeylist.h"
38    #include "wptKeyEdit.h"
39    #include "StringBuffer.h"
40    
41    
42    void key_unload_photo (HWND dlg);
43    int  key_load_photo (HWND dlg, gpgme_key_t key, gpgme_validity_t *r_valid);
44    const char* get_photo_tmpname (HWND dlg);
45    
46    /* Return a beautified printable fingerprint of @fpr. */
47    static const char*
48    get_printable_fpr (const char *fpr)
49    {
50        static char pfpr[64];
51        int pos = 0;
52        size_t i;
53    
54        for (i = 0; i < strlen (fpr); i += 4) {
55            pfpr[pos++] = fpr[i];
56            pfpr[pos++] = fpr[i+1];
57            pfpr[pos++] = fpr[i+2];
58            pfpr[pos++] = fpr[i+3];
59            pfpr[pos++] = ' ';
60        }
61        return pfpr;
62    }
63    
64    
65    /* Return human friendly information about the key @key. */
66    const char*
67    get_keyinfo (gpgme_key_t key)
68    {
69        static char buf[64+16];
70        struct winpt_key_s k;
71    
72        memset (&k, 0, sizeof (k));
73        if (winpt_get_seckey (key->subkeys->keyid, &k))
74            BUG (0);
75        _snprintf (buf, DIM (buf)-1-16, _("%d-bit %s key, ID 0x%s"),
76                   key->subkeys->length,
77                   get_key_pubalgo (key->subkeys->pubkey_algo),
78                   key->subkeys->keyid+8);
79        if (k.ext->gloflags.divert_to_card)
80            strcat (buf, " (Card)");
81        return buf;
82    }
83    
84    
85    /* Fill the secret key combo-box with all entries from the cache.
86       @dlg is the handle to the combo-box. @keyid show which key to skip.
87       Return value: 0 on success. */
88    static int
89    do_fill_seckeylist (HWND dlg, const char *keyid)
90    {
91        gpg_keycache_t sec;
92        gpgme_key_t pk, defkey;
93        const char *s;
94        char *uid;
95        int i = 0, n = 0, curr_sel = 0;
96    
97        sec = keycache_get_ctx (0);
98        gpg_keycache_get_default_key (sec, &defkey);
99        gpg_keycache_rewind (sec);
100        while (!gpg_keycache_next_key (sec, 1, &pk)) {
101            StringBuffer p;
102    
103            if (!pk)
104                continue;
105            s = pk->subkeys->keyid;
106            if (!strcmp (s, keyid))
107                continue;
108            /* skip all ElGamal sign+encrypt keys */
109            if (pk->subkeys->pubkey_algo == GPGME_PK_ELG)
110                continue;
111            /* make sure the public key is okay not: revoked, expired or disabled. */
112            if (pk->expired ||pk->revoked || pk->disabled)
113                continue;
114            s = pk->uids->name;
115            if (!s)
116                continue;
117            if (defkey && !strcmp (defkey->subkeys->keyid, pk->subkeys->keyid))
118                curr_sel = i;
119    
120            uid = utf8_to_native (s);
121            p = uid;
122            p = p + " (" + get_keyinfo (pk) + ")";
123            SendDlgItemMessage (dlg, IDC_KEYSIGN_KEYLIST,
124                                CB_ADDSTRING, i, (LPARAM)(char *)p.getBuffer ());
125            SendDlgItemMessage (dlg, IDC_KEYSIGN_KEYLIST,
126                                CB_SETITEMDATA, i++, (LPARAM)(DWORD)pk);
127            safe_free (uid);
128            n++;
129        }
130        SendDlgItemMessage (dlg, IDC_KEYSIGN_KEYLIST,
131                            CB_SETCURSEL, (WPARAM)curr_sel, 0);
132        if (!n)
133            return -1;
134        return 0;
135    }
136    
137    
138    /* Check if the selected key is protected and en- or disable the
139       passphrase control. */
140    static void
141    do_check_protection (HWND dlg)
142    {
143        gpgme_key_t key;
144        struct winpt_key_s k;
145        int idx, protec;    
146    
147        idx = SendDlgItemMessage (dlg, IDC_KEYSIGN_KEYLIST, CB_GETCURSEL, 0, 0);
148        key = (gpgme_key_t)SendDlgItemMessage (dlg, IDC_KEYSIGN_KEYLIST,
149                                               CB_GETITEMDATA, (WPARAM)idx, 0);
150        if (key) {
151            if (winpt_get_seckey (key->subkeys->keyid, &k))
152                BUG (0);
153            protec = k.is_protected;
154            if (!protec)
155                protec = k.ext->gloflags.divert_to_card;
156            EnableWindow (GetDlgItem (dlg, IDC_KEYSIGN_PASSPHRASE), protec? TRUE : FALSE);
157        }
158    }
159    
160    
161    /* Show the photographic ID of the key in a separate dialog window. */
162    BOOL CALLBACK
163    showphoto_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
164    {
165        static winpt_key_t key;
166        char buf[200];
167    
168        switch (msg) {
169        case WM_INITDIALOG:
170            key = (winpt_key_t)lparam;
171            if (!key)
172                BUG (0);
173            key_load_photo (dlg, key->ctx, NULL);
174            _snprintf (buf, DIM (buf)-1, _("Photo of %s"), key->uid);
175            SetWindowText (dlg, buf);
176            SetForegroundWindow (dlg);
177            break;
178    
179        case WM_PAINT:
180            if (key->ext && key->ext->attrib.len > 0) {
181                POINT p;
182                p.x = p.y = 0;
183                PTD_jpg_show (GetDlgItem (dlg, IDC_SHOWPHOTO_IMG),
184                                &p, get_photo_tmpname (dlg));
185            }
186            break;
187    
188        case WM_DESTROY:
189            key_unload_photo (dlg);
190            break;
191    
192        case WM_COMMAND:
193            switch (LOWORD (wparam)) {
194            case IDCANCEL:
195            case IDOK:
196                EndDialog (dlg, TRUE);
197                break;
198            }
199            break;
200        }
201    
202        return FALSE;
203    }
204    
205    
206    /* Dialog box procedure to choose the signature class. */
207    BOOL CALLBACK
208    sig_class_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
209    {
210        HWND parent;
211        int sig_class = 0;
212    
213        switch (msg) {
214        case WM_INITDIALOG:
215            parent = (HWND)lparam;
216            SetWindowText (dlg, _("Choose Signature Class"));
217            SetDlgItemText (dlg, IDC_SIGCLASS_TITLEINF,
218                            _("How carefully have you verified the key you are about to sign actually belongs to the person? If you don't know what to anwser, use \"0\"."));
219            SetDlgItemText (dlg, IDC_SIGCLASS_CLASS0,
220                            _("(0) I will not answer (default)"));
221            SetDlgItemText (dlg, IDC_SIGCLASS_CLASS1,
222                            _("(1) I have not checked at all."));
223            SetDlgItemText (dlg, IDC_SIGCLASS_CLASS2,
224                            _("(2) I have done causal checking."));
225            SetDlgItemText (dlg, IDC_SIGCLASS_CLASS3,
226                            _("(3) I have done very careful checking."));
227            CheckDlgButton (dlg, IDC_SIGCLASS_CLASS0, BST_CHECKED);
228            SetForegroundWindow (dlg);
229            center_window (dlg, parent? parent : NULL);
230            return TRUE;
231    
232        case WM_COMMAND:
233            switch (LOWORD (wparam)) {
234            case IDOK:
235                if (IsDlgButtonChecked (dlg, IDC_SIGCLASS_CLASS0))
236                    sig_class = 0;
237                else if (IsDlgButtonChecked (dlg, IDC_SIGCLASS_CLASS1))
238                    sig_class = 1;
239                else if (IsDlgButtonChecked (dlg, IDC_SIGCLASS_CLASS2))
240                    sig_class = 2;
241                else if (IsDlgButtonChecked (dlg, IDC_SIGCLASS_CLASS3))
242                    sig_class = 3;
243                else
244                    sig_class = 0;
245                EndDialog (dlg, sig_class);
246                return TRUE;
247    
248            case IDCANCEL:
249                EndDialog (dlg, 0);
250                return TRUE;
251            }
252            break;
253        }
254    
255        return FALSE;
256    }
257    
258    
259    /* Return the humand friendly expiration date of @key. */
260    static const char*
261    get_expire_date (gpgme_key_t key)
262    {
263        u32 u = key->subkeys->expires;
264        if (!u)
265            return _("never");
266        return get_key_expire_date (u);
267    }
268    
269    
270    /* Check if the given system time @st points to today. */
271    static int
272    date_is_today (SYSTEMTIME *st)
273    {
274        time_t t;
275        struct tm *tm;
276    
277        t = time (NULL);
278        tm = localtime (&t);
279        if (st->wDay == tm->tm_mday &&
280            st->wYear == tm->tm_year+1900 &&
281            st->wMonth == tm->tm_mon+1)
282            return -1;
283        return 0;
284    }
285    
286    
287    /* Display all needed information about the key @key in the dialog @dlg. */
288    static void
289    display_keyinfo (HWND dlg, winpt_key_t key)
290    {
291        StringBuffer kinf;
292        struct native_uid_s *u;
293        char tmp[256];
294        int len=0;
295      
296        _snprintf (tmp, DIM (tmp) -1,
297                       _("pub %d/0x%s   created: %s    expires: %s\n\n"
298                         "Primary key fingerprint: %s\n\n"),
299                       key->ctx->subkeys->length,
300                       key->ctx->subkeys->keyid+8,
301                       get_key_created (key->ctx->subkeys->timestamp),
302                       get_expire_date (key->ctx),
303                       get_printable_fpr (key->ctx->subkeys->fpr));    
304        kinf = tmp;    
305        for (len=0, u = key->ext->uids; u; u = u->next) {
306            if (u->revoked)
307                continue;
308            if (len++ > 2)
309                break;
310            kinf = kinf + "\t\"" + u->uid + "\"\n";
311        }
312        kinf = kinf + _("\n\nAre you really sure that you want to sign this key with YOUR key?\n");
313        SetDlgItemText (dlg, IDC_KEYSIGN_INFOS, kinf.getBuffer ());
314    }
315    
316    
317    /* Perform the actual procedure to sign the key. */
318    static BOOL
319    on_click_ok (HWND dlg, winpt_key_t key)
320    {
321        gpgme_key_t k;
322        gpgme_error_t err;
323        SYSTEMTIME st;
324        GpgKeyEdit ke;
325        int sig_class = 0;
326        char keymsg[64], *pwd;
327        int type, expires=0, idx;
328    
329        if (IsDlgButtonChecked (dlg, IDC_KEYSIGN_ASKLEVEL))
330            sig_class = dialog_box_param (glob_hinst,
331                                            (LPCSTR)IDD_WINPT_SIGCLASS, dlg,
332                                                  sig_class_dlg_proc, (LPARAM)dlg,
333                                                  _("Choose Signature Class"),
334                                                  IDS_WINPT_SIGCLASS);
335        type = IsDlgButtonChecked (dlg, IDC_KEYSIGN_LOCAL);
336        if (type)
337            type = GPG_EDITKEY_LSIGN;
338        else
339            type = GPG_EDITKEY_SIGN;
340    
341                
342        if (reg_prefs.expert && IsDlgButtonChecked (dlg, IDC_KEYSIGN_NREV)) {
343            type = GPG_EDITKEY_NRSIGN;
344            if (type == GPG_EDITKEY_LSIGN)
345                type = GPG_EDITKEY_NRLSIGN;
346        }
347    
348        if (IsDlgButtonChecked (dlg, IDC_KEYSIGN_EXPSIG)) {
349            DateTime_GetSystemtime (GetDlgItem (dlg, IDC_KEYSIGN_EXPIRES), &st);
350            if (date_is_today (&st)) {
351                msg_box (dlg, _("You cannot select today as the expiration date."),
352                         _("Key Signing"), MB_INFO);
353                return TRUE;
354            }
355            else
356                expires = 1;
357            _snprintf (keymsg, DIM (keymsg)-1, "%04d-%02d-%02d",
358                        st.wYear, st.wMonth, st.wDay);
359        }
360    
361        /* XXX: check for --ask-cert-level and --ask-cert-expire in the gpg.conf
362                if an advanced button is checked and offer to add it to the config
363                file. */    
364        GetDlgItemText_utf8 (dlg, IDC_KEYSIGN_PASSPHRASE, &pwd);
365        
366        assert (key->ctx->subkeys->keyid != NULL); /* should be always true. */
367        ke.setKeyID (key->ctx->subkeys->keyid);
368        ke.setPassphrase (pwd);
369        idx = SendDlgItemMessage (dlg, IDC_KEYSIGN_KEYLIST, CB_GETCURSEL, 0, 0);
370        k = (gpgme_key_t)SendDlgItemMessage (dlg, IDC_KEYSIGN_KEYLIST,
371                                                     CB_GETITEMDATA, (WPARAM)idx, 0);
372        if (k != NULL)
373            ke.setLocalUser (k);
374    
375        err = ke.signKey (type, sig_class, expires? keymsg : "0");
376        sfree_if_alloc (pwd);
377        if (err) {
378            msg_box (dlg, gpgme_strerror (err), _("Key Signing"), MB_ERR);
379            return TRUE;    
380        }
381        else if (ke.getResult () != 0)
382            msg_box (dlg, _("This key is already signed by your key"),
383                        _("Key Signing"), MB_INFO);
384        else {
385            status_box (dlg, _("Key successfully signed."), _("Key Signing"));
386            key->update = 1;        
387        }
388        EndDialog (dlg, TRUE);
389        return TRUE;
390    }
391    
392    
393    /* Dialog box procedure to sign a key. */
394    BOOL CALLBACK
395    keysign_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
396    {
397        static winpt_key_t key;
398        const char *s;
399        
400        switch (msg)  {
401        case WM_INITDIALOG:
402            key = (winpt_key_t) lparam;
403            if (!key)
404                BUG (0);        
405            s = key->ctx->subkeys->keyid;
406            if (do_fill_seckeylist (dlg, s)) {
407                msg_box (dlg, _("No valid secret key found."), _("Key Signing"), MB_ERR);
408                EndDialog (dlg, FALSE);
409                return TRUE;
410            }
411            do_check_protection (dlg);
412            display_keyinfo (dlg, key);
413            SetDlgItemText (dlg, IDC_KEYSIGN_LOCAL, _("Sign local only (non exportable signature)"));
414            SetDlgItemText (dlg, IDC_KEYSIGN_EXPSIG, _("Signature expires on"));
415            SetDlgItemText (dlg, IDC_KEYSIGN_NREV, _("Sign non-revocably"));
416            SetDlgItemText (dlg, IDC_KEYSIGN_ASKLEVEL, _("&Ask for certification level"));
417            SetDlgItemText (dlg, IDC_KEYSIGN_PWDINF, _("Passphrase"));
418            SetDlgItemText (dlg, IDCANCEL, _("&Cancel"));
419            SetDlgItemText (dlg, IDC_KEYSIGN_SHOWIMG, _("&Show photo"));
420            SetDlgItemText (dlg, IDC_KEYSIGN_HIDE, _("&Hide Typing"));
421            SetWindowText (dlg, _("Key Signing"));
422            CheckDlgButton (dlg, IDC_KEYSIGN_LOCAL, BST_CHECKED);
423            CheckDlgButton (dlg, IDC_KEYSIGN_EXPSIG, BST_UNCHECKED);
424            CheckDlgButton (dlg, IDC_KEYSIGN_ASKLEVEL, BST_UNCHECKED);
425            EnableWindow (GetDlgItem (dlg, IDC_KEYSIGN_EXPIRES), FALSE);
426            if (reg_prefs.expert == 0)
427                ShowWindow (GetDlgItem (dlg, IDC_KEYSIGN_NREV), SW_HIDE);
428            if (key->ext && key->ext->attrib.len == 0)
429                ShowWindow (GetDlgItem (dlg, IDC_KEYSIGN_SHOWIMG), SW_HIDE);
430            if (!reg_prefs.gpg.ask_cert_level)
431                EnableWindow (GetDlgItem (dlg, IDC_KEYSIGN_ASKLEVEL), FALSE);
432            if (!reg_prefs.gpg.ask_cert_expire)
433                EnableWindow (GetDlgItem (dlg, IDC_KEYSIGN_EXPSIG), FALSE);
434            CheckDlgButton (dlg, IDC_KEYSIGN_HIDE, BST_CHECKED);
435            SetForegroundWindow (dlg);
436            SetFocus (GetDlgItem (dlg, IDC_KEYSIGN_PASSPHRASE));
437            return FALSE;
438            
439        case WM_COMMAND:
440            if (HIWORD (wparam) == CBN_SELCHANGE) {
441                do_check_protection (dlg);
442                break;
443            }
444            if (HIWORD (wparam) == BN_CLICKED &&
445                LOWORD (wparam) == IDC_KEYSIGN_EXPSIG) {
446                int enable = IsDlgButtonChecked (dlg, IDC_KEYSIGN_EXPSIG);
447    
448                EnableWindow (GetDlgItem (dlg, IDC_KEYSIGN_EXPIRES), enable? TRUE : FALSE);
449            }
450            if (HIWORD (wparam) == BN_CLICKED &&
451                LOWORD (wparam) == IDC_KEYSIGN_HIDE) {
452                HWND hwnd = GetDlgItem (dlg, IDC_KEYSIGN_PASSPHRASE);
453                int hide = IsDlgButtonChecked (dlg, IDC_KEYSIGN_HIDE);
454    
455                SendMessage (hwnd, EM_SETPASSWORDCHAR, hide? '*' : 0, 0);
456                SetFocus (hwnd);
457            }
458    
459            switch (LOWORD (wparam)) {
460            case IDOK:
461                return on_click_ok (dlg, key);
462                
463            case IDCANCEL:
464                EndDialog (dlg, FALSE);
465                return TRUE;
466    
467            case IDC_KEYSIGN_SHOWIMG:
468                DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_SHOWPHOTO,
469                                dlg, showphoto_dlg_proc, (LPARAM)key);
470                return TRUE;
471            }
472            break;
473        }
474        
475        return FALSE;
476    }

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26