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

Annotation of /trunk/Src/wptKeysignDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 24 - (hide annotations)
Sat Oct 8 10:43:08 2005 UTC (19 years, 4 months ago) by twoaday
File size: 11011 byte(s)
Bug fixes to correct some problems introduced by
the MyGPGME to GPGME port.

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     gpgme_keycache_t sec;
86     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     gpgme_keycache_rewind (sec);
95 twoaday 22 while (!gpgme_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     return "never";
198     return get_key_expire_date (u);
199     }
200    
201    
202 twoaday 23 /* Dialog box procedure to sign a key. */
203 twoaday 2 BOOL CALLBACK
204     keysign_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
205     {
206 twoaday 22 static winpt_key_t key;
207 twoaday 23 GpgKeyEdit *ke;
208 twoaday 2 gpgme_error_t err;
209 twoaday 23 gpgme_key_t k;
210 twoaday 2 SYSTEMTIME st;
211     HWND h;
212     char keymsg[4096], pwd[256], *uid = NULL;
213     const char *keyid, *s;
214     u32 created;
215 twoaday 23 int type, expires=0, idx;
216 twoaday 2
217     switch ( msg ) {
218     case WM_INITDIALOG:
219 twoaday 22 if (lparam == NULL)
220 twoaday 23 dlg_fatal_error (dlg, "Could not get dialog param.");
221 twoaday 2 #ifndef LANG_DE
222 twoaday 23 SetWindowText (dlg, _("Key Signing"));
223 twoaday 2 #endif
224 twoaday 22 key = (winpt_key_t) lparam;
225 twoaday 23 created = key->ctx->subkeys->timestamp;
226     s = key->ctx->uids->uid;
227     if (s)
228 twoaday 2 uid = utf8_to_wincp (s, strlen (s));
229 twoaday 23 _snprintf (keymsg, sizeof keymsg -1,
230 twoaday 24 _("pub %d/%s created: %s expires: %s\n\n"
231 twoaday 2 "Primary key fingerprint: %s\n\n"
232     "\t%s\n\n"
233     "\nAre you really sure that you want to sign this key with YOUR key?\n"),
234 twoaday 23 key->ctx->subkeys->length,
235     key->ctx->subkeys->keyid+8,
236     get_key_created (key->ctx->subkeys->timestamp),
237 twoaday 22 get_expire_date (key->ctx),
238 twoaday 23 get_printable_fpr (key->ctx->subkeys->fpr),
239     uid);
240 twoaday 2 free (uid);
241 twoaday 23 s = key->ctx->subkeys->keyid;
242 twoaday 2 if (do_fill_seckeylist (dlg, s)) {
243     msg_box( dlg, _("No valid secret key found."), _("Key Signing"), MB_ERR );
244     EndDialog( dlg, FALSE );
245     }
246 twoaday 23 SetDlgItemText (dlg, IDC_KEYSIGN_INFOS, keymsg);
247 twoaday 2 #ifndef LANG_DE
248 twoaday 22 SetDlgItemText (dlg, IDC_KEYSIGN_LOCAL, _("Sign local only (non exportable signature)"));
249 twoaday 2 SetDlgItemText (dlg, IDC_KEYSIGN_EXPSIG, _("Signature expires on"));
250     SetDlgItemText (dlg, IDC_KEYSIGN_NREV, _("Sign non-revocably"));
251     #endif
252 twoaday 22 CheckDlgButton (dlg, IDC_KEYSIGN_LOCAL, BST_CHECKED);
253 twoaday 2 CheckDlgButton (dlg, IDC_KEYSIGN_EXPSIG, BST_UNCHECKED);
254 twoaday 22 CheckDlgButton (dlg, IDC_KEYSIGN_ASKLEVEL, BST_UNCHECKED);
255 twoaday 2 EnableWindow (GetDlgItem (dlg, IDC_KEYSIGN_EXPIRES), FALSE);
256     if (reg_prefs.expert == 0)
257     ShowWindow (GetDlgItem (dlg, IDC_KEYSIGN_NREV), SW_HIDE);
258     SetForegroundWindow( dlg );
259     h = GetDlgItem( dlg, IDC_KEYSIGN_PASSPHRASE );
260     SetFocus( h );
261     return FALSE;
262 twoaday 22
263     case WM_DESTROY:
264     sig_class_choice = 0;
265     break;
266    
267 twoaday 2 case WM_SYSCOMMAND:
268     if( LOWORD( wparam ) == SC_CLOSE ) {
269     SetDlgItemText( dlg, IDC_KEYSIGN_PASSPHRASE, "" );
270     EndDialog( dlg, TRUE );
271     }
272     return FALSE;
273    
274     case WM_COMMAND:
275     if( HIWORD( wparam ) == CBN_SELCHANGE ) {
276     do_check_protection( dlg );
277     break;
278     }
279     if (HIWORD (wparam) == BN_CLICKED && LOWORD (wparam) == IDC_KEYSIGN_EXPSIG) {
280 twoaday 22 int enable = IsDlgButtonChecked (dlg, IDC_KEYSIGN_EXPSIG);
281 twoaday 2 EnableWindow (GetDlgItem (dlg, IDC_KEYSIGN_EXPIRES), enable? TRUE : FALSE);
282     }
283    
284     switch( LOWORD( wparam ) ) {
285     case IDOK:
286 twoaday 22 if (IsDlgButtonChecked (dlg, IDC_KEYSIGN_ASKLEVEL))
287     dialog_box_param (glob_hinst, (LPCSTR)IDD_WINPT_SIGCLASS, dlg,
288     sig_class_dlg_proc, (LPARAM)NULL,
289     _("Choose Signature Class"), IDS_WINPT_SIGCLASS);
290 twoaday 2 type = IsDlgButtonChecked (dlg, IDC_KEYSIGN_LOCAL);
291     if (type)
292 twoaday 23 type = GPG_EDITKEY_LSIGN;
293 twoaday 2 else
294 twoaday 23 type = GPG_EDITKEY_SIGN;
295 twoaday 2
296 twoaday 22 if (reg_prefs.expert && IsDlgButtonChecked (dlg, IDC_KEYSIGN_NREV)) {
297 twoaday 23 type = GPG_EDITKEY_NRSIGN;
298     if (type == GPG_EDITKEY_LSIGN)
299     type = GPG_EDITKEY_NRLSIGN;
300 twoaday 2 }
301     if (IsDlgButtonChecked (dlg, IDC_KEYSIGN_EXPSIG)) {
302     expires = 1;
303     DateTime_GetSystemtime (GetDlgItem (dlg, IDC_KEYSIGN_EXPIRES), &st);
304     sprintf (keymsg, "%04d-%02d-%02d", st.wYear, st.wMonth, st.wDay);
305     }
306    
307 twoaday 22 GetDlgItemText( dlg, IDC_KEYSIGN_PASSPHRASE, pwd, DIM (pwd)-1);
308 twoaday 23 keyid = key->ctx->subkeys->keyid;
309 twoaday 2 if( !keyid ) {
310     msg_box( dlg, _("Could not get Key ID from key."), _("Key Signing"), MB_ERR );
311 twoaday 23 return TRUE;
312 twoaday 2 }
313 twoaday 23 ke = new GpgKeyEdit (keyid);
314     if (!ke)
315     BUG (NULL);
316     ke->setPassphrase (pwd);
317     idx = SendDlgItemMessage (dlg, IDC_KEYSIGN_KEYLIST, CB_GETCURSEL, 0, 0);
318     k = (gpgme_key_t)SendDlgItemMessage (dlg, IDC_KEYSIGN_KEYLIST,
319     CB_GETITEMDATA, (WPARAM)idx, 0);
320     if (k)
321     ke->setLocalUser (k);
322 twoaday 2
323 twoaday 23 err = ke->signKey (type, sig_class_choice, expires? keymsg : NULL);
324 twoaday 24 memset (&pwd, 0, sizeof pwd);
325 twoaday 2 if (err) {
326 twoaday 24 delete ke;
327 twoaday 2 msg_box (dlg, gpgme_strerror (err), _("Key Signing"), MB_ERR);
328 twoaday 23 return TRUE;
329 twoaday 2 }
330 twoaday 24 if (ke->getResultValue () != 0)
331     msg_box (dlg, _("This key is already signed by your key"), _("Key Signing"), MB_INFO);
332     else {
333     status_box (dlg, _("Key successfully signed."), PGM_NAME);
334     key->update = 1;
335     }
336     delete ke;
337 twoaday 23 EndDialog (dlg, TRUE);
338 twoaday 2 return TRUE;
339    
340     case IDCANCEL:
341 twoaday 23 EndDialog (dlg, FALSE);
342     return TRUE;
343 twoaday 2 }
344     break;
345     }
346    
347     return FALSE;
348 twoaday 23 }
349    

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26