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

Annotation of /trunk/Src/wptKeysignDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 36 - (hide annotations)
Thu Oct 27 15:25:13 2005 UTC (19 years, 4 months ago) by werner
File size: 11267 byte(s)
First set of changes to use autotools for building.
1 werner 36 /* wptKeysignDlg.cpp - Key signing dialog
2     * Copyright (C) 2001-2005 Timo Schulz
3     *
4     * This file is part of WinPT.
5     *
6     * 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
8     * the Free Software Foundation; either version 2 of the License, or
9     * (at your option) any later version.
10     *
11     * WinPT is distributed in the hope that it will be useful,
12     * but WITHOUT ANY WARRANTY; without even the implied warranty of
13     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14     * GNU General Public License for more details.
15     *
16     * You should have received a copy of the GNU General Public License
17     * along with WinPT; if not, write to the Free Software Foundation,
18     * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19     */
20 twoaday 23
21 werner 36 #ifdef HAVE_CONFIG_H
22     #include <config.h>
23     #endif
24    
25     #include <windows.h>
26     #include <windows.h>
27     #include <commctrl.h>
28    
29     #include "../resource.h"
30     #include "wptGPG.h"
31     #include "wptNLS.h"
32     #include "wptW32API.h"
33     #include "wptVersion.h"
34     #include "wptTypes.h"
35     #include "wptErrors.h"
36     #include "wptCommonCtl.h"
37     #include "wptContext.h"
38     #include "wptDlgs.h"
39     #include "wptUTF8.h"
40     #include "wptRegistry.h"
41     #include "wptKeyList.h"
42     #include "wptKeyEdit.h"
43    
44     static int sig_class_choice = 0;
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     static 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     winpt_get_seckey (key->subkeys->keyid, &k);
74     _snprintf (buf, DIM (buf)-1-16, "%d-bit %s key, ID %s",
75     key->subkeys->length,
76     get_key_pubalgo (key->subkeys->pubkey_algo),
77     key->subkeys->keyid+8);
78     if (k.ext->gloflags.divert_to_card)
79     strcat (buf, " (Card)");
80     return buf;
81     }
82    
83    
84     /* Fill the secret key combo-box with all entries from the cache.
85     @dlg is the handle to the combo-box. @keyid show which key to skip.
86     Return value: 0 on success. */
87     static int
88     do_fill_seckeylist (HWND dlg, const char *keyid)
89     {
90     gpg_keycache_t sec;
91     gpgme_key_t pk;
92     const char * s;
93     char * uid, * p;
94     int i = 0, n=0;
95    
96     sec = keycache_get_ctx (0);
97     if (!sec)
98     BUG (0);
99     gpg_keycache_rewind (sec);
100     while (!gpg_keycache_next_key (sec, 1, &pk)) {
101     if (!pk)
102     continue;
103     s = pk->subkeys->keyid;
104     if (!strcmp (s, keyid))
105     continue;
106     /* skip all ElGamal sign+encrypt keys */
107     if (pk->subkeys->pubkey_algo == GPGME_PK_ELG)
108     continue;
109     /* make sure the public key is okay not: revoked, expired or disabled. */
110     if (pk->expired ||pk->revoked || pk->disabled)
111     continue;
112     s = pk->uids->name;
113     if (!s)
114     continue;
115     uid = utf8_to_wincp (s, strlen (s));
116     p = new char[strlen (uid) + 64];
117     if (!p)
118     BUG (NULL);
119     _snprintf (p, strlen (uid) + 63, "%s (%s)", uid, get_keyinfo (pk));
120     SendDlgItemMessage (dlg, IDC_KEYSIGN_KEYLIST, CB_ADDSTRING, i, (LPARAM)(char *)p);
121     SendDlgItemMessage (dlg, IDC_KEYSIGN_KEYLIST, CB_SETITEMDATA, i++, (LPARAM)(DWORD)pk);
122     free_if_alloc (p);
123     free (uid);
124     n++;
125     }
126     SendDlgItemMessage (dlg, IDC_KEYSIGN_KEYLIST, CB_SETCURSEL, 0, 0);
127     if (!n)
128     return -1;
129     return 0;
130     }
131    
132    
133     /* Check if the selected key is protected and en- or disable the
134     passphrase control. */
135     static void
136     do_check_protection (HWND dlg)
137     {
138     int idx, protec;
139     gpgme_key_t key;
140     struct winpt_key_s k;
141    
142     idx = SendDlgItemMessage( dlg, IDC_KEYSIGN_KEYLIST, CB_GETCURSEL, 0, 0 );
143     key = (gpgme_key_t)SendDlgItemMessage( dlg, IDC_KEYSIGN_KEYLIST, CB_GETITEMDATA, (WPARAM)idx, 0 );
144     if (key) {
145     winpt_get_seckey (key->subkeys->keyid, &k);
146     protec = k.is_protected;
147     if (!protec)
148     protec = k.ext->gloflags.divert_to_card;
149     EnableWindow (GetDlgItem (dlg, IDC_KEYSIGN_PASSPHRASE), protec? TRUE : FALSE);
150     }
151     }
152    
153    
154     /* Dialog box procedure to choose the signature class. */
155     BOOL CALLBACK
156     sig_class_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
157     {
158     switch (msg) {
159     case WM_INITDIALOG:
160     SetWindowText (dlg, _("Choose Signature Class"));
161     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\"."));
162     SetDlgItemText (dlg, IDC_SIGCLASS_CLASS0, _("(0) I will not answer (default)"));
163     SetDlgItemText (dlg, IDC_SIGCLASS_CLASS1, _("(1) I have not checked at all."));
164     SetDlgItemText (dlg, IDC_SIGCLASS_CLASS2, _("(2) I have done causal checking."));
165     SetDlgItemText (dlg, IDC_SIGCLASS_CLASS3, _("(3) I have done very careful checkings."));
166     CheckDlgButton (dlg, IDC_SIGCLASS_CLASS0, BST_CHECKED);
167     SetForegroundWindow (dlg);
168     center_window (dlg, NULL);
169     return TRUE;
170    
171     case WM_COMMAND:
172     switch( LOWORD( wparam ) ) {
173     case IDOK:
174     if (IsDlgButtonChecked (dlg, IDC_SIGCLASS_CLASS0))
175     sig_class_choice = 0;
176     else if (IsDlgButtonChecked (dlg, IDC_SIGCLASS_CLASS1))
177     sig_class_choice = 1;
178     else if (IsDlgButtonChecked (dlg, IDC_SIGCLASS_CLASS2))
179     sig_class_choice = 2;
180     else if (IsDlgButtonChecked (dlg, IDC_SIGCLASS_CLASS3))
181     sig_class_choice = 3;
182     else
183     sig_class_choice = 0;
184     EndDialog (dlg, TRUE);
185     return TRUE;
186     }
187     break;
188     }
189    
190     return FALSE;
191     }
192    
193    
194     /* Return the humand friendly expiration date of @key. */
195     static const char*
196     get_expire_date (gpgme_key_t key)
197     {
198     static char tmp[64];
199    
200     u32 u = key->subkeys->expires;
201     if (!u)
202     return _("never");
203     return get_key_expire_date (u);
204     }
205    
206     /* Display photo of key @key in a separate window. */
207     static void
208     show_photo (winpt_key_t key)
209     {
210     /* XXX: fill it with life. */
211     }
212    
213    
214     /* Dialog box procedure to sign a key. */
215     BOOL CALLBACK
216     keysign_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
217     {
218     static winpt_key_t key;
219     GpgKeyEdit *ke;
220     gpgme_error_t err;
221     gpgme_key_t k;
222     SYSTEMTIME st;
223     HWND h;
224     char keymsg[2048], pwd[256];
225     char *uid = NULL;
226     const char *keyid, *s;
227     int type, expires=0, idx;
228    
229     switch ( msg ) {
230     case WM_INITDIALOG:
231     if (lparam == NULL)
232     dlg_fatal_error (dlg, "Could not get dialog param.");
233     #ifndef LANG_DE
234     SetWindowText (dlg, _("Key Signing"));
235     #endif
236     key = (winpt_key_t) lparam;
237     s = key->ctx->uids->uid;
238     uid = utf8_to_wincp (s, strlen (s));
239     _snprintf (keymsg, sizeof keymsg -1,
240     _("pub %d/%s created: %s expires: %s\n\n"
241     "Primary key fingerprint: %s\n\n"
242     "\t%s\n\n"
243     "\nAre you really sure that you want to sign this key with YOUR key?\n"),
244     key->ctx->subkeys->length,
245     key->ctx->subkeys->keyid+8,
246     get_key_created (key->ctx->subkeys->timestamp),
247     get_expire_date (key->ctx),
248     get_printable_fpr (key->ctx->subkeys->fpr),
249     uid);
250     free (uid);
251     s = key->ctx->subkeys->keyid;
252     if (do_fill_seckeylist (dlg, s)) {
253     msg_box (dlg, _("No valid secret key found."), _("Key Signing"), MB_ERR);
254     EndDialog (dlg, FALSE);
255     }
256     SetDlgItemText (dlg, IDC_KEYSIGN_INFOS, keymsg);
257     #ifndef LANG_DE
258     SetDlgItemText (dlg, IDC_KEYSIGN_LOCAL, _("Sign local only (non exportable signature)"));
259     SetDlgItemText (dlg, IDC_KEYSIGN_EXPSIG, _("Signature expires on"));
260     SetDlgItemText (dlg, IDC_KEYSIGN_NREV, _("Sign non-revocably"));
261     SetDlgItemText (dlg, IDC_KEYSIGN_ASKLEVEL, _("&Ask for certification level"));
262     SetDlgItemText (dlg, IDC_KEYSIGN_PWDINF, _("Passphrase"));
263     #endif
264     CheckDlgButton (dlg, IDC_KEYSIGN_LOCAL, BST_CHECKED);
265     CheckDlgButton (dlg, IDC_KEYSIGN_EXPSIG, BST_UNCHECKED);
266     CheckDlgButton (dlg, IDC_KEYSIGN_ASKLEVEL, BST_UNCHECKED);
267     EnableWindow (GetDlgItem (dlg, IDC_KEYSIGN_EXPIRES), FALSE);
268     if (reg_prefs.expert == 0)
269     ShowWindow (GetDlgItem (dlg, IDC_KEYSIGN_NREV), SW_HIDE);
270     if (key->ext && key->ext->attrib.len > 0)
271     EnableWindow (GetDlgItem (dlg, IDC_KEYSIGN_SHOWIMG), TRUE);
272     SetForegroundWindow (dlg);
273     h = GetDlgItem (dlg, IDC_KEYSIGN_PASSPHRASE);
274     SetFocus (h);
275     return FALSE;
276    
277     case WM_DESTROY:
278     sig_class_choice = 0;
279     break;
280    
281     case WM_SYSCOMMAND:
282     if( LOWORD( wparam ) == SC_CLOSE ) {
283     SetDlgItemText( dlg, IDC_KEYSIGN_PASSPHRASE, "" );
284     EndDialog( dlg, TRUE );
285     }
286     return FALSE;
287    
288     case WM_COMMAND:
289     if (HIWORD( wparam ) == CBN_SELCHANGE) {
290     do_check_protection (dlg);
291     break;
292     }
293     if (HIWORD (wparam) == BN_CLICKED && LOWORD (wparam) == IDC_KEYSIGN_EXPSIG) {
294     int enable = IsDlgButtonChecked (dlg, IDC_KEYSIGN_EXPSIG);
295     EnableWindow (GetDlgItem (dlg, IDC_KEYSIGN_EXPIRES), enable? TRUE : FALSE);
296     }
297    
298     switch( LOWORD( wparam ) ) {
299     case IDOK:
300     if (IsDlgButtonChecked (dlg, IDC_KEYSIGN_ASKLEVEL))
301     dialog_box_param (glob_hinst, (LPCSTR)IDD_WINPT_SIGCLASS, dlg,
302     sig_class_dlg_proc, (LPARAM)NULL,
303     _("Choose Signature Class"), IDS_WINPT_SIGCLASS);
304     type = IsDlgButtonChecked (dlg, IDC_KEYSIGN_LOCAL);
305     if (type)
306     type = GPG_EDITKEY_LSIGN;
307     else
308     type = GPG_EDITKEY_SIGN;
309    
310     if (reg_prefs.expert && IsDlgButtonChecked (dlg, IDC_KEYSIGN_NREV)) {
311     type = GPG_EDITKEY_NRSIGN;
312     if (type == GPG_EDITKEY_LSIGN)
313     type = GPG_EDITKEY_NRLSIGN;
314     }
315     if (IsDlgButtonChecked (dlg, IDC_KEYSIGN_EXPSIG)) {
316     expires = 1;
317     DateTime_GetSystemtime (GetDlgItem (dlg, IDC_KEYSIGN_EXPIRES), &st);
318     sprintf (keymsg, "%04d-%02d-%02d", st.wYear, st.wMonth, st.wDay);
319     }
320    
321     /* XXX: check for --ask-cert-level and --ask-cert-expire in the gpg.conf
322     if an advanced button is checked and offer to add it to the config
323     file. */
324    
325     GetDlgItemText( dlg, IDC_KEYSIGN_PASSPHRASE, pwd, DIM (pwd)-1);
326     keyid = key->ctx->subkeys->keyid;
327     if( !keyid ) {
328     msg_box( dlg, _("Could not get Key ID from key."), _("Key Signing"), MB_ERR );
329     return TRUE;
330     }
331     ke = new GpgKeyEdit (keyid);
332     if (!ke)
333     BUG (NULL);
334     ke->setPassphrase (pwd);
335     idx = SendDlgItemMessage (dlg, IDC_KEYSIGN_KEYLIST, CB_GETCURSEL, 0, 0);
336     k = (gpgme_key_t)SendDlgItemMessage (dlg, IDC_KEYSIGN_KEYLIST,
337     CB_GETITEMDATA, (WPARAM)idx, 0);
338     if (k)
339     ke->setLocalUser (k);
340    
341     err = ke->signKey (type, sig_class_choice, expires? keymsg : "0");
342     memset (&pwd, 0, sizeof pwd);
343     if (err) {
344     delete ke;
345     msg_box (dlg, gpgme_strerror (err), _("Key Signing"), MB_ERR);
346     return TRUE;
347     }
348     if (ke->getResult () != 0)
349     msg_box (dlg, _("This key is already signed by your key"), _("Key Signing"), MB_INFO);
350     else {
351     status_box (dlg, _("Key successfully signed."), PGM_NAME);
352     key->update = 1;
353     }
354     delete ke;
355     EndDialog (dlg, TRUE);
356     return TRUE;
357    
358     case IDCANCEL:
359     EndDialog (dlg, FALSE);
360     return TRUE;
361    
362     case IDC_KEYSIGN_SHOWIMG:
363     show_photo (key);
364     return TRUE;
365     }
366     break;
367     }
368    
369     return FALSE;
370     }
371    

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26