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 |
twoaday |
20 |
/*rcs.tr_update = 1;*/ |
53 |
twoaday |
2 |
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 |
20 |
keycache_update (int is_sec, const char *keyid) |
79 |
twoaday |
2 |
{ |
80 |
twoaday |
20 |
gpgme_keycache_t ctx = pub; |
81 |
|
|
if (is_sec) |
82 |
|
|
ctx = sec; |
83 |
|
|
return gpgme_keycache_update_key (ctx, is_sec, pub, keyid); |
84 |
twoaday |
19 |
} |
85 |
|
|
|
86 |
|
|
|
87 |
|
|
gpgme_error_t |
88 |
|
|
keycache_init (const char *pubring, const char * secring) |
89 |
|
|
{ |
90 |
twoaday |
2 |
struct progress_filter_s pfx; |
91 |
|
|
gpgme_error_t err; |
92 |
|
|
int val = 0; |
93 |
|
|
char * p; |
94 |
|
|
|
95 |
|
|
if (reload) { |
96 |
|
|
keycache_release (); |
97 |
|
|
reload = 0; |
98 |
|
|
} |
99 |
|
|
p = get_reg_entry (HKEY_CURRENT_USER, "Software\\WinPT", "nKeys"); |
100 |
twoaday |
19 |
if (p && *p != ' ') { |
101 |
twoaday |
2 |
val = atoi (p); |
102 |
|
|
free_if_alloc (p); |
103 |
|
|
memset (&pfx, 0, sizeof pfx); |
104 |
|
|
} |
105 |
|
|
|
106 |
|
|
err = gpgme_keycache_new (&pub); |
107 |
|
|
if (err) |
108 |
|
|
return err; |
109 |
|
|
if (val != 0) |
110 |
|
|
gpgme_keycache_set_cb (pub, progress_callback, &pfx, val); |
111 |
|
|
err = gpgme_keycache_new (&sec); |
112 |
|
|
if (!err) |
113 |
|
|
err = gpgme_keycache_init (pub, NULL, 0); |
114 |
|
|
if (!err) |
115 |
|
|
err = gpgme_keycache_init( sec, NULL, 1 ); |
116 |
|
|
if( !err && pubring && *pubring ) |
117 |
|
|
err = gpgme_keycache_prepare( pub, pubring, NULL ); |
118 |
|
|
if( !err && secring && * secring ) |
119 |
|
|
err = gpgme_keycache_prepare( sec, NULL, secring ); |
120 |
|
|
if (!err) |
121 |
|
|
gpgme_keycache_sync (pub, sec); |
122 |
|
|
if (val != 0) |
123 |
|
|
progress_cleanup (&pfx); |
124 |
|
|
return err; |
125 |
|
|
} /* keycache_init */ |
126 |
|
|
|
127 |
|
|
|
128 |
|
|
void |
129 |
|
|
keycache_set_reload( int yes ) |
130 |
|
|
{ |
131 |
|
|
reload = yes; |
132 |
|
|
} /* keycache_set_reload */ |
133 |
|
|
|
134 |
|
|
|
135 |
|
|
int |
136 |
|
|
keycache_get_reload( void ) |
137 |
|
|
{ |
138 |
|
|
return reload; |
139 |
|
|
} /* keycache_get_reload */ |
140 |
|
|
|
141 |
|
|
|
142 |
|
|
gpgme_keycache_t |
143 |
|
|
keycache_get_ctx( int is_pub ) |
144 |
|
|
{ |
145 |
|
|
return is_pub? pub : sec; |
146 |
|
|
} /* keycache_get_ctx */ |
147 |
|
|
|
148 |
|
|
|
149 |
|
|
static int |
150 |
|
|
get_key( const char *keyid, gpgme_key_t *r_key, int secret ) |
151 |
|
|
{ |
152 |
|
|
gpgme_keycache_t cache; |
153 |
|
|
gpgme_error_t err; |
154 |
|
|
int mode = secret? KEYCACHE_PRV : KEYCACHE_PUB; |
155 |
|
|
|
156 |
|
|
if( !keyid ) |
157 |
|
|
return WPTERR_GENERAL; |
158 |
|
|
if( r_key ) |
159 |
|
|
*r_key = NULL; |
160 |
|
|
cache = keycache_get_ctx( mode ); |
161 |
|
|
if( !cache ) |
162 |
|
|
BUG( NULL ); |
163 |
|
|
err = gpgme_keycache_find_key( cache, keyid, 0, r_key ); |
164 |
|
|
if( err ) |
165 |
|
|
return WPTERR_GENERAL; |
166 |
|
|
return 0; |
167 |
|
|
} /* get_key */ |
168 |
|
|
|
169 |
|
|
|
170 |
|
|
static int |
171 |
|
|
get_key2( const char * keyid, gpgme_key_t * r_key, int secret ) |
172 |
|
|
{ |
173 |
|
|
gpg_iobuf_t inp; |
174 |
|
|
char * p; |
175 |
|
|
int rc; |
176 |
|
|
|
177 |
|
|
p = get_gnupg_keyring (1, !NO_STRICT); |
178 |
|
|
if( !p ) |
179 |
|
|
BUG( NULL ); |
180 |
|
|
|
181 |
|
|
inp = gpg_iobuf_open( p ); |
182 |
|
|
if( !inp ) { |
183 |
|
|
const char *s = winpt_strerror( WPTERR_FILE_OPEN ); |
184 |
|
|
log_box( _("WinPT Error"), 0, "%s: %s", p, s ); |
185 |
|
|
free_if_alloc( p ); |
186 |
|
|
return NULL; |
187 |
|
|
} |
188 |
|
|
gpg_iobuf_ioctl( inp, 3, 1, NULL ); /* disable cache */ |
189 |
|
|
rc = gpgme_getkey_bykeyid( inp, keyid, r_key ); |
190 |
|
|
|
191 |
|
|
gpg_iobuf_close( inp ); |
192 |
|
|
free_if_alloc( p ); |
193 |
|
|
return rc; |
194 |
|
|
} /* get_key2 */ |
195 |
|
|
|
196 |
|
|
|
197 |
|
|
int |
198 |
|
|
get_pubkey( const char *keyid, gpgme_key_t *ret_key ) |
199 |
|
|
{ |
200 |
|
|
int rc = 0; |
201 |
|
|
|
202 |
|
|
if( pub && sec ) |
203 |
|
|
rc = get_key( keyid, ret_key, 0 ); |
204 |
|
|
else |
205 |
|
|
rc = get_key2( keyid, ret_key, 0 ); |
206 |
|
|
return rc; |
207 |
|
|
} /* get_pubkey */ |
208 |
|
|
|
209 |
|
|
|
210 |
|
|
int |
211 |
|
|
get_seckey( const char *keyid, gpgme_key_t *ret_skey ) |
212 |
|
|
{ |
213 |
|
|
int rc = 0; |
214 |
|
|
|
215 |
|
|
if( pub && sec ) |
216 |
|
|
rc = get_key( keyid, ret_skey, 1 ); |
217 |
|
|
else |
218 |
|
|
rc = get_key2( keyid, ret_skey, 1 ); |
219 |
|
|
return rc; |
220 |
|
|
} /* get_seckey */ |
221 |
|
|
|
222 |
|
|
|
223 |
|
|
int |
224 |
|
|
count_insecure_elgkeys (void) |
225 |
|
|
{ |
226 |
|
|
gpgme_keycache_t pc; |
227 |
|
|
gpgme_key_t key; |
228 |
|
|
int n=0; |
229 |
|
|
|
230 |
|
|
pc = keycache_get_ctx (1); |
231 |
|
|
if (!pc) |
232 |
|
|
BUG (0); |
233 |
|
|
while (!gpgme_keycache_next_key (pc, 0, &key)) { |
234 |
|
|
if (gpgme_key_get_ulong_attr (key, GPGME_ATTR_ALGO, NULL, 0) |
235 |
|
|
== GPGME_PK_ELG_ES) |
236 |
|
|
n++; |
237 |
|
|
} |
238 |
|
|
gpgme_keycache_rewind (pc); |
239 |
|
|
return n; |
240 |
|
|
} |