1 |
/* wptKeyManager.cpp - Handy functions for the Key Manager dialog |
/* wptKeyManager.cpp - Handy functions for the Key Manager dialog |
2 |
* Copyright (C) 2001-2005 Timo Schulz |
* Copyright (C) 2001-2005 Timo Schulz |
3 |
|
* Copyright (C) 2005 g10 Code GmbH |
4 |
* |
* |
5 |
* This file is part of WinPT. |
* This file is part of WinPT. |
6 |
* |
* |
21 |
#include <windows.h> |
#include <windows.h> |
22 |
#include <commctrl.h> |
#include <commctrl.h> |
23 |
#include <stdio.h> |
#include <stdio.h> |
|
#include <io.h> |
|
24 |
|
|
25 |
|
#include "gpgme.h" |
26 |
#include "../resource.h" |
#include "../resource.h" |
27 |
#include "wptTypes.h" |
#include "wptTypes.h" |
28 |
#include "wptW32API.h" |
#include "wptW32API.h" |
30 |
#include "wptCommonCtl.h" |
#include "wptCommonCtl.h" |
31 |
#include "wptNLS.h" |
#include "wptNLS.h" |
32 |
#include "wptErrors.h" |
#include "wptErrors.h" |
|
#include "wptGPG.h" |
|
33 |
#include "wptContext.h" |
#include "wptContext.h" |
34 |
|
#include "wptGPG.h" |
35 |
#include "wptKeylist.h" |
#include "wptKeylist.h" |
36 |
#include "wptFileManager.h" |
#include "wptFileManager.h" |
37 |
#include "wptDlgs.h" |
#include "wptDlgs.h" |
39 |
#include "wptKeyManager.h" |
#include "wptKeyManager.h" |
40 |
#include "wptKeylist.h" |
#include "wptKeylist.h" |
41 |
#include "wptHTTP.h" |
#include "wptHTTP.h" |
42 |
|
#include "wptKeyEdit.h" |
43 |
|
#include "wptImport.h" |
44 |
|
|
45 |
|
|
46 |
|
/* Return a user friendly key representation in @buf of |
47 |
|
the key given by @keyid. */ |
48 |
static void |
static void |
49 |
km_get_clip_info (const char *uid, char *buf, size_t buflen) |
key_get_clip_info (const char *keyid, char *buf, size_t buflen) |
50 |
{ |
{ |
51 |
gpgme_key_t pk; |
gpgme_key_t pk; |
|
unsigned long a, algo; |
|
52 |
|
|
53 |
if (get_pubkey (uid, &pk)) |
if (get_pubkey (keyid, &pk)) |
54 |
BUG (0); |
BUG (NULL); |
|
|
|
|
a = gpgme_key_get_ulong_attr (pk, GPGME_ATTR_CREATED, NULL, 0); |
|
|
algo = gpgme_key_get_ulong_attr (pk, GPGME_ATTR_ALGO, NULL, 0); |
|
55 |
_snprintf (buf, buflen-1, |
_snprintf (buf, buflen-1, |
56 |
"pub %04d%s/%s %s %s\r\n" |
"pub %04d%s/%s %s %s\r\n" |
57 |
" Primary key fingerprint: %s\r\n", |
" Primary key fingerprint: %s\r\n", |
58 |
gpgme_key_get_ulong_attr (pk, GPGME_ATTR_LEN, NULL, 0), |
pk->subkeys->length, |
59 |
gpgme_key_expand_attr (GPGME_ATTR_ALGO_SHORT, algo), |
get_key_pubalgo (pk->subkeys->pubkey_algo), |
60 |
gpgme_key_get_string_attr (pk, GPGME_ATTR_KEYID, NULL, 0) + 8, |
pk->subkeys->keyid+8, |
61 |
gpgme_key_expand_attr (GPGME_ATTR_CREATED, a ), |
get_key_created (pk->subkeys->timestamp), |
62 |
gpgme_key_get_string_attr (pk, GPGME_ATTR_USERID, NULL, 0), |
pk->uids->uid, |
63 |
get_key_fpr (pk)); |
get_key_fpr (pk)); |
64 |
} |
} |
65 |
|
|
66 |
|
|
67 |
|
#if 0 |
68 |
|
/* Quoted the user-id given by @uid. If @uid is already |
69 |
|
quoted @uid is returned without any modifications. |
70 |
|
Return value: quoted @uid. */ |
71 |
char* |
char* |
72 |
km_quote_uid (const char * uid) |
km_quote_uid (const char *uid) |
73 |
{ |
{ |
74 |
char * q = new char[strlen (uid) + 4]; |
char *q; |
75 |
|
|
76 |
|
if (*uid == '"' && uid[strlen (uid)-1] == '"') |
77 |
|
return m_strdup (uid); |
78 |
|
q = new char[strlen (uid) + 4]; |
79 |
if (!q) |
if (!q) |
80 |
BUG (NULL); |
BUG (NULL); |
81 |
_snprintf (q, strlen (uid) + 3, "\"%s\"", uid); |
_snprintf (q, strlen (uid) + 3, "\"%s\"", uid); |
82 |
return q; |
return q; |
83 |
} /* km_quote_uid */ |
} |
84 |
|
#endif |
85 |
|
|
86 |
|
|
87 |
|
/* Check if list view @lv contains a secret key at position @pos. |
88 |
|
If utrust is valid, set it to 1 if the key is valid -1 otherwise. |
89 |
|
Return value: 1 normal key, 2 smart card key. */ |
90 |
int |
int |
91 |
km_check_for_seckey (listview_ctrl_t lv, int pos, int * utrust) |
km_check_for_seckey (listview_ctrl_t lv, int pos, int *utrust) |
92 |
{ |
{ |
93 |
char t[32], t2[64]; |
char t[32], t2[64]; |
94 |
int type = 0; |
int type = 0; |
95 |
|
|
96 |
listview_get_item_text (lv, pos, 5, t, sizeof (t) - 1); |
if (utrust) |
97 |
listview_get_item_text (lv, pos, 2, t2, sizeof (t2) - 1); |
*utrust = 0; |
98 |
|
listview_get_item_text (lv, pos, 5, t, DIM (t)-1); |
99 |
|
listview_get_item_text (lv, pos, 2, t2, DIM (t2)-1); |
100 |
if (!strcmp (t2, "pub/sec")) |
if (!strcmp (t2, "pub/sec")) |
101 |
type = 1; |
type = 1; |
102 |
else if (!strcmp (t2, "pub/crd")) |
else if (!strcmp (t2, "pub/crd")) |
103 |
type = 2; |
type = 2; |
104 |
if (stristr (t, "ultimate") && utrust) |
if ((strstr (t, "Expired") || strstr (t, "Revoked")) && utrust) |
105 |
|
*utrust = -1; |
106 |
|
else if (stristr (t, "Ultimate") && utrust) |
107 |
*utrust = 1; |
*utrust = 1; |
108 |
return type; |
return type; |
109 |
} /* km_check_for_seckey */ |
} |
110 |
|
|
111 |
|
|
112 |
|
/* Check if the key at position @pos is protected with a passwd. */ |
113 |
int |
int |
114 |
km_check_if_protected( listview_ctrl_t lv, int pos ) |
km_check_if_protected (listview_ctrl_t lv, int pos) |
115 |
{ |
{ |
116 |
gpgme_key_t key; |
gpgme_key_t key; |
117 |
char keyid[32]; |
winpt_key_s k; |
118 |
|
|
119 |
listview_get_item_text( lv, pos, 1, keyid, sizeof keyid-1 ); |
key = (gpgme_key_t)listview_get_item2 (lv, pos); |
120 |
if( get_pubkey( keyid, &key ) ) |
if (!key) |
121 |
BUG( NULL ); |
return 1; /* assume yes */ |
122 |
return gpgme_key_get_ulong_attr( key, GPGME_ATTR_IS_PROTECTED, NULL, 0 ); |
winpt_get_pubkey (key->subkeys->keyid, &k); |
123 |
} /* km_check_if_protected */ |
return k.is_protected; |
124 |
|
} |
125 |
|
|
126 |
|
|
127 |
int |
int |
128 |
km_check_key_status (listview_ctrl_t lv, int pos) |
km_check_key_status (listview_ctrl_t lv, int pos) |
129 |
{ |
{ |
130 |
char t[128]; |
int flags = km_get_key_status (lv, pos); |
|
int i = 1; |
|
131 |
|
|
132 |
listview_get_item_text( lv, pos, 5, t, sizeof t - 1 ); |
if (flags & KM_FLAG_EXPIRED) { |
133 |
if( t[0] == '[' && t[1] == ']' ) |
msg_box (lv->ctrl, _("This key has expired!\n" |
134 |
return 1; |
"Key check failed."), _("Key Manager"), MB_ERR); |
135 |
for( i = 0; t[i] != ']'; i++ ) { |
return -1; |
136 |
if( t[i] == 'E' ) |
} |
137 |
msg_box(lv->ctrl, _("This key has expired!\n" |
else if (flags & KM_FLAG_REVOKED) { |
138 |
"Key check failed."), _("Key Manager"), MB_ERR ); |
msg_box (lv->ctrl, _("This key has been revoked by its owner!\n" |
139 |
else if( t[i] == 'R' ) |
"Key check failed."), _("Key Manager"), MB_ERR); |
140 |
msg_box(lv->ctrl, _("This key has been revoked by its owner!\n" |
return -1; |
|
"Key check failed."), _("Key Manager"), MB_ERR ); |
|
141 |
} |
} |
142 |
|
|
143 |
return 0; |
return 0; |
145 |
|
|
146 |
|
|
147 |
int |
int |
148 |
km_get_key_status( listview_ctrl_t lv, int pos ) |
km_get_key_status (listview_ctrl_t lv, int pos) |
149 |
{ |
{ |
150 |
char t[128]; |
gpgme_key_t key; |
151 |
int i, flags = 0; |
int flags = 0; |
152 |
|
|
153 |
if( pos == -1 ) |
if (pos == -1) |
154 |
return 0; |
return 0; |
155 |
listview_get_item_text( lv, pos, 5, t, sizeof t-1 ); |
key = (gpgme_key_t)listview_get_item2 (lv, pos); |
156 |
for( i = 0; t[i] != ']'; i++ ) { |
if (key == NULL) |
157 |
if( t[i] == 'E' ) |
return 0; |
158 |
flags |= KM_FLAG_EXPIRED; |
|
159 |
if( t[i] == 'R' ) |
if (key->expired) |
160 |
flags |= KM_FLAG_REVOKED; |
flags |= KM_FLAG_EXPIRED; |
161 |
if( t[i] == 'D' ) |
if (key->revoked) |
162 |
flags |= KM_FLAG_DISABLED; |
flags |= KM_FLAG_REVOKED; |
163 |
} |
if (key->disabled) |
164 |
|
flags |= KM_FLAG_DISABLED; |
165 |
return flags; |
return flags; |
166 |
} /* km_get_key_status */ |
} /* km_get_key_status */ |
167 |
|
|
168 |
|
|
169 |
|
/* Interface to enable or disable a key (@enable = 1 then enable). |
170 |
|
The key is retrieved from a list control @lv at the pos @pos. */ |
171 |
int |
int |
172 |
km_enable_disable_key (listview_ctrl_t lv, HWND dlg, int pos, int enable) |
km_enable_disable_key (listview_ctrl_t lv, HWND dlg, int pos, int enable) |
173 |
{ |
{ |
174 |
gpgme_ctx_t ctx; |
GpgKeyEdit *ke; |
|
gpgme_editkey_t ek; |
|
175 |
gpgme_error_t err; |
gpgme_error_t err; |
|
int edit_id; |
|
176 |
char keyid[32]; |
char keyid[32]; |
177 |
|
|
178 |
listview_get_item_text( lv, pos, 1, keyid, DIM (keyid)-1 ); |
listview_get_item_text (lv, pos, 1, keyid, DIM (keyid)-1); |
179 |
err = gpgme_editkey_new( &ek ); |
|
180 |
if( err ) |
ke = new GpgKeyEdit (keyid); |
181 |
BUG( NULL ); |
if (!ke) |
182 |
if( enable ) { |
BUG (NULL); |
183 |
gpgme_editkey_enable_set (ek); |
|
184 |
edit_id = GPGME_EDITKEY_ENABLE; |
err = enable? ke->enable () : ke->disable (); |
185 |
|
if (!err) { |
186 |
|
show_msg (dlg, 1500, _("Key status changed.")); |
187 |
|
keycache_set_reload (1); /* XXX: set update flag */ |
188 |
} |
} |
189 |
else { |
else |
190 |
gpgme_editkey_disable_set (ek); |
msg_box (dlg, gpgme_strerror (err), _("Key Manager"), MB_ERR); |
191 |
edit_id = GPGME_EDITKEY_DISABLE; |
delete ke; |
192 |
|
return err? WPTERR_GENERAL : 0; |
193 |
|
} |
194 |
|
|
195 |
|
|
196 |
|
|
197 |
|
/* Create a string that contain all keyids from |
198 |
|
the key list @rset separated by a space. */ |
199 |
|
char* |
200 |
|
gpg_keylist_to_pattern (gpgme_key_t *rset, int n) |
201 |
|
{ |
202 |
|
char *p; |
203 |
|
int i; |
204 |
|
|
205 |
|
if (!n) |
206 |
|
return NULL; |
207 |
|
p = (char *)calloc (1, n*(16+1)+n+2); |
208 |
|
if (!p) |
209 |
|
BUG (NULL); |
210 |
|
for (i=0; i < n; i++) { |
211 |
|
strcat (p, rset[i]->subkeys->keyid); |
212 |
|
strcat (p, " "); |
213 |
} |
} |
214 |
|
return p; |
215 |
|
} |
216 |
|
|
217 |
|
|
218 |
|
/* Export the keys given in @rset to the clipboard. |
219 |
|
Return value: 0 on success. */ |
220 |
|
static gpgme_error_t |
221 |
|
gpg_clip_export (gpgme_key_t *rset, int n) |
222 |
|
{ |
223 |
|
gpgme_error_t err = 0; |
224 |
|
gpgme_ctx_t ctx = NULL; |
225 |
|
gpgme_data_t keydata = NULL; |
226 |
|
char *patt=NULL; |
227 |
|
|
228 |
err = gpgme_new (&ctx); |
err = gpgme_new (&ctx); |
229 |
if( err ) |
if (err) |
230 |
BUG( NULL ); |
return err; |
231 |
gpgme_set_edit_ctx (ctx, ek, edit_id); |
gpgme_set_armor (ctx, 1); |
232 |
err = gpgme_op_editkey (ctx, keyid); |
err = gpgme_data_new (&keydata); |
233 |
if( !err ) { |
if (err) |
234 |
show_msg( dlg, 1500, _("Key status changed.") ); |
goto leave; |
235 |
keycache_set_reload( 1 ); |
|
236 |
|
patt = gpg_keylist_to_pattern (rset, n); |
237 |
|
if (!patt) { |
238 |
|
err = gpg_error (GPG_ERR_ENOMEM); |
239 |
|
goto leave; |
240 |
} |
} |
|
else |
|
|
msg_box( dlg, gpgme_strerror( err ), _("Key Manager"), MB_ERR ); |
|
241 |
|
|
242 |
|
err = gpgme_op_export (ctx, patt, 0, keydata); |
243 |
|
if (err) |
244 |
|
goto leave; |
245 |
|
|
246 |
|
gpg_data_release_and_set_clipboard (keydata, 1); |
247 |
|
|
248 |
|
leave: |
249 |
|
if (patt) |
250 |
|
free (patt); |
251 |
gpgme_release (ctx); |
gpgme_release (ctx); |
252 |
gpgme_editkey_release (ek); |
return err; |
253 |
return err? WPTERR_GENERAL : 0; |
} |
|
} /* km_enable_disable_key */ |
|
254 |
|
|
255 |
|
|
256 |
|
/* Export the selected keys in @lv to the clipboard. */ |
257 |
int |
int |
258 |
km_clip_export (HWND dlg, listview_ctrl_t lv) |
km_clip_export (HWND dlg, listview_ctrl_t lv) |
259 |
{ |
{ |
260 |
gpgme_error_t err; |
gpgme_error_t err; |
261 |
gpgme_recipients_t rset; |
gpgme_key_t *rset; |
262 |
int rc, id = 0, n = 0; |
char buf[256]; |
263 |
|
int n=0; |
264 |
|
int rc=0; |
265 |
|
|
266 |
rset = keylist_enum_recipients (lv, KEYLIST_LIST); |
rset = keylist_enum_recipients (lv, KEYLIST_LIST, &n); |
|
n = gpgme_recipients_count (rset); |
|
267 |
if (!n) { |
if (!n) { |
268 |
msg_box (dlg, _("No key was selected for export."), _("Key Manager"), MB_ERR); |
msg_box (dlg, _("No key was selected for export."), _("Key Manager"), MB_ERR); |
269 |
rc = WPTERR_GENERAL; |
rc = WPTERR_GENERAL; |
270 |
goto leave; |
goto leave; |
271 |
} |
} |
272 |
|
|
273 |
err = gpgme_op_clip_export (rset); |
err = gpg_clip_export (rset, n); |
274 |
if (err) { |
if (err) { |
275 |
msg_box( dlg, gpgme_strerror (err), _("Key Manager"), MB_ERR); |
msg_box( dlg, gpgme_strerror (err), _("Key Manager"), MB_ERR); |
276 |
rc = WPTERR_GENERAL; |
rc = WPTERR_GENERAL; |
277 |
goto leave; |
goto leave; |
278 |
} |
} |
279 |
if (n == 1) { |
if (n == 1) { |
280 |
const char *s; |
key_get_clip_info (rset[0]->subkeys->keyid, buf, sizeof (buf)-1); |
|
char buf[256]; |
|
|
s = gpgme_recipients_get_name (rset, 0); |
|
|
km_get_clip_info (s, buf, 255); |
|
281 |
set_clip_text2 (NULL, buf, strlen (buf), 0); |
set_clip_text2 (NULL, buf, strlen (buf), 0); |
282 |
} |
} |
283 |
|
|
284 |
show_msg (dlg, 1500, _("GnuPG Status: Finished")); |
show_msg (dlg, 1500, _("GnuPG Status: Finished")); |
285 |
|
|
286 |
leave: |
leave: |
287 |
gpgme_recipients_release (rset); |
free (rset); |
288 |
return rc; |
return rc; |
289 |
} /* km_clip_export */ |
} /* km_clip_export */ |
290 |
|
|
291 |
|
|
292 |
|
/* Export the selected secret key from @lv into @fname. |
293 |
|
It is only allowed to export a single secret key. */ |
294 |
int |
int |
295 |
km_privkey_export( HWND dlg, listview_ctrl_t lv, const char *fname ) |
km_privkey_export (HWND dlg, listview_ctrl_t lv, const char *fname) |
296 |
{ |
{ |
297 |
gpgme_recipients_t rset; |
gpgme_key_t *rset; |
|
gpgme_data_t keydata; |
|
298 |
gpgme_error_t err; |
gpgme_error_t err; |
299 |
gpgme_ctx_t ctx; |
int n = 0; |
|
size_t n = 0; |
|
300 |
|
|
301 |
rset = keylist_enum_recipients( lv, KEYLIST_LIST ); |
rset = keylist_enum_recipients (lv, KEYLIST_LIST, &n); |
302 |
n = gpgme_recipients_count( rset ); |
if (!n) { |
303 |
if( !n ) { |
msg_box (dlg, _("No key was selected for export."), |
304 |
msg_box( dlg, _("No key was selected for export."), _("Key Manager"), MB_ERR ); |
_("Key Manager"), MB_ERR); |
305 |
return WPTERR_GENERAL; |
return WPTERR_GENERAL; |
306 |
} |
} |
307 |
if( n > 1 ) { |
if (n > 1) { |
308 |
msg_box( dlg, _("Only one secret key can be exported."), _("Key Manager"), MB_ERR ); |
msg_box (dlg, _("Only one secret key can be exported."), |
309 |
|
_("Key Manager"), MB_ERR); |
310 |
|
free (rset); |
311 |
return 0; /* we checked this before, so we just quit */ |
return 0; /* we checked this before, so we just quit */ |
312 |
} |
} |
|
err = gpgme_data_new( &keydata ); |
|
|
if( err ) |
|
|
BUG( dlg ); |
|
|
err = gpgme_new( &ctx ); |
|
|
if( err ) |
|
|
BUG( dlg ); |
|
|
gpgme_control( ctx, GPGME_CTRL_ARMOR, 1 ); |
|
|
gpgme_control( ctx, GPGME_CTRL_WITH_SECRET_KEY, 1 ); |
|
|
|
|
|
err = gpgme_op_export( ctx, rset, keydata ); |
|
|
if( err ) { |
|
|
msg_box( dlg, gpgme_strerror( err ), _("Key Manager"), MB_ERR ); |
|
|
goto leave; |
|
|
} |
|
313 |
|
|
314 |
log_box( _("Key Manager"), MB_OK, |
err = gpg_export_seckey (rset[0]->subkeys->keyid, fname); |
315 |
_("Secret key successfully saved in '%s'."), fname ); |
if (err) |
316 |
|
msg_box (dlg, gpgme_strerror (err), _("Key Manager"), MB_ERR); |
317 |
leave: |
else |
318 |
err = gpgme_data_release_and_set_file( keydata, fname ); |
log_box (_("Key Manager"), MB_OK, |
319 |
if( err ) |
_("Secret key successfully saved in '%s'."), fname); |
|
log_box( _("Key Manager"), MB_OK, |
|
|
_("Could not save data to '%s'."), fname ); |
|
|
gpgme_release( ctx ); |
|
320 |
|
|
321 |
return (int)err; |
free (rset); |
322 |
} /* km_privkey_export */ |
return err? WPTERR_GENERAL : 0; |
323 |
|
} |
324 |
|
|
325 |
|
|
326 |
int |
int |
327 |
km_file_export (HWND dlg, listview_ctrl_t lv, const char * fname) |
km_file_export (HWND dlg, listview_ctrl_t lv, const char * fname) |
328 |
{ |
{ |
329 |
gpgme_recipients_t rset; |
gpgme_key_t *rset; |
330 |
gpgme_data_t keydata; |
gpgme_data_t keydata; |
331 |
gpgme_error_t err; |
gpgme_error_t err; |
332 |
gpgme_ctx_t ctx; |
gpgme_ctx_t ctx; |
333 |
|
char *patt; |
334 |
|
int n; |
335 |
|
|
336 |
rset = keylist_enum_recipients( lv, KEYLIST_LIST ); |
rset = keylist_enum_recipients (lv, KEYLIST_LIST, &n); |
337 |
if( !gpgme_recipients_count( rset ) ) { |
if (!n) { |
338 |
msg_box( dlg, _("No key was selected for export."), _("Key Manager"), MB_ERR ); |
msg_box (dlg, _("No key was selected for export."), |
339 |
|
_("Key Manager"), MB_ERR); |
340 |
return WPTERR_GENERAL; |
return WPTERR_GENERAL; |
341 |
} |
} |
342 |
|
|
343 |
err = gpgme_data_new( &keydata ); |
err = gpgme_data_new (&keydata); |
344 |
if( err ) |
if (err) |
345 |
BUG( dlg ); |
BUG (NULL); |
346 |
err = gpgme_new( &ctx ); |
err = gpgme_new (&ctx); |
347 |
if( err ) |
if (err) |
348 |
BUG( dlg ); |
BUG (NULL); |
349 |
gpgme_control( ctx, GPGME_CTRL_ARMOR, 1 ); |
gpgme_set_armor (ctx, 1); |
350 |
gpgme_set_comment (ctx, "Generated by WinPT "PGM_VERSION); |
|
351 |
|
/*gpgme_set_comment (ctx, "Generated by WinPT "PGM_VERSION);*/ |
352 |
|
patt = gpg_keylist_to_pattern (rset, n); |
353 |
|
|
354 |
err = gpgme_op_export( ctx, rset, keydata ); |
err = gpgme_op_export( ctx, patt, 0, keydata); |
355 |
if( err ) { |
if( err ) { |
356 |
msg_box( dlg, gpgme_strerror( err ), _("Key Manager"), MB_ERR ); |
msg_box( dlg, gpgme_strerror( err ), _("Key Manager"), MB_ERR ); |
357 |
goto leave; |
goto leave; |
361 |
_("Key(s) successfully saved in '%s'."), fname ); |
_("Key(s) successfully saved in '%s'."), fname ); |
362 |
|
|
363 |
leave: |
leave: |
364 |
err = gpgme_data_release_and_set_file( keydata, fname ); |
err = gpg_data_release_and_set_file( keydata, fname ); |
365 |
if( err ) |
if (err) |
366 |
log_box( _("Key Manager"), MB_OK, |
log_box (_("Key Manager"), MB_OK, |
367 |
_("Could not save data to '%s'."), fname ); |
_("Could not save data to '%s'."), fname); |
368 |
gpgme_release( ctx ); |
gpgme_release (ctx); |
369 |
|
free (patt); |
370 |
return (int)err; |
return (int)err; |
371 |
} /* km_file_export */ |
} /* km_file_export */ |
372 |
|
|
377 |
gpgme_data_t inp, plain; |
gpgme_data_t inp, plain; |
378 |
gpgme_error_t err; |
gpgme_error_t err; |
379 |
|
|
380 |
err = gpgme_data_new_from_clipboard (&inp); |
err = gpg_data_new_from_clipboard (&inp, 0); |
381 |
if (err) { |
if (err) { |
382 |
msg_box (NULL, gpgme_strerror( err ), _("Key Manager"), MB_ERR); |
msg_box (NULL, gpgme_strerror( err ), _("Key Manager"), MB_ERR); |
383 |
return -1; |
return -1; |
384 |
} |
} |
385 |
gpgme_data_extract_plaintext (inp, &plain); |
gpg_data_extract_plaintext (inp, &plain); |
386 |
|
gpg_data_release_and_set_clipboard (plain, 0); |
387 |
gpgme_data_release (inp); |
gpgme_data_release (inp); |
|
gpgme_data_release_and_set_clipboard (plain); |
|
388 |
|
|
389 |
return 0; |
return 0; |
390 |
} /* extract_dash_escaped_key */ |
} /* extract_dash_escaped_key */ |
391 |
|
|
392 |
|
|
393 |
|
/* Import keys from the clipboard. */ |
394 |
int |
int |
395 |
km_clip_import( HWND dlg ) |
km_clip_import (HWND dlg) |
396 |
{ |
{ |
397 |
gpgme_error_t err; |
gpgme_error_t err; |
398 |
gpgme_pgptype_t pgptype; |
gpg_pgptype_t pgptype; |
399 |
int id; |
int id; |
400 |
int has_data = 0; |
int has_data = 0; |
401 |
|
|
402 |
if( !gpgme_clip_istext_avail( &has_data ) && !has_data ) { |
if (!gpg_clip_istext_avail (&has_data) && !has_data) { |
403 |
msg_box( dlg, winpt_strerror( WPTERR_CLIP_ISEMPTY ), _("Key Manager"), MB_ERR ); |
msg_box( dlg, winpt_strerror (WPTERR_CLIP_ISEMPTY), _("Key Manager"), MB_ERR); |
404 |
return WPTERR_CLIP_ISEMPTY; |
return WPTERR_CLIP_ISEMPTY; |
405 |
} |
} |
406 |
err = gpgme_clip_is_secured( &pgptype, &has_data ); |
err = gpg_clip_is_secured (&pgptype, &has_data); |
407 |
if( err ) |
if (err) |
408 |
msg_box( dlg, gpgme_strerror( err ), _("Key Manager"), MB_ERR ); |
msg_box (dlg, gpgme_strerror (err), _("Key Manager"), MB_ERR); |
409 |
if( !has_data ) { |
if (!has_data) { |
410 |
msg_box( dlg, _("No valid OpenPGP data found."), _("Key Manager"), MB_ERR ); |
msg_box (dlg, _("No valid OpenPGP data found."), _("Key Manager"), MB_ERR); |
411 |
return WPTERR_GENERAL; |
return WPTERR_GENERAL; |
412 |
} |
} |
413 |
if( !(pgptype & GPGME_PGP_PUBKEY) && !(pgptype & GPGME_PGP_SECKEY) ) { |
if (!(pgptype & PGP_PUBKEY) && !(pgptype & PGP_SECKEY)) { |
414 |
msg_box( dlg, _("No valid OpenPGP keys found."), _("Key Manager"), MB_ERR ); |
msg_box (dlg, _("No valid OpenPGP keys found."), _("Key Manager"), MB_ERR); |
415 |
return WPTERR_GENERAL; |
return WPTERR_GENERAL; |
416 |
} |
} |
417 |
if( pgptype & GPGME_PGP_DASH_ESCAPED ) { |
if (pgptype & PGP_DASH_ESCAPED) { |
418 |
id = msg_box( dlg, _("The key you want to import is dash escacped.\n" |
id = msg_box (dlg, _("The key you want to import is dash escacped.\n" |
419 |
"Do you want to extract the key?"), |
"Do you want to extract the key?"), |
420 |
_("Key Manager"), MB_YESNO ); |
_("Key Manager"), MB_YESNO); |
421 |
if( id == IDYES ) |
if (id == IDYES) |
422 |
extract_dash_escaped_key( ); |
extract_dash_escaped_key (); |
423 |
else |
else |
424 |
msg_box( dlg, _("Cannot import dash escaped OpenPGP keys."), _("Key Manager"), MB_INFO ); |
msg_box (dlg, _("Cannot import dash escaped OpenPGP keys."), |
425 |
|
_("Key Manager"), MB_INFO); |
426 |
} |
} |
427 |
|
|
428 |
dialog_box_param( glob_hinst, (LPCSTR)IDD_WINPT_IMPORT, dlg, |
dialog_box_param (glob_hinst, (LPCSTR)IDD_WINPT_IMPORT, dlg, |
429 |
clip_import_dlg_proc, NULL, |
clip_import_dlg_proc, NULL, |
430 |
_("Key Import"), IDS_WINPT_IMPORT ); |
_("Key Import"), IDS_WINPT_IMPORT); |
431 |
|
|
432 |
return 0; |
return 0; |
433 |
} /* km_clip_import */ |
} |
434 |
|
|
435 |
|
|
436 |
|
/* Import a key from the http URL @url. */ |
437 |
int |
int |
438 |
km_http_import (HWND dlg, const char * url) |
km_http_import (HWND dlg, const char *url) |
439 |
{ |
{ |
|
FILE * fp; |
|
|
char * p; |
|
|
char tmpdir[500]; |
|
440 |
http_hd_t hd; |
http_hd_t hd; |
441 |
|
FILE *fp; |
442 |
|
char *p; |
443 |
|
char tmpfile[500]; |
444 |
int statcode; |
int statcode; |
445 |
int rc = 0; |
int rc = 0; |
446 |
|
|
449 |
return WPTERR_GENERAL; |
return WPTERR_GENERAL; |
450 |
} |
} |
451 |
|
|
452 |
GetTempPath (499, tmpdir); |
GetTempPath (sizeof (tmpfile)-128, tmpfile); |
453 |
p = make_filename (tmpdir, "file_http", "tmp"); |
p = make_filename (tmpfile, "winpt_file_http", "tmp"); |
454 |
if (!p) |
if (!p) |
455 |
BUG (0); |
BUG (0); |
456 |
fp = fopen (p, "wb"); |
fp = fopen (p, "wb"); |
457 |
if (fp == NULL) { |
if (!fp) { |
458 |
free_if_alloc (p); |
free_if_alloc (p); |
459 |
log_box (_("Key Import HTTP"), MB_ERR, "%s: %s", p, winpt_strerror (WPTERR_FILE_CREAT)); |
log_box (_("Key Import HTTP"), MB_ERR, "%s: %s", p, winpt_strerror (WPTERR_FILE_CREAT)); |
460 |
return WPTERR_FILE_CREAT; |
return WPTERR_FILE_CREAT; |
461 |
} |
} |
462 |
|
|
463 |
/* parse URL */ |
/* parse URL */ |
464 |
rc = http_send_request2 (url, &hd); |
rc = http_send_request2 (url, &hd); |
465 |
if (!rc) |
if (!rc) |
468 |
rc = http_parse_data (hd, fp); |
rc = http_parse_data (hd, fp); |
469 |
http_hd_free (hd); |
http_hd_free (hd); |
470 |
fclose (fp); |
fclose (fp); |
471 |
if (rc) { |
if (rc) { |
472 |
free_if_alloc (p); |
msg_box (dlg, winpt_strerror (rc), _("Key Import HTTP"), MB_ERR); |
473 |
msg_box (dlg, http_strerror (rc), _("Key Import HTTP"), MB_ERR); |
rc = WPTERR_GENERAL; |
|
return WPTERR_GENERAL; |
|
474 |
} |
} |
475 |
km_file_import (dlg, p); |
km_file_import (dlg, p); |
476 |
|
unlink (p); |
477 |
free_if_alloc (p); |
free_if_alloc (p); |
478 |
return 0; |
return rc; |
479 |
} |
} |
480 |
|
|
481 |
|
|
482 |
|
/* Import a key from the given file @fname. |
483 |
|
On success an import statistics dialog is shown. */ |
484 |
int |
int |
485 |
km_file_import( HWND dlg, const char * fname ) |
km_file_import (HWND dlg, const char *fname) |
486 |
{ |
{ |
487 |
gpgme_data_t keydata = NULL; |
gpgme_data_t keydata = NULL; |
488 |
gpgme_ctx_t ctx; |
gpgme_ctx_t ctx; |
489 |
gpgme_error_t err; |
gpgme_error_t err; |
490 |
fm_state_s fm_stat; |
fm_state_s fm_stat; |
491 |
int import_res[14]; |
gpgme_import_result_t res; |
492 |
|
|
493 |
memset( &fm_stat, 0, sizeof fm_stat ); |
memset (&fm_stat, 0, sizeof (fm_stat)); |
494 |
fm_stat.opaque = m_strdup( fname ); |
fm_stat.opaque = m_strdup (fname); |
495 |
|
|
496 |
dialog_box_param( glob_hinst, (LPCSTR)IDD_WINPT_IMPORT, dlg, |
dialog_box_param (glob_hinst, (LPCSTR)IDD_WINPT_IMPORT, dlg, |
497 |
file_import_dlg_proc, (LPARAM)&fm_stat, |
file_import_dlg_proc, (LPARAM)&fm_stat, |
498 |
_("File Import"), IDS_WINPT_IMPORT ); |
_("File Import"), IDS_WINPT_IMPORT); |
499 |
if( fm_stat.cancel == 1 ) |
if (fm_stat.cancel == 1 ) { |
500 |
|
free_if_alloc (fm_stat.opaque); |
501 |
return WPTERR_GENERAL; |
return WPTERR_GENERAL; |
|
|
|
|
err = gpgme_new( &ctx ); |
|
|
if( err ) |
|
|
BUG( dlg ); |
|
|
gpgme_control( ctx, GPGME_CTRL_FORCETRUST, 1 ); |
|
|
err = gpgme_data_new_from_file (&keydata, fname); |
|
|
if( err ) { |
|
|
msg_box( dlg, _("Could not read key-data from file."), _("Key Manager"), MB_ERR ); |
|
|
goto leave; |
|
502 |
} |
} |
503 |
|
|
504 |
err = gpgme_op_import( ctx, NULL, keydata ); |
err = gpgme_new (&ctx); |
505 |
if( err ) { |
if (err) |
506 |
msg_box( dlg, gpgme_strerror( err ), _("Key Manager"), MB_ERR ); |
BUG (NULL); |
507 |
|
err = gpgme_data_new_from_file (&keydata, fname, 1); |
508 |
|
if (err) { |
509 |
|
msg_box (dlg, _("Could not read key-data from file."), |
510 |
|
_("Key Manager"), MB_ERR); |
511 |
goto leave; |
goto leave; |
512 |
} |
} |
513 |
|
|
514 |
gpgme_get_import_status( ctx, import_res, NULL ); |
err = gpgme_op_import (ctx, keydata); |
515 |
print_import_status( import_res, fm_stat.implist_revcert ); |
if (err) { |
516 |
if( import_res[GPGME_IMPSTAT_NOSELFSIG] > 0 ) { |
msg_box (dlg, gpgme_strerror (err), _("Key Manager"), MB_ERR); |
517 |
msg_box( dlg, _("Key without a self signature was dectected!\n" |
goto leave; |
518 |
|
} |
519 |
|
|
520 |
|
res = gpgme_op_import_result (ctx); |
521 |
|
if (res->new_revocations == 0 && fm_stat.import.revcert == 1) |
522 |
|
res->new_revocations = 1; |
523 |
|
if (res->secret_imported == 0 && fm_stat.import.has_seckey == 1) |
524 |
|
res->secret_imported = 1; |
525 |
|
|
526 |
|
print_import_status (res); |
527 |
|
if (res->no_user_id > 0) { |
528 |
|
msg_box (dlg, _("Key without a self signature was dectected!\n" |
529 |
"(This key is NOT usable for encryption, etc)\n" |
"(This key is NOT usable for encryption, etc)\n" |
530 |
"\n" |
"\n" |
531 |
"Cannot import these key(s)!"), _("Import"), MB_INFO ); |
"Cannot import these key(s)!"), _("Import"), MB_INFO); |
532 |
} |
} |
533 |
|
|
534 |
leave: |
leave: |
535 |
gpgme_data_release( keydata ); |
gpgme_data_release (keydata); |
536 |
gpgme_release( ctx ); |
gpgme_release (ctx); |
537 |
free_if_alloc( fm_stat.opaque ); |
free_if_alloc (fm_stat.opaque); |
538 |
return (int)err; |
return (int)err; |
539 |
} /* km_file_import */ |
} |
540 |
|
|
541 |
|
|
542 |
|
/* Mark the keys in @rset as deleted in the keycache. */ |
543 |
static void |
static void |
544 |
delete_keys_from_cache (gpgme_recipients_t rset) |
delete_keys_from_cache (gpgme_key_t *rset, size_t n) |
545 |
{ |
{ |
546 |
gpgme_keycache_t pub = keycache_get_ctx (1); |
gpg_keycache_t pub = keycache_get_ctx (1); |
547 |
void * ctx =NULL; |
int i=0; |
|
const char * s; |
|
548 |
|
|
549 |
gpgme_recipients_enum_open (rset, &ctx); |
while (n-- > 0) |
550 |
while ((s = gpgme_recipients_enum_read (rset, &ctx))) |
gpg_keycache_delete_key (pub, rset[i++]->subkeys->keyid); |
551 |
gpgme_keycache_delete_key (pub, s); |
} |
|
gpgme_recipients_enum_close (rset, &ctx); |
|
|
} /* delete_keys_from_cache */ |
|
552 |
|
|
553 |
|
|
554 |
|
/* Delete all selected keys from the list view @lv. */ |
555 |
int |
int |
556 |
km_delete_keys (listview_ctrl_t lv, HWND dlg) |
km_delete_keys (listview_ctrl_t lv, HWND dlg) |
557 |
{ |
{ |
558 |
gpgme_error_t err; |
gpgme_error_t err; |
559 |
gpgme_recipients_t rset; |
gpgme_ctx_t ctx; |
560 |
|
gpgme_key_t *rset, k; |
561 |
char keyid[32], uid[256], date[64], keylen[64]; |
char keyid[32], uid[256], date[64], keylen[64]; |
562 |
int with_seckey, seckey_type=0; |
int with_seckey=0, seckey_type=0, confirm=0; |
563 |
int i, rc, n; |
int i, rc, n, k_pos=0; |
564 |
|
|
565 |
if( listview_get_curr_pos( lv ) == -1 ) { |
if (listview_get_curr_pos (lv) == -1) { |
566 |
msg_box( dlg, _("Please select a key."), _("Key Manager"), MB_ERR ); |
msg_box (dlg, _("Please select a key."), _("Key Manager"), MB_ERR); |
567 |
return WPTERR_GENERAL; |
return WPTERR_GENERAL; |
568 |
} |
} |
569 |
|
|
570 |
err = gpgme_recipients_new (&rset); |
if (listview_count_items (lv, 1) > 8) { |
571 |
if (err) |
i = msg_box (NULL, _("Do you really want to confirm each key?"), |
572 |
BUG (0); |
_("Delete Confirmation"), MB_YESNOCANCEL|MB_ICONQUESTION); |
573 |
|
if (i == IDCANCEL) |
574 |
|
return 0; |
575 |
|
if (i != IDNO) |
576 |
|
confirm = 1; |
577 |
|
} |
578 |
|
else |
579 |
|
confirm = 1; |
580 |
|
|
581 |
n = listview_count_items (lv, 0); |
n = listview_count_items (lv, 0); |
582 |
|
rset = (gpgme_key_t *)calloc (n+1, sizeof (gpgme_key_t)); |
583 |
|
if (!rset) |
584 |
|
BUG (NULL); |
585 |
for( i = 0; i < n; i++ ) { |
for( i = 0; i < n; i++ ) { |
586 |
if( listview_get_item_state( lv, i ) ) { |
if( listview_get_item_state( lv, i ) ) { |
587 |
listview_get_item_text( lv, i, 0, uid, sizeof uid - 1 ); |
listview_get_item_text( lv, i, 0, uid, sizeof uid - 1 ); |
588 |
listview_get_item_text( lv, i, 1, keyid, sizeof keyid - 1 ); |
listview_get_item_text( lv, i, 1, keyid, sizeof keyid - 1 ); |
589 |
listview_get_item_text( lv, i, 3, keylen, sizeof keylen - 1 ); |
listview_get_item_text( lv, i, 3, keylen, sizeof keylen - 1 ); |
590 |
listview_get_item_text( lv, i, 7, date, sizeof date - 1 ); |
listview_get_item_text( lv, i, 7, date, sizeof date - 1 ); |
591 |
seckey_type = km_check_for_seckey( lv, i, NULL ); |
seckey_type = km_check_for_seckey (lv, i, NULL); |
592 |
if( !seckey_type ) { |
if (confirm && !seckey_type) { |
593 |
rc = log_box( _("Key Manager"), MB_YESNO|MB_ICONWARNING, |
rc = log_box( _("Key Manager"), MB_YESNO|MB_ICONWARNING, |
594 |
_("Do you really want to delete this key?\n\n" |
_("Do you really want to delete this key?\n\n" |
595 |
"pub %s %s %s\n" |
"pub %s %s %s\n" |
596 |
" \"%s\""), keylen, keyid, date, uid ); |
" \"%s\""), keylen, keyid, date, uid ); |
597 |
if( rc == IDYES ) |
if (rc == IDYES) { |
598 |
gpgme_recipients_add_name( rset, keyid ); |
get_pubkey (keyid, &k); |
599 |
|
rset[k_pos++] = k; |
600 |
|
} |
601 |
with_seckey = 0; |
with_seckey = 0; |
602 |
} |
} |
603 |
else { |
else if (confirm) { |
604 |
rc = log_box( _("Key Manager"), MB_YESNO|MB_ICONWARNING, |
rc = log_box( _("Key Manager"), MB_YESNO|MB_ICONWARNING, |
605 |
_("Do you really want to delete this KEY PAIR?\n\n" |
_("Do you really want to delete this KEY PAIR?\n\n" |
606 |
"Please remember that you are not able to decrypt\n" |
"Please remember that you are not able to decrypt\n" |
609 |
"pub/sec %s %s %s\n" |
"pub/sec %s %s %s\n" |
610 |
" \"%s\""), keylen, keyid, date, uid ); |
" \"%s\""), keylen, keyid, date, uid ); |
611 |
if( rc == IDYES ) { |
if( rc == IDYES ) { |
612 |
if( seckey_type == 2 ) |
if (seckey_type == 2) |
613 |
msg_box( dlg, _("The actual secret key is stored on a smartcard.\n" |
msg_box( dlg, _("The actual secret key is stored on a smartcard.\n" |
614 |
"Only the public key and the secret key \n" |
"Only the public key and the secret key \n" |
615 |
"placeholder will be deleted.\n"), _("Key Manager"), MB_OK ); |
"placeholder will be deleted.\n"), _("Key Manager"), MB_OK ); |
616 |
gpgme_recipients_add_name( rset, keyid ); |
get_pubkey (keyid, &k); |
617 |
|
rset[k_pos++] = k; |
618 |
} |
} |
619 |
with_seckey = 1; |
with_seckey = 1; |
620 |
} |
} |
621 |
|
else { |
622 |
|
with_seckey = 1; |
623 |
|
get_pubkey (keyid, &k); |
624 |
|
rset[k_pos++] = k; |
625 |
|
} |
626 |
} |
} |
627 |
} |
} |
628 |
|
|
629 |
if (!gpgme_recipients_count (rset)) { |
if (k_pos == 0) { |
630 |
gpgme_recipients_release (rset); |
free (rset); |
631 |
return 0; |
return 0; |
632 |
} |
} |
633 |
|
|
634 |
err = gpgme_op_delete_keys (rset, with_seckey); |
err = gpgme_new (&ctx); |
635 |
if (err) { |
if (err) |
636 |
if (err == GPGME_Invalid_Key) |
BUG (NULL); |
637 |
msg_box (dlg, _("No such key."), _("Key Manager"), MB_INFO); |
n=k_pos; |
638 |
else if (err == GPGME_Conflict) |
for (i=0; i < k_pos; i++) { |
639 |
msg_box (dlg, _("Must delete secret key first."), _("Key Manager"), MB_INFO); |
err = gpgme_op_delete (ctx, rset[i], with_seckey); |
640 |
else |
if (err) |
641 |
msg_box (dlg, gpgme_strerror (err), _("Key Manager"), MB_ERR); |
msg_box (dlg, gpgme_strerror (err), _("Key Manager"), MB_ERR); |
642 |
return FALSE; |
else |
643 |
|
n--; |
644 |
} |
} |
645 |
show_msg (dlg, 1500, _("GnuPG Status: Finished")); |
if (n == 0) |
646 |
|
show_msg (dlg, 1500, _("GnuPG Status: Finished")); |
647 |
|
gpgme_release (ctx); |
648 |
listview_del_items (lv); |
listview_del_items (lv); |
649 |
if (keyring_check_last_access ()) |
delete_keys_from_cache (rset, k_pos); |
650 |
keycache_set_reload (1); |
free (rset); |
|
delete_keys_from_cache (rset); |
|
|
gpgme_recipients_release (rset); |
|
651 |
|
|
652 |
return (int)err; |
return (int)err; |
653 |
} /* km_delete_keys */ |
} |
654 |
|
|
655 |
|
|
656 |
int |
int |
684 |
int |
int |
685 |
km_send_to_mail_recipient( listview_ctrl_t lv, HWND dlg ) |
km_send_to_mail_recipient( listview_ctrl_t lv, HWND dlg ) |
686 |
{ |
{ |
687 |
|
#if 0 /*FIXME*/ |
688 |
gpgme_key_t key; |
gpgme_key_t key; |
689 |
gpgme_ctx_t ctx=NULL; |
gpgme_ctx_t ctx=NULL; |
690 |
gpgme_recipients_t rset=NULL; |
gpgme_recipients_t rset=NULL; |
705 |
listview_get_item_text( lv, pos, 1, keyid, sizeof keyid-1 ); |
listview_get_item_text( lv, pos, 1, keyid, sizeof keyid-1 ); |
706 |
if( get_pubkey( keyid, &key ) ) |
if( get_pubkey( keyid, &key ) ) |
707 |
BUG( NULL ); |
BUG( NULL ); |
708 |
s = gpgme_key_get_string_attr( key, GPGME_ATTR_NAME, NULL, 0 ); |
s = key->uids->name; |
709 |
GetTempPath (sizeof tmp-1, tmp); |
GetTempPath (sizeof tmp-1, tmp); |
710 |
strncat (tmp, s, sizeof tmp-200); |
strncat (tmp, s, sizeof tmp-200); |
711 |
strncat (tmp, ".asc", sizeof tmp-200); |
strncat (tmp, ".asc", sizeof tmp-200); |
717 |
if( !rc ) |
if( !rc ) |
718 |
rc = gpgme_new( &ctx ); |
rc = gpgme_new( &ctx ); |
719 |
if( !rc ) { |
if( !rc ) { |
720 |
gpgme_control( ctx, GPGME_CTRL_ARMOR, 1 ); |
gpgme_set_armor (ctx, 1); |
721 |
rc = gpgme_op_file_export( ctx, rset, p ); |
rc = gpgme_op_file_export( ctx, rset, p ); |
722 |
} |
} |
723 |
if( rc ) |
if( rc ) |
728 |
gpgme_recipients_release( rset ); |
gpgme_recipients_release( rset ); |
729 |
gpgme_release( ctx ); |
gpgme_release( ctx ); |
730 |
return rc; |
return rc; |
731 |
|
#endif |
732 |
|
return 0; |
733 |
} |
} |
734 |
|
|
735 |
|
|
786 |
|
|
787 |
|
|
788 |
void |
void |
789 |
km_set_clip_info( const char *uid ) |
km_set_clip_info (const char *uid) |
790 |
{ |
{ |
791 |
char buf[256]; |
char buf[256]; |
792 |
|
|
793 |
km_get_clip_info (uid, buf, 255); |
key_get_clip_info (uid, buf, 255); |
794 |
set_clip_text( NULL, buf, strlen( buf ) ); |
set_clip_text (NULL, buf, strlen (buf)); |
795 |
} /* km_set_clip_info */ |
} /* km_set_clip_info */ |
796 |
|
|
797 |
|
|
798 |
|
|
799 |
|
/* Return TRUE if the key in the list @lv at pos @pos is an |
800 |
|
old version 3 key. */ |
801 |
int |
int |
802 |
km_key_is_v3 (listview_ctrl_t lv, int pos) |
km_key_is_v3 (listview_ctrl_t lv, int pos) |
803 |
{ |
{ |
807 |
listview_get_item_text (lv, pos, 1, keyid, sizeof keyid-1); |
listview_get_item_text (lv, pos, 1, keyid, sizeof keyid-1); |
808 |
if (get_pubkey (keyid, &pk)) |
if (get_pubkey (keyid, &pk)) |
809 |
BUG (NULL); |
BUG (NULL); |
810 |
return gpgme_key_get_ulong_attr (pk, GPGME_ATTR_VERSION, NULL, 0); |
if (strlen (pk->subkeys->fpr) == 32 && |
811 |
} /* km_key_is_v3 */ |
pk->subkeys->pubkey_algo == GPGME_PK_RSA) |
812 |
|
return -1; |
813 |
|
return 0; |
814 |
|
} |
815 |
|
|
816 |
|
|
817 |
void |
void |
818 |
km_update_default_key_str (HWND dlg, int * ret_len) |
km_update_default_key_str (HWND dlg) |
819 |
{ |
{ |
820 |
char * keyid, defkeyinf[512]; |
char * keyid, defkeyinf[512]; |
821 |
const char * fmt; |
const char * fmt; |
828 |
fmt = _("Default Key: %s"); |
fmt = _("Default Key: %s"); |
829 |
else |
else |
830 |
fmt = _("Default Key: 0x%s"); |
fmt = _("Default Key: 0x%s"); |
831 |
_snprintf( defkeyinf, sizeof defkeyinf - 1, fmt, keyid ); |
_snprintf (defkeyinf, sizeof defkeyinf - 1, fmt, keyid); |
832 |
SetWindowText( dlg, defkeyinf ); |
SendMessage (dlg, SB_SETTEXT, 0, (LPARAM)defkeyinf); |
833 |
*ret_len = strlen( defkeyinf ); |
free_if_alloc (keyid); |
|
free_if_alloc( keyid ); |
|
834 |
} /* km_return_default_key_str */ |
} /* km_return_default_key_str */ |
835 |
|
|
836 |
|
|
837 |
void |
void |
838 |
km_complete_status_bar( HWND sb, listview_ctrl_t lv, int startpos ) |
km_complete_status_bar (HWND sb, listview_ctrl_t lv) |
839 |
{ |
{ |
840 |
char text[384]; |
char txt_sec[128], txt_pub[128]; |
841 |
int nkeys = 0, nsec = 0, i; |
int nkeys = 0, nsec = 0, i; |
842 |
|
|
843 |
GetWindowText( sb, text, sizeof text -1 ); |
nkeys = listview_count_items (lv, 0); |
844 |
nkeys = listview_count_items( lv, 0 ); |
for (i = 0; i < nkeys; i++) { |
845 |
for( i = 0; i < nkeys; i++ ) { |
if (km_check_for_seckey (lv, i, NULL)) |
|
if( km_check_for_seckey( lv, i, NULL ) ) |
|
846 |
nsec++; |
nsec++; |
847 |
} |
} |
848 |
_snprintf( text+startpos, sizeof text-1, " %d keys (%d secret keys)", nkeys, nsec ); |
_snprintf (txt_sec, sizeof (txt_sec)-1, "%d secret keys", nsec); |
849 |
SetWindowText( sb, text ); |
_snprintf (txt_pub, sizeof (txt_pub)-1, "%d keys", nkeys); |
850 |
|
SendMessage (sb, SB_SETTEXT, 1, (LPARAM)txt_sec); |
851 |
|
SendMessage (sb, SB_SETTEXT, 2, (LPARAM)txt_pub); |
852 |
} /* km_complete_status_bar */ |
} /* km_complete_status_bar */ |
853 |
|
|
854 |
|
|
855 |
void |
|
856 |
|
int |
857 |
km_set_implicit_trust (HWND dlg, listview_ctrl_t lv, int pos) |
km_set_implicit_trust (HWND dlg, listview_ctrl_t lv, int pos) |
858 |
{ |
{ |
859 |
gpgme_error_t err; |
GpgKeyEdit *ke; |
860 |
gpgme_ctx_t ctx; |
gpgme_error_t err; |
|
gpgme_editkey_t ek; |
|
861 |
char keyid[32]; |
char keyid[32]; |
862 |
|
|
863 |
listview_get_item_text (lv, pos, 1, keyid, 31); |
listview_get_item_text (lv, pos, 1, keyid, 31); |
864 |
err = gpgme_new (&ctx); |
|
865 |
if (err) |
ke = new GpgKeyEdit (keyid); |
866 |
BUG (0); |
if (!ke) |
|
err = gpgme_editkey_new (&ek); |
|
|
if (err) |
|
867 |
BUG (0); |
BUG (0); |
|
gpgme_set_edit_ctx (ctx, ek, GPGME_EDITKEY_TRUST); |
|
|
gpgme_editkey_trust_set (ek, 5); |
|
868 |
|
|
869 |
err = gpgme_op_editkey (ctx, keyid); |
err = ke->setTrust (GPGME_VALIDITY_ULTIMATE); |
870 |
if (err) |
if (err) |
871 |
msg_box (dlg, gpgme_strerror (err), _("Key Manager"), MB_ERR); |
msg_box (dlg, gpgme_strerror (err), _("Key Manager"), MB_ERR); |
872 |
else { |
else |
873 |
show_msg (dlg, 1500, _("GnuPG Status: Finished")); |
show_msg (dlg, 1500, _("GnuPG Status: Finished")); |
|
keycache_set_reload (1); |
|
|
} |
|
874 |
|
|
875 |
gpgme_release (ctx); |
delete ke; |
876 |
gpgme_editkey_release (ek); |
return (int)err; |
877 |
} |
} |
878 |
|
|
879 |
|
|
880 |
|
void |
881 |
|
km_find_key (HWND dlg, listview_ctrl_t lv) |
882 |
|
{ |
883 |
|
int oldpos = listview_get_curr_pos (lv); |
884 |
|
int n; |
885 |
|
char *name = get_input_dialog (dlg, "Search", "Search for:"); |
886 |
|
if (name == NULL) |
887 |
|
return; |
888 |
|
if (oldpos < 0) |
889 |
|
oldpos = 0; |
890 |
|
n = listview_find (lv, name); |
891 |
|
if (n != -1) { |
892 |
|
listview_select_one (lv, n); |
893 |
|
listview_scroll (lv, oldpos, n); |
894 |
|
} |
895 |
|
else { |
896 |
|
const char *s = _("String pattern \"%s\" not found."); |
897 |
|
char *p = new char[strlen (s) + strlen (name) + 2]; |
898 |
|
if (!p) |
899 |
|
BUG (0); |
900 |
|
sprintf (p, s, name); |
901 |
|
msg_box (dlg, p, _("Key Manager"), MB_INFO); |
902 |
|
free_if_alloc (p); |
903 |
|
} |
904 |
|
free_if_alloc (name); |
905 |
|
} |
906 |
|
|
907 |
|
|
908 |
|
|
909 |
|
void |
910 |
|
km_dump_key (gpgme_key_t key) |
911 |
|
{ |
912 |
|
#if _DEBUG |
913 |
|
log_box ("DEBUG", MB_OK, |
914 |
|
"%d %d %s %d\n%s", key->subkeys->length, |
915 |
|
key->subkeys->pubkey_algo, |
916 |
|
key->subkeys->keyid, |
917 |
|
key->subkeys->timestamp, |
918 |
|
key->uids->uid); |
919 |
|
#endif |
920 |
|
} |
921 |
|
|
922 |
|
#if 0 |
923 |
gpg_optfile_t |
gpg_optfile_t |
924 |
km_groupdb_open( void ) |
km_groupdb_open( void ) |
925 |
{ |
{ |
940 |
int |
int |
941 |
km_groupdb_expand_recipients( const char *name, gpgme_recipients_t rset ) |
km_groupdb_expand_recipients( const char *name, gpgme_recipients_t rset ) |
942 |
{ |
{ |
943 |
gpgme_keycache_t kc; |
gpg_keycache_t kc; |
944 |
gpgme_key_t pk; |
gpgme_key_t pk; |
945 |
gpg_optfile_t opt; |
gpg_optfile_t opt; |
946 |
gpg_group_t grp; |
gpg_group_t grp; |
965 |
for( mbr = grp->list; mbr; mbr = mbr->next ) { |
for( mbr = grp->list; mbr; mbr = mbr->next ) { |
966 |
if( gpgme_keycache_find_key( kc, mbr->name, 0, &pk ) ) |
if( gpgme_keycache_find_key( kc, mbr->name, 0, &pk ) ) |
967 |
BUG( NULL ); |
BUG( NULL ); |
968 |
n = gpgme_key_count_items( pk, GPGME_ATTR_USERID ); |
n = count_userids (pk); |
969 |
while( n-- ) { |
while( n-- ) { |
970 |
const char * s = gpgme_key_get_string_attr( pk, GPGME_ATTR_USERID, NULL, n ); |
gpgme_user_id_t u = get_nth_userid (pk, n); |
971 |
|
const char * s = u->uid; |
972 |
if( s && stristr( s, mbr->name ) |
if( s && stristr( s, mbr->name ) |
973 |
&& gpgme_key_get_ulong_attr( pk, GPGME_ATTR_VALIDITY, NULL, n ) < 3 ) |
&& u->validity < 3 ) |
974 |
no_trust++; |
no_trust++; |
975 |
} |
} |
976 |
} |
} |
1177 |
else |
else |
1178 |
return km_groups_del_main( gc ); |
return km_groups_del_main( gc ); |
1179 |
} /* km_groups_del */ |
} /* km_groups_del */ |
1180 |
|
#endif |