1 |
twoaday |
2 |
/* wptGPGME.cpp - WinPT GPGME interface |
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 <sys/types.h> |
22 |
|
|
#include <windows.h> |
23 |
|
|
|
24 |
|
|
#include "../resource.h" |
25 |
|
|
#include "wptNLS.h" |
26 |
|
|
#include "wptGPG.h" |
27 |
|
|
#include "wptErrors.h" |
28 |
|
|
#include "wptTypes.h" |
29 |
|
|
#include "wptW32API.h" |
30 |
|
|
#include "wptVersion.h" |
31 |
|
|
#include "wptCommonCtl.h" |
32 |
|
|
#include "wptContext.h" |
33 |
|
|
#include "wptRegistry.h" |
34 |
|
|
#include "wptDlgs.h" |
35 |
|
|
|
36 |
|
|
#include "openpgp.h" |
37 |
|
|
|
38 |
|
|
BOOL CALLBACK keycache_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam); |
39 |
|
|
void progress_cleanup (progress_filter_s * pfx); |
40 |
|
|
|
41 |
|
|
static gpgme_keycache_t pub = NULL; |
42 |
|
|
static gpgme_keycache_t sec = NULL; |
43 |
|
|
static unsigned int reload = 0; |
44 |
|
|
|
45 |
|
|
|
46 |
|
|
void |
47 |
|
|
keycache_reload (HWND dlg) |
48 |
|
|
{ |
49 |
|
|
refresh_cache_s rcs = {0}; |
50 |
|
|
|
51 |
|
|
rcs.kr_reload = rcs.kr_update = 1; |
52 |
|
|
rcs.tr_update = 1; |
53 |
|
|
DialogBoxParam( glob_hinst, (LPCSTR)IDD_WINPT_KEYCACHE, dlg, |
54 |
|
|
keycache_dlg_proc, (LPARAM)&rcs ); |
55 |
|
|
} /* keycache_reload */ |
56 |
|
|
|
57 |
|
|
void |
58 |
|
|
keycache_release (void) |
59 |
|
|
{ |
60 |
|
|
int n = gpgme_keycache_count (pub); |
61 |
|
|
char tmpbuf[64]; |
62 |
|
|
|
63 |
|
|
sprintf (tmpbuf, "%d", n); |
64 |
|
|
set_reg_key (HKEY_CURRENT_USER, "Software\\WinPT", "nKeys", tmpbuf); |
65 |
|
|
|
66 |
|
|
if (pub) { |
67 |
|
|
gpgme_keycache_release( pub ); |
68 |
|
|
pub = NULL; |
69 |
|
|
} |
70 |
|
|
if (sec) { |
71 |
|
|
gpgme_keycache_release( sec ); |
72 |
|
|
sec = NULL; |
73 |
|
|
} |
74 |
|
|
} /* keycache_release */ |
75 |
|
|
|
76 |
|
|
|
77 |
|
|
gpgme_error_t |
78 |
twoaday |
19 |
keycache_update (const char *keyid) |
79 |
twoaday |
2 |
{ |
80 |
twoaday |
19 |
return gpgme_keycache_update_key (pub, keyid); |
81 |
|
|
} |
82 |
|
|
|
83 |
|
|
|
84 |
|
|
gpgme_error_t |
85 |
|
|
keycache_init (const char *pubring, const char * secring) |
86 |
|
|
{ |
87 |
twoaday |
2 |
struct progress_filter_s pfx; |
88 |
|
|
gpgme_error_t err; |
89 |
|
|
int val = 0; |
90 |
|
|
char * p; |
91 |
|
|
|
92 |
|
|
if (reload) { |
93 |
|
|
keycache_release (); |
94 |
|
|
reload = 0; |
95 |
|
|
} |
96 |
|
|
p = get_reg_entry (HKEY_CURRENT_USER, "Software\\WinPT", "nKeys"); |
97 |
twoaday |
19 |
if (p && *p != ' ') { |
98 |
twoaday |
2 |
val = atoi (p); |
99 |
|
|
free_if_alloc (p); |
100 |
|
|
memset (&pfx, 0, sizeof pfx); |
101 |
|
|
} |
102 |
|
|
|
103 |
|
|
err = gpgme_keycache_new (&pub); |
104 |
|
|
if (err) |
105 |
|
|
return err; |
106 |
|
|
if (val != 0) |
107 |
|
|
gpgme_keycache_set_cb (pub, progress_callback, &pfx, val); |
108 |
|
|
err = gpgme_keycache_new (&sec); |
109 |
|
|
if (!err) |
110 |
|
|
err = gpgme_keycache_init (pub, NULL, 0); |
111 |
|
|
if (!err) |
112 |
|
|
err = gpgme_keycache_init( sec, NULL, 1 ); |
113 |
|
|
if( !err && pubring && *pubring ) |
114 |
|
|
err = gpgme_keycache_prepare( pub, pubring, NULL ); |
115 |
|
|
if( !err && secring && * secring ) |
116 |
|
|
err = gpgme_keycache_prepare( sec, NULL, secring ); |
117 |
|
|
if (!err) |
118 |
|
|
gpgme_keycache_sync (pub, sec); |
119 |
|
|
if (val != 0) |
120 |
|
|
progress_cleanup (&pfx); |
121 |
|
|
return err; |
122 |
|
|
} /* keycache_init */ |
123 |
|
|
|
124 |
|
|
|
125 |
|
|
void |
126 |
|
|
keycache_set_reload( int yes ) |
127 |
|
|
{ |
128 |
|
|
reload = yes; |
129 |
|
|
} /* keycache_set_reload */ |
130 |
|
|
|
131 |
|
|
|
132 |
|
|
int |
133 |
|
|
keycache_get_reload( void ) |
134 |
|
|
{ |
135 |
|
|
return reload; |
136 |
|
|
} /* keycache_get_reload */ |
137 |
|
|
|
138 |
|
|
|
139 |
|
|
gpgme_keycache_t |
140 |
|
|
keycache_get_ctx( int is_pub ) |
141 |
|
|
{ |
142 |
|
|
return is_pub? pub : sec; |
143 |
|
|
} /* keycache_get_ctx */ |
144 |
|
|
|
145 |
|
|
|
146 |
|
|
static int |
147 |
|
|
get_key( const char *keyid, gpgme_key_t *r_key, int secret ) |
148 |
|
|
{ |
149 |
|
|
gpgme_keycache_t cache; |
150 |
|
|
gpgme_error_t err; |
151 |
|
|
int mode = secret? KEYCACHE_PRV : KEYCACHE_PUB; |
152 |
|
|
|
153 |
|
|
if( !keyid ) |
154 |
|
|
return WPTERR_GENERAL; |
155 |
|
|
if( r_key ) |
156 |
|
|
*r_key = NULL; |
157 |
|
|
cache = keycache_get_ctx( mode ); |
158 |
|
|
if( !cache ) |
159 |
|
|
BUG( NULL ); |
160 |
|
|
err = gpgme_keycache_find_key( cache, keyid, 0, r_key ); |
161 |
|
|
if( err ) |
162 |
|
|
return WPTERR_GENERAL; |
163 |
|
|
return 0; |
164 |
|
|
} /* get_key */ |
165 |
|
|
|
166 |
|
|
|
167 |
|
|
static int |
168 |
|
|
get_key2( const char * keyid, gpgme_key_t * r_key, int secret ) |
169 |
|
|
{ |
170 |
|
|
gpg_iobuf_t inp; |
171 |
|
|
char * p; |
172 |
|
|
int rc; |
173 |
|
|
|
174 |
|
|
p = get_gnupg_keyring (1, !NO_STRICT); |
175 |
|
|
if( !p ) |
176 |
|
|
BUG( NULL ); |
177 |
|
|
|
178 |
|
|
inp = gpg_iobuf_open( p ); |
179 |
|
|
if( !inp ) { |
180 |
|
|
const char *s = winpt_strerror( WPTERR_FILE_OPEN ); |
181 |
|
|
log_box( _("WinPT Error"), 0, "%s: %s", p, s ); |
182 |
|
|
free_if_alloc( p ); |
183 |
|
|
return NULL; |
184 |
|
|
} |
185 |
|
|
gpg_iobuf_ioctl( inp, 3, 1, NULL ); /* disable cache */ |
186 |
|
|
rc = gpgme_getkey_bykeyid( inp, keyid, r_key ); |
187 |
|
|
|
188 |
|
|
gpg_iobuf_close( inp ); |
189 |
|
|
free_if_alloc( p ); |
190 |
|
|
return rc; |
191 |
|
|
} /* get_key2 */ |
192 |
|
|
|
193 |
|
|
|
194 |
|
|
int |
195 |
|
|
get_pubkey( const char *keyid, gpgme_key_t *ret_key ) |
196 |
|
|
{ |
197 |
|
|
int rc = 0; |
198 |
|
|
|
199 |
|
|
if( pub && sec ) |
200 |
|
|
rc = get_key( keyid, ret_key, 0 ); |
201 |
|
|
else |
202 |
|
|
rc = get_key2( keyid, ret_key, 0 ); |
203 |
|
|
return rc; |
204 |
|
|
} /* get_pubkey */ |
205 |
|
|
|
206 |
|
|
|
207 |
|
|
int |
208 |
|
|
get_seckey( const char *keyid, gpgme_key_t *ret_skey ) |
209 |
|
|
{ |
210 |
|
|
int rc = 0; |
211 |
|
|
|
212 |
|
|
if( pub && sec ) |
213 |
|
|
rc = get_key( keyid, ret_skey, 1 ); |
214 |
|
|
else |
215 |
|
|
rc = get_key2( keyid, ret_skey, 1 ); |
216 |
|
|
return rc; |
217 |
|
|
} /* get_seckey */ |
218 |
|
|
|
219 |
|
|
|
220 |
|
|
int |
221 |
|
|
count_insecure_elgkeys (void) |
222 |
|
|
{ |
223 |
|
|
gpgme_keycache_t pc; |
224 |
|
|
gpgme_key_t key; |
225 |
|
|
int n=0; |
226 |
|
|
|
227 |
|
|
pc = keycache_get_ctx (1); |
228 |
|
|
if (!pc) |
229 |
|
|
BUG (0); |
230 |
|
|
while (!gpgme_keycache_next_key (pc, 0, &key)) { |
231 |
|
|
if (gpgme_key_get_ulong_attr (key, GPGME_ATTR_ALGO, NULL, 0) |
232 |
|
|
== GPGME_PK_ELG_ES) |
233 |
|
|
n++; |
234 |
|
|
} |
235 |
|
|
gpgme_keycache_rewind (pc); |
236 |
|
|
return n; |
237 |
|
|
} |