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

Annotation of /trunk/Src/wptKeysignDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 33 - (hide annotations)
Tue Oct 25 07:46:20 2005 UTC (19 years, 4 months ago) by twoaday
File size: 11562 byte(s)
More bug fixes and cleanups.
See ChangeLog for details.

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26