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

Annotation of /trunk/Src/wptKeyPropsDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 392 - (hide annotations)
Sun Jan 15 18:05:11 2012 UTC (13 years, 1 month ago) by twoaday
File size: 10139 byte(s)
2012-01-15  Timo Schulz  <twoaday@gmx.net>
               
     * wptKeyEditDlgs.cpp (keyedit_addsubkey_dlg_proc):
     Invert logic to disable the calendar control for the
     key expiration date.
     * wptKeyPropsDialog.cpp (on_init_dialog): Localized
     missing string.


1 twoaday 256 /* wptKeyPropsDlg.cpp - WinPT key property dialog
2 twoaday 328 * Copyright (C) 2000-2003, 2005-2006, 2008 Timo Schulz
3 werner 36 *
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 werner 42 #ifdef HAVE_CONFIG_H
17     #include <config.h>
18     #endif
19    
20 werner 36 #include <windows.h>
21 twoaday 217 #include <assert.h>
22 werner 36
23 werner 47 #include "resource.h"
24 werner 36 #include "wptErrors.h"
25     #include "wptGPG.h"
26     #include "wptCommonCtl.h"
27     #include "wptContext.h" /* for passphrase_s */
28     #include "wptNLS.h"
29     #include "wptDlgs.h"
30     #include "wptTypes.h"
31     #include "wptKeylist.h"
32     #include "wptW32API.h"
33     #include "wptVersion.h"
34     #include "wptKeyEdit.h"
35 twoaday 366 #include "StringBuffer.h"
36 werner 36
37    
38 twoaday 328 /* Context to store associated data of the dialog. */
39     struct prop_info_s {
40     winpt_key_t key;
41     char photo_file[MAX_PATH+128+1];
42     bool has_photo;
43     };
44    
45    
46 werner 36 /* Check that the key is not expired or revoked. */
47     static int
48     do_check_key (gpgme_key_t key)
49     {
50 twoaday 366 int invalid = key->expired;
51 twoaday 217 if (!invalid)
52     invalid = key->revoked;
53     return invalid;
54 werner 36 }
55    
56    
57     /* Convert a trust integer into a string representation. */
58     static const char*
59 twoaday 150 ownertrust_to_string (int val, bool is_keypair)
60 werner 36 {
61 twoaday 41 const char *inf;
62 werner 36 int id = val;
63 twoaday 41
64 werner 36 switch (id) {
65     case 1: inf = _("Don't know"); break;
66     case 2: inf = _("I do NOT trust"); break;
67     case 3: inf = _("I trust marginally"); break;
68     case 4: inf = _("I trust fully"); break;
69     case 5:
70 twoaday 150 case 6:
71     if (is_keypair)
72     inf = _("I trust ultimately (implicit)");
73     else
74     inf = _("I trust ultimately"); break;
75 werner 36 default:inf = _("Unknown"); break;
76     }
77    
78     return inf;
79     }
80    
81 twoaday 366 /* Generate a temporary name for the photo ID */
82 twoaday 328 int
83     get_photo_tmpname (gpgme_key_t key, char *buf, size_t buflen)
84 werner 36 {
85 twoaday 328 const char *fmt = "winpt_photo_%p.tmp";
86 twoaday 174 char name[64];
87 werner 36
88 twoaday 328 if (buflen < (MAX_PATH+strlen(fmt)+8+1))
89     return WPTERR_GENERAL;
90     _snprintf (name, DIM (name)-1, fmt, key);
91     get_temp_name (buf, buflen-1, name);
92     return 0;
93 werner 36 }
94    
95    
96 twoaday 129
97    
98 werner 36 /* Load the photo from the key @key */
99 twoaday 226 int
100 twoaday 328 key_load_photo (winpt_key_t key,
101     char *photo_file, size_t photo_file_size,
102     gpgme_validity_t *r_valid)
103 werner 36 {
104 twoaday 366 const BYTE *img = key->ext->attrib.d;
105     DWORD imglen = key->ext->attrib.len;
106 twoaday 328 if (img && !key->ext->attrib.validity)
107     get_uat_validity (key->ctx->subkeys->keyid,
108     &key->ext->attrib.validity);
109 twoaday 226 if (r_valid)
110 twoaday 328 *r_valid = key->ext->attrib.validity;
111 werner 36
112 twoaday 328 if (!img || imglen < 1)
113 werner 36 return -1;
114 twoaday 328
115     get_photo_tmpname (key->ctx, photo_file, photo_file_size);
116 twoaday 366 FILE *fp = fopen (photo_file, "wb");
117     if (fp == NULL)
118     return -1;
119 twoaday 328
120 twoaday 366 const int HEADER_OFF = 16;
121     fwrite (img + HEADER_OFF, 1, imglen - HEADER_OFF, fp);
122     fclose (fp);
123     return 0;
124 werner 36 }
125    
126    
127     /* Return string representation of the key validity. @key. */
128     static const char*
129     get_validity (gpgme_key_t key)
130     {
131 twoaday 211 if (key->expired)
132 werner 36 return _("Expired");
133 twoaday 211 if (key->revoked)
134 werner 36 return _("Revoked");
135 twoaday 211 if (key->disabled)
136 twoaday 41 return _("Disabled");
137 twoaday 225 if (key->invalid)
138     return _("Invalid");
139 werner 36 return get_key_trust2 (NULL, key->uids->validity, 0, 0);
140     }
141    
142    
143     /* Return true if the key has designated revokers. */
144     static bool
145     check_for_desig_rev (gpgme_key_t key)
146     {
147 twoaday 225 winpt_key_s k;
148 twoaday 217
149 twoaday 225 memset (&k, 0, sizeof (k));
150     if (!winpt_get_pubkey (key->subkeys->keyid, &k))
151     return k.ext->gloflags.has_desig_rev? true : false;
152 werner 36 return false;
153     }
154    
155    
156     /* Print information (name) of the smart card. */
157     static const char*
158     get_card_type (winpt_key_t k)
159     {
160     static char buf[64];
161    
162 twoaday 181 if (!k->ext || !k->ext->card_type)
163 werner 36 return "";
164 twoaday 328 _snprintf (buf, DIM (buf)-1, _("Card-Type: %s\r\n"),
165     k->ext->card_type);
166 werner 36 return buf;
167     }
168    
169    
170 twoaday 211 /* Return 1 if at least one user-ID is valid. */
171     static int
172 twoaday 217 key_is_valid (gpgme_key_t key)
173 twoaday 211 {
174     gpgme_user_id_t u;
175    
176 twoaday 217 for (u=key->uids; u; u=u->next) {
177 twoaday 211 if (u->validity >= GPGME_VALIDITY_MARGINAL)
178     return 1;
179     }
180     return 0;
181     }
182    
183    
184 twoaday 273 /* Return extended algorithm information. */
185     const char*
186     props_get_key_algo (gpgme_key_t key, int idx)
187     {
188     /* PGP calls the old RSAv3 keys 'RSA Legacy' and because this
189     is a good method to differ between OpenPGP v4 cert-only keys
190     and v3 RSA keys, we use the same notation. */
191     if (key->subkeys != NULL && strlen (key->subkeys->fpr) == 32)
192     return "RSA Legacy";
193     return get_key_algo (key, idx);
194     }
195 twoaday 211
196 twoaday 273
197 twoaday 328 /* Display the key information for key @k. */
198 werner 36 static void
199 twoaday 211 display_key_info (HWND dlg, winpt_key_t k)
200 werner 36 {
201 twoaday 181 gpgme_key_t key;
202 twoaday 211 struct winpt_key_s sk;
203 werner 36 char info[512];
204     const char *inf;
205 twoaday 181 DWORD created, expires;
206 werner 36
207 twoaday 211 gpg_keycache_update_attr (k->ext, KC_ATTR_PREFSYM, 0);
208 twoaday 217 memset (&sk, 0, sizeof (sk));
209 twoaday 181 if (k->key_pair && !winpt_get_seckey (k->keyid, &sk))
210     k->is_protected = sk.is_protected;
211 twoaday 211 key = k->ext->key;
212 twoaday 181 created = key->subkeys->timestamp;
213     expires = key->subkeys->expires;
214 werner 36 _snprintf (info, DIM (info)-1,
215     _("Type: %s\r\n"
216 twoaday 211 "Key ID: 0x%s\r\n"
217 werner 36 "Algorithm: %s\r\n"
218 twoaday 128 "Size: %s bits\r\n"
219 werner 36 "Created: %s\r\n"
220     "Expires: %s\r\n"
221     "Validity: %s\r\n"
222     "%s\r\n"),
223     get_key_type (key),
224     k->keyid,
225 twoaday 273 props_get_key_algo (key, 0),
226 werner 36 get_key_size (key, 0),
227     get_key_created (created),
228     get_key_expire_date (expires),
229     get_validity (key),
230 twoaday 181 get_card_type (&sk));
231 werner 36
232     SetDlgItemText (dlg, IDC_KEYPROPS_INFO, info);
233     SetDlgItemText (dlg, IDC_KEYPROPS_FPR, get_key_fpr (key));
234 twoaday 150 inf = ownertrust_to_string (key->owner_trust, k->key_pair);
235 werner 36 SetDlgItemText (dlg, IDC_KEYPROPS_OT, inf);
236     }
237    
238    
239 twoaday 217
240     static void
241     on_init_dialog (HWND dlg, WPARAM wparam, LPARAM lparam)
242     {
243     winpt_key_t k = (winpt_key_t)lparam;
244 twoaday 310 assert (k != NULL);
245 twoaday 366
246     StringBuffer s = _("Key Properties");
247     if (k->ext->key->revoked)
248 twoaday 392 s = s + " - " + _("REVOKED KEY");
249 twoaday 366
250     SetWindowText (dlg, s.getBuffer());
251 twoaday 217 SetDlgItemText (dlg, IDC_KEYPROPS_OT_CHANGE, _("&Change"));
252     SetDlgItemText (dlg, IDC_KEYPROPS_REVOKERS, _("&Revokers"));
253     SetDlgItemText (dlg, IDC_KEYPROPS_CHANGE_PWD, _("Change &Password"));
254 twoaday 328 SetDlgItemText (dlg, IDC_KEYPROPS_OTINF, _("Ownertrust:"));
255     SetDlgItemText (dlg, IDC_KEYPROPS_FPRTXT, _("Fingerprint:"));
256 twoaday 217
257     display_key_info (dlg, k);
258 twoaday 328
259 twoaday 217 if (k->key_pair)
260     EnableWindow (GetDlgItem (dlg, IDC_KEYPROPS_CHANGE_PWD), TRUE);
261     if (check_for_desig_rev (k->ctx))
262     EnableWindow (GetDlgItem (dlg, IDC_KEYPROPS_REVOKERS), TRUE);
263     if (do_check_key (k->ctx))
264     EnableWindow (GetDlgItem (dlg, IDC_KEYPROPS_OT_CHANGE), FALSE);
265     center_window (dlg, NULL);
266     SetForegroundWindow (dlg);
267     }
268    
269    
270 werner 36 /* Dialog box procedure to show the key properties. */
271     BOOL CALLBACK
272     keyprops_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
273     {
274 twoaday 217 struct prop_info_s *prop = NULL;
275 werner 36 const char *inf;
276 twoaday 328 gpgme_validity_t valid;
277 werner 36 int rc;
278    
279 twoaday 328 if (msg != WM_INITDIALOG) {
280     prop = (struct prop_info_s*)GetWindowLong (dlg, GWL_USERDATA);
281     if (prop == NULL)
282     return FALSE;
283     }
284 twoaday 217
285 werner 36 switch (msg) {
286     case WM_INITDIALOG:
287 twoaday 234 assert (lparam != 0);
288 twoaday 217 prop = new struct prop_info_s;
289     prop->key = (winpt_key_t)lparam;
290 twoaday 328 prop->has_photo = 0;
291 twoaday 217 SetWindowLong (dlg, GWL_USERDATA, (LONG)prop);
292     on_init_dialog (dlg, wparam, lparam);
293 twoaday 328 if (!key_load_photo (prop->key,
294     prop->photo_file, DIM (prop->photo_file)-1,
295     &valid)) {
296     prop->key->has_photo = 1;
297     prop->has_photo = true;
298     if (valid < GPGME_VALIDITY_MARGINAL)
299     SetDlgItemText (dlg, IDC_KEYPROPS_IMGINF,
300     _("Photo-ID not validated."));
301     }
302 werner 36 return TRUE;
303    
304     case WM_DESTROY:
305 twoaday 328 if (prop->has_photo)
306     DeleteFile(prop->photo_file);
307 twoaday 226 delete prop;prop = NULL;
308 twoaday 217 SetWindowLong (dlg, GWL_USERDATA, 0);
309 werner 36 break;
310    
311     case WM_PAINT:
312 twoaday 226 /* Display the photo in the frame of the dialog @dlg.
313     The coordinates are fixed to (0,0). */
314 twoaday 328 if (prop->has_photo) {
315 twoaday 226 POINT p;
316     p.x = p.y = 0;
317 twoaday 328 /* In case of errors we disable the flag to
318     avoid an infinite loop. */
319     if (jpg_show (GetDlgItem (dlg, IDC_KEYPROPS_IMG),
320     &p, prop->photo_file))
321     prop->has_photo = false;
322 twoaday 226 }
323 werner 36 break;
324 twoaday 226
325 werner 36 case WM_COMMAND:
326     switch (LOWORD (wparam)) {
327     case IDOK:
328     EndDialog (dlg, TRUE);
329     return TRUE;
330 twoaday 170
331     case IDCANCEL:
332     EndDialog (dlg, FALSE);
333     return TRUE;
334 werner 36
335     case IDC_KEYPROPS_OT_CHANGE:
336 twoaday 217 if (!prop->key->key_pair && !key_is_valid (prop->key->ctx)) {
337 twoaday 150 rc = msg_box (dlg, _("This is a non-valid key.\n"
338 werner 36 "Modifying the ownertrust has no effect on such keys.\n\n"
339     "Do you really want to continue?"),
340 twoaday 150 _("WinPT Warning"), MB_ICONWARNING|MB_YESNO);
341 werner 36 if (rc == IDNO)
342     return TRUE;
343     }
344 twoaday 165 rc = dialog_box_param (glob_hinst,
345     (LPCSTR)IDD_WINPT_KEYEDIT_OWNERTRUST,
346     dlg, keyedit_ownertrust_dlg_proc,
347 twoaday 217 (LPARAM)prop->key, _("Change Ownertrust"),
348 twoaday 165 IDS_WINPT_KEYEDIT_OWNERTRUST);
349     if (rc == FALSE) /* Cancel */
350     return TRUE;
351 werner 36
352 twoaday 217 inf = ownertrust_to_string (prop->key->callback.new_val,
353     prop->key->key_pair);
354 werner 36 SetDlgItemText (dlg, IDC_KEYPROPS_OT, inf);
355     msg_box (dlg, _("Ownertrust successfully changed."),
356     _("GnuPG Status"), MB_OK);
357 twoaday 217 prop->key->update = 1;
358 werner 36 return TRUE;
359    
360     case IDC_KEYPROPS_CHANGE_PWD:
361 twoaday 217 keyedit_change_passwd (prop->key, dlg);
362 werner 36 return TRUE;
363    
364     case IDC_KEYPROPS_REVOKERS:
365 twoaday 217 prop->key->update = dialog_box_param (glob_hinst,
366 twoaday 328 (LPCTSTR)IDD_WINPT_KEYREVOKERS, dlg,
367     key_revokers_dlg_proc,
368     (LPARAM)prop->key,
369     _("Key Revokers"),
370     IDS_WINPT_KEY_REVOKERS);
371 twoaday 217 UpdateWindow (dlg);
372 werner 36 break;
373     }
374     }
375    
376     return FALSE;
377     }

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26