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