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

Contents of /trunk/Src/wptKeyPropsDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2 - (show annotations)
Mon Jan 31 11:02:21 2005 UTC (20 years, 1 month ago) by twoaday
File size: 9260 byte(s)
WinPT initial checkin.


1 /* wptKeyPropsDlg.cpp - WinPT key properties dialog
2 * Copyright (C) 2000, 2001, 2002, 2003 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
21 #include <windows.h>
22
23 #include "../resource.h"
24 #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
36 int keyedit_ownertrust_get_val (void);
37
38
39 static void
40 do_change_ownertrust( winpt_key_t k, const char *s )
41 {
42 char ot[64], *p;
43
44 if( strstr( s, "ultimate" ) ) {
45 listview_get_item_text( k->callback.ctl, k->callback.idx, 5, ot, sizeof ot-1 );
46 if( (p = strrchr( ot, ']' )) )
47 strcpy( ot + (p - ot) + 2, "Ultimate" );
48 else
49 strcpy( ot, "Ultimate" );
50 listview_add_sub_item( k->callback.ctl, k->callback.idx, 5, ot );
51 /* fixme: If we switch back from Ultimate to a lower level */
52 }
53 } /* do_change_ownertrust */
54
55
56 static int
57 do_check_key( gpgme_key_t key )
58 {
59 int okay = 0;
60 okay = gpgme_key_get_ulong_attr( key, GPGME_ATTR_KEY_EXPIRED, NULL, 0 );
61 if( !okay )
62 okay = gpgme_key_get_ulong_attr( key, GPGME_ATTR_KEY_REVOKED, NULL, 0 );
63 return okay;
64 } /* do_check_key */
65
66
67 static const char*
68 ownertrust_to_string (gpgme_editkey_t ctx, int val)
69 {
70 const char * inf;
71 int id;
72
73 if (ctx)
74 id = keyedit_ownertrust_get_val ();
75 else if (!ctx && val)
76 id = val;
77 switch (id) {
78 case 1: inf = _("Don't know"); break;
79 case 2: inf = _("I do NOT trust"); break;
80 case 3: inf = _("I trust marginally"); break;
81 case 4: inf = _("I trust fully"); break;
82 case 5:
83 case 6: inf = _("I trust ultimately"); break;
84 default:inf = _("Unknown"); break;
85 }
86
87 return inf;
88 } /* ownertrust_to_string */
89
90
91 static int
92 keyprops_show_photo (HWND dlg, gpgme_key_t key)
93 {
94 RECT r;
95 POINT p;
96 HWND h;
97 const BYTE *img;
98 DWORD imglen = 0;
99 int pos=0;
100
101 img = (const byte *)gpgme_key_get_string_attr( key, GPGME_ATTR_PHOTO,
102 (void **)&imglen, 0 );
103 if (!img || !imglen)
104 return -1;
105 FILE * f = fopen ("temp.jpg", "wb");
106 if (f) {
107 for (pos = 0; img[pos] != 0x10; pos++)
108 ;
109 pos += 16;
110 fwrite (img + pos, 1, imglen - pos, f);
111 fwrite (img, 1, imglen, f);
112 fclose (f);
113 }
114
115 h = GetDlgItem (dlg, IDC_KEYPROPS_IMG);
116 GetWindowRect (h, &r);
117 p.x = r.left + 5;
118 p.y = r.top - 2;
119 memset (&p, 0, sizeof p);
120 PTD_jpg_show (h, &p, "temp.jpg");
121 unlink ("temp.jpg");
122
123 return 0;
124 }
125
126
127 BOOL CALLBACK
128 keyprops_dlg_proc( HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam )
129 {
130 static winpt_key_t k;
131 static gpgme_key_t key, sk;
132 static int photo_done = 0;
133 gpgme_editkey_t ek;
134 gpgme_ctx_t ctx;
135 gpgme_error_t ec;
136 refresh_cache_s rcs = {0};
137 const char *inf;
138 const char * sym_prefs;
139 char info[2048], dummy_symprefs[] = {0x01, 0x00};
140 u32 created, expires;
141 int valid, ot, cancel = 0;
142 int rc;
143
144 switch (msg) {
145 case WM_INITDIALOG:
146 if (!lparam)
147 dlg_fatal_error( dlg, "Could not get dialog param!" );
148 k = (winpt_key_t)lparam;
149 #ifndef LANG_DE
150 SetWindowText( dlg, _("Key Properties") );
151 SetDlgItemText (dlg, IDC_KEYPROPS_OT_CHANGE, _("&Change"));
152 SetDlgItemText (dlg, IDC_KEYPROPS_REVOKERS, _("&Revokers"));
153 SetDlgItemText (dlg, IDC_KEYPROPS_CHANGE_PWD, _("Change &Passwd"));
154 #endif
155
156 photo_done = 0;
157 get_seckey (k->keyid, &sk);
158 if (sk)
159 k->is_protected = gpgme_key_get_ulong_attr( sk, GPGME_ATTR_IS_PROTECTED, NULL, 0 );
160 if (get_pubkey (k->keyid, &key))
161 BUG (0);
162 created = gpgme_key_get_ulong_attr( key, GPGME_ATTR_CREATED, NULL, 0 );
163 expires = gpgme_key_get_ulong_attr( key, GPGME_ATTR_EXPIRES, NULL, 0 );
164 sym_prefs = gpgme_key_get_string_attr( key, GPGME_ATTR_KEY_SYMPREFS, NULL, 0 );
165 if (!sym_prefs)
166 sym_prefs = dummy_symprefs;
167 valid = gpgme_key_get_ulong_attr (key, GPGME_ATTR_VALIDITY, NULL, 0);
168 _snprintf (info, sizeof info -1,
169 "Type: %s\r\n"
170 "Key ID: %s\r\n"
171 "Algorithm: %s\r\n"
172 "Size: %s\r\n"
173 "Created: %s\r\n"
174 "Expires: %s\r\n"
175 "Validity: %s\r\n"
176 "Cipher: %s\r\n",
177 get_key_type( key ),
178 k->keyid,
179 get_key_algo( key, 0 ),
180 get_key_size( key, 0 ),
181 get_key_created( created ),
182 get_key_expire_date( expires ),
183 gpgme_key_expand_attr( GPGME_ATTR_VALIDITY, valid ),
184 gpgme_key_expand_attr( GPGME_ATTR_KEY_SYMPREFS, *sym_prefs ) );
185 SetDlgItemText( dlg, IDC_KEYPROPS_INFO, info );
186 SetDlgItemText( dlg, IDC_KEYPROPS_FPR, get_key_fpr( key ) );
187 ot = gpgme_key_get_ulong_attr( key, GPGME_ATTR_OTRUST, NULL, 0 );
188 inf = ownertrust_to_string( NULL, ot );
189 SetDlgItemText( dlg, IDC_KEYPROPS_OT, inf );
190 if( k->key_pair )
191 EnableWindow( GetDlgItem( dlg, IDC_KEYPROPS_CHANGE_PWD ), TRUE );
192 if( gpgme_key_count_items( key, GPGME_ATTR_REVKEY_FPR ) )
193 EnableWindow( GetDlgItem( dlg, IDC_KEYPROPS_REVOKERS ), TRUE );
194 center_window (dlg);
195 SetForegroundWindow (dlg);
196 return TRUE;
197
198 case WM_PAINT:
199 if (photo_done == 0) {
200 photo_done = 1;
201 keyprops_show_photo (dlg, key);
202 }
203 break;
204
205 case WM_SYSCOMMAND:
206 if( LOWORD( wparam ) == SC_CLOSE )
207 EndDialog( dlg, TRUE );
208 return FALSE;
209
210 case WM_COMMAND:
211 switch( LOWORD( wparam ) ) {
212 case IDOK:
213 EndDialog( dlg, TRUE );
214 return TRUE;
215
216 case IDC_KEYPROPS_OT_CHANGE:
217 if( do_check_key( key ) ) {
218 msg_box( dlg, _("The status of this key is 'revoked' or 'expired'.\n"
219 "You cannot change the ownertrust of such keys."),
220 _("WinPT Warning"), MB_ERR );
221 return FALSE;
222 }
223 if( !k->key_pair && gpgme_key_get_ulong_attr( key, GPGME_ATTR_VALIDITY, NULL, 0 ) < 3 ) {
224 rc = msg_box( dlg, _("This is a non-valid key.\n"
225 "Modifying the ownertrust has no effect on such keys.\n"
226 "Do you really want to continue?"),
227 _("WinPT Warning"), MB_ICONWARNING|MB_YESNO );
228 if (rc == IDNO)
229 return FALSE;
230 }
231 GetDlgItemText( dlg, IDC_KEYPROPS_OT, info, sizeof info -1 );
232 gpgme_editkey_new( &ek );
233 gpgme_editkey_is_secret( ek, k->key_pair );
234 dialog_box_param( glob_hinst, (LPCSTR)IDD_WINPT_KEYEDIT_OWNERTRUST,
235 dlg, (DLGPROC)keyedit_ownertrust_dlg_proc,
236 (LPARAM)ek, _("Change Ownertrust"),
237 IDS_WINPT_KEYEDIT_OWNERTRUST );
238 if (!gpgme_editkey_is_valid (ek))
239 return FALSE;
240 ec = gpgme_new (&ctx);
241 if (ec)
242 BUG (0);
243 gpgme_set_edit_ctx( ctx, ek, GPGME_EDITKEY_TRUST );
244 ec = gpgme_op_editkey( ctx, k->keyid );
245 gpgme_editkey_release( ek );
246 gpgme_release( ctx );
247 if( ec ) {
248 msg_box( dlg, gpgme_strerror( ec ), _("Ownertrust"), MB_ERR );
249 return FALSE;
250 }
251 inf = ownertrust_to_string (ek, 0);
252 k->callback.new_val = keyedit_ownertrust_get_val ();
253 do_change_ownertrust (k, inf);
254 SetDlgItemText (dlg, IDC_KEYPROPS_OT, inf);
255 msg_box (dlg, _("Ownertrust successfully changed."), _("GnuPG Status"), MB_OK);
256
257 /* reload only the keylist */
258 rcs.kr_reload = 1; rcs.kr_update = 1;
259 rcs.tr_update = 0;
260 DialogBoxParam( glob_hinst, (LPCSTR)IDD_WINPT_KEYCACHE, dlg,
261 keycache_dlg_proc, (LPARAM)&rcs );
262 get_seckey (k->keyid, &sk);
263 if (get_pubkey (k->keyid, &key))
264 BUG (0);
265 return TRUE;
266
267 case IDC_KEYPROPS_CHANGE_PWD:
268 keyedit_change_passwd (k, dlg);
269 return TRUE;
270
271 case IDC_KEYPROPS_REVOKERS:
272 dialog_box_param( glob_hinst, (LPCTSTR)IDD_WINPT_KEYREVOKERS, dlg,
273 key_revokers_dlg_proc, (LPARAM)key, _("Key Revokers"),
274 IDS_WINPT_KEY_REVOKERS );
275 break;
276 }
277 }
278
279 return FALSE;
280 } /* keyprops_dlg_proc */

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26