1 |
twoaday |
2 |
/* wptImportList.cpp |
2 |
|
|
* Copyright (C) 2001-2004 Timo Schulz |
3 |
|
|
* |
4 |
|
|
* This file is part of WinPT. |
5 |
|
|
* |
6 |
|
|
* WinPT is free software; you can redistribute it and/or |
7 |
|
|
* modify it under the terms of the GNU General Public License |
8 |
|
|
* as published by the Free Software Foundation; either version 2 |
9 |
|
|
* of the License, or (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 GNU |
14 |
|
|
* 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 <sys/types.h> |
23 |
|
|
|
24 |
|
|
#include "wptTypes.h" |
25 |
|
|
#include "wptGPG.h" |
26 |
|
|
#include "wptCommonCtl.h" |
27 |
|
|
#include "wptKeylist.h" |
28 |
|
|
#include "wptNLS.h" |
29 |
|
|
#include "wptErrors.h" |
30 |
|
|
#include "wptUTF8.h" |
31 |
|
|
#include "wptW32API.h" |
32 |
|
|
#include "wptRegistry.h" |
33 |
|
|
#include "openpgp.h" |
34 |
|
|
|
35 |
|
|
|
36 |
twoaday |
23 |
static char* |
37 |
|
|
key_description (gpgme_key_t key) |
38 |
twoaday |
2 |
{ |
39 |
|
|
char * p; |
40 |
|
|
const char * type, * state; |
41 |
twoaday |
23 |
gpgme_pubkey_algo_t pkalgo; |
42 |
|
|
gpgme_pubkey_algo_t subalgo = (gpgme_pubkey_algo_t)0; |
43 |
twoaday |
2 |
|
44 |
twoaday |
23 |
if (!key->subkeys) |
45 |
|
|
return NULL; |
46 |
twoaday |
2 |
p = new char[64+1]; |
47 |
twoaday |
23 |
if (!p) |
48 |
|
|
BUG (NULL); |
49 |
|
|
pkalgo = key->subkeys->pubkey_algo; |
50 |
|
|
if (key->subkeys->next) |
51 |
|
|
subalgo = key->subkeys->next->pubkey_algo; |
52 |
|
|
if (key->revoked) |
53 |
twoaday |
2 |
state = _("Revoked" ); |
54 |
twoaday |
23 |
else if (key->expired) |
55 |
twoaday |
2 |
state = _("Expired" ); |
56 |
|
|
else |
57 |
|
|
state = ""; |
58 |
twoaday |
23 |
if (key->secret) |
59 |
twoaday |
2 |
type = _("secret key"); |
60 |
|
|
else |
61 |
|
|
type = _("public key"); |
62 |
|
|
|
63 |
twoaday |
23 |
if (!subalgo) |
64 |
|
|
_snprintf (p, 64, "%s %s %s", state, |
65 |
|
|
get_key_pubalgo (pkalgo),type); |
66 |
twoaday |
2 |
else |
67 |
twoaday |
23 |
_snprintf (p, 64, "%s %s/%s %s", state, |
68 |
|
|
get_key_pubalgo (pkalgo), |
69 |
|
|
get_key_pubalgo (subalgo), type); |
70 |
twoaday |
2 |
return p; |
71 |
|
|
} /* key_description */ |
72 |
|
|
|
73 |
|
|
|
74 |
|
|
static int |
75 |
|
|
implist_add_key( listview_ctrl_t lv, int pos, gpgme_key_t key ) |
76 |
|
|
{ |
77 |
|
|
char *uid = NULL; |
78 |
|
|
char buf[128], * desc; |
79 |
|
|
const char *t; |
80 |
twoaday |
23 |
u32 tt, tt2=0; |
81 |
twoaday |
2 |
|
82 |
|
|
if( listview_add_item( lv, " " ) ) |
83 |
|
|
return WPTERR_GENERAL; |
84 |
|
|
|
85 |
twoaday |
23 |
t = key->uids->uid; |
86 |
twoaday |
2 |
if( !t || strlen( t ) < 5 ) { |
87 |
|
|
t = _("Invalid user ID"); |
88 |
|
|
listview_add_sub_item( lv, pos, 0, t ); |
89 |
|
|
} |
90 |
|
|
else { |
91 |
|
|
uid = utf8_to_wincp (t, strlen (t)); |
92 |
|
|
if (uid) { |
93 |
|
|
listview_add_sub_item( lv, pos, 0, uid ); |
94 |
|
|
free( uid ); |
95 |
|
|
} |
96 |
|
|
} |
97 |
|
|
|
98 |
twoaday |
23 |
tt = key->subkeys->length; |
99 |
|
|
if (key->subkeys->next) |
100 |
|
|
tt2 = key->subkeys->next->length; |
101 |
twoaday |
2 |
if( tt && tt2 ) |
102 |
|
|
_snprintf( buf, sizeof buf - 1, "%d/%d", tt, tt2 ); |
103 |
|
|
else |
104 |
|
|
_snprintf( buf, sizeof buf-1, "%d", tt ); |
105 |
|
|
listview_add_sub_item( lv, pos, 1, buf ); |
106 |
|
|
|
107 |
twoaday |
23 |
t = key->subkeys->keyid; |
108 |
twoaday |
2 |
if( !t || strlen( t ) < 8 ) |
109 |
|
|
t = "DEADBEEFDEADBEEF"; |
110 |
|
|
_snprintf( buf, sizeof buf -1, "0x%s", t + 8 ); |
111 |
|
|
listview_add_sub_item( lv, pos, 2, buf ); |
112 |
|
|
|
113 |
|
|
|
114 |
twoaday |
23 |
tt = key->subkeys->timestamp; |
115 |
twoaday |
2 |
t = get_key_created( tt ); |
116 |
|
|
if( !t ) |
117 |
|
|
t = "????-??-??"; |
118 |
|
|
listview_add_sub_item( lv, pos, 3, (char *)t ); |
119 |
|
|
|
120 |
|
|
desc = key_description( key ); |
121 |
|
|
if( desc ) { |
122 |
|
|
listview_add_sub_item( lv, pos, 4, desc ); |
123 |
|
|
free_if_alloc( desc ); |
124 |
|
|
} |
125 |
|
|
|
126 |
|
|
return 0; |
127 |
|
|
} /* implist_add_key */ |
128 |
|
|
|
129 |
|
|
|
130 |
|
|
int |
131 |
|
|
implist_build( listview_ctrl_t *lv, HWND ctrl ) |
132 |
|
|
{ |
133 |
|
|
struct listview_ctrl_s *c; |
134 |
|
|
struct listview_column_s implist[] = { |
135 |
|
|
{ 0, 190, (char *)_("User ID") }, |
136 |
|
|
{ 1, 66, (char *)_("Size") }, |
137 |
|
|
{ 2, 80, (char *)_("Key ID") }, |
138 |
|
|
{ 3, 72, (char *)_("Creation") }, |
139 |
|
|
{ 4, 132, (char *)_("Type") }, |
140 |
|
|
{ 0, 0, NULL } |
141 |
|
|
}; |
142 |
|
|
int j, rc = 0; |
143 |
|
|
|
144 |
|
|
rc = listview_new( &c ); |
145 |
|
|
if( rc ) |
146 |
|
|
return rc; |
147 |
|
|
c->ctrl = ctrl; |
148 |
|
|
for( j = 0; implist[j].fieldname != NULL; j++ ) |
149 |
|
|
listview_add_column( c, &implist[j] ); |
150 |
|
|
listview_set_ext_style( c ); |
151 |
|
|
*lv = c; |
152 |
|
|
return 0; |
153 |
|
|
} /* implist_build */ |
154 |
|
|
|
155 |
|
|
|
156 |
|
|
int |
157 |
|
|
implist_load( listview_ctrl_t lv, const char *file, |
158 |
|
|
int *r_revcerts, int *r_seckeys ) |
159 |
|
|
{ |
160 |
|
|
gpgme_ctx_t ctx; |
161 |
|
|
gpgme_data_t dh, list; |
162 |
|
|
gpgme_error_t err; |
163 |
|
|
gpgme_key_t pubk, key; |
164 |
|
|
int rc = 0; |
165 |
twoaday |
23 |
size_t n; |
166 |
twoaday |
2 |
char *p, keyid[16+1]; |
167 |
|
|
char * pending_line = NULL; |
168 |
|
|
|
169 |
|
|
if( file && *file ) { |
170 |
twoaday |
23 |
err = gpgme_data_new_from_file (&dh, file, 1); |
171 |
twoaday |
2 |
if( err ) { |
172 |
|
|
msg_box( NULL, gpgme_strerror( err ), _("Import"), MB_ERR ); |
173 |
|
|
return WPTERR_GENERAL; |
174 |
|
|
} |
175 |
|
|
} |
176 |
|
|
else { |
177 |
twoaday |
23 |
err = gpg_data_new_from_clipboard (&dh, 0); |
178 |
twoaday |
2 |
if( err ) { |
179 |
|
|
msg_box( NULL, gpgme_strerror( err ), _("Import"), MB_ERR ); |
180 |
|
|
return WPTERR_CLIP_OPEN; |
181 |
|
|
} |
182 |
|
|
} |
183 |
twoaday |
23 |
|
184 |
|
|
#if 0 /* FIXME */ |
185 |
|
|
p = gpgme_data_release_and_get_mem (dh, &n); |
186 |
|
|
p[n] = 0; |
187 |
twoaday |
2 |
/* fixme: the code could need a little more work because it's |
188 |
|
|
no very flexible. */ |
189 |
|
|
if( p && stristr( p, _("A revocation certificate should follow") ) ) { |
190 |
|
|
gpgme_revcert_t cert; |
191 |
|
|
gpgme_revcert_new( &cert ); |
192 |
|
|
rc = gpgme_revcert_parse( p, cert, keyid ); |
193 |
|
|
if( rc ) { |
194 |
|
|
msg_box( NULL, gpgme_strerror( (gpgme_error_t)rc ), _("Revocation"), MB_ERR ); |
195 |
|
|
free( p ); |
196 |
|
|
return WPTERR_GENERAL; |
197 |
|
|
} |
198 |
|
|
if( get_pubkey( keyid, &pubk ) ) |
199 |
|
|
BUG( NULL ); |
200 |
|
|
gpgme_implist_create_revcert( &key, cert, pubk ); |
201 |
|
|
implist_add_key( lv, 0, key ); |
202 |
|
|
gpgme_key_release( key ); |
203 |
|
|
gpgme_revcert_release( cert ); |
204 |
|
|
if( r_revcerts ) |
205 |
|
|
*r_revcerts = 1; |
206 |
|
|
free( p ); |
207 |
|
|
return 0; |
208 |
|
|
} |
209 |
|
|
safe_free (p); |
210 |
twoaday |
23 |
#endif |
211 |
twoaday |
2 |
err = gpgme_data_new( &list ); |
212 |
|
|
if( err ) |
213 |
|
|
BUG( NULL ); |
214 |
twoaday |
23 |
err = gpgme_new (&ctx); |
215 |
|
|
if (err) |
216 |
twoaday |
2 |
BUG( NULL ); |
217 |
|
|
|
218 |
|
|
err = gpgme_op_import_list( ctx, dh, list ); |
219 |
|
|
if( err ) { |
220 |
|
|
gpgme_release( ctx ); |
221 |
|
|
gpgme_data_release( dh ); |
222 |
|
|
gpgme_data_release( list ); |
223 |
|
|
if (err == GPGME_General_Error) { |
224 |
|
|
msg_box (NULL, _("It is possible that the ASCII-Armor is damaged\n" |
225 |
|
|
"and thus a CRC error occurs."), _("Import"), MB_ERR); |
226 |
|
|
} |
227 |
|
|
else |
228 |
|
|
msg_box( NULL, gpgme_strerror( err ), _("Import"), MB_ERR ); |
229 |
|
|
return WPTERR_GENERAL; |
230 |
|
|
} |
231 |
|
|
|
232 |
|
|
gpgme_release( ctx ); |
233 |
|
|
gpgme_data_release( dh ); |
234 |
|
|
|
235 |
|
|
if( r_seckeys ) |
236 |
|
|
*r_seckeys = 0; |
237 |
|
|
while( !rc ) { |
238 |
|
|
/* xxx: if the key has a direct key signature, the user-id field |
239 |
|
|
in the --with-colons mode is empty! */ |
240 |
|
|
rc = gpgme_op_import_list_next( list, &pending_line, &key ); |
241 |
|
|
if( !rc ) |
242 |
|
|
rc = implist_add_key( lv, 0, key ); |
243 |
twoaday |
23 |
if( key->secret ) |
244 |
twoaday |
2 |
(*r_seckeys)++; |
245 |
|
|
gpgme_key_release( key ); |
246 |
|
|
} |
247 |
|
|
gpgme_data_release( list ); |
248 |
|
|
|
249 |
|
|
return rc; |
250 |
|
|
} /* implist_load */ |
251 |
|
|
|
252 |
|
|
|
253 |
|
|
void |
254 |
|
|
implist_delete( listview_ctrl_t lv ) |
255 |
|
|
{ |
256 |
|
|
if( lv ) { |
257 |
|
|
listview_release( lv ); |
258 |
|
|
} |
259 |
|
|
} /* implist_delete */ |