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

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26