1 |
/* wptGPGME.cpp - WinPT GPGME interface |
/* wptGPGME.cpp - WinPT GPGME interface |
2 |
* Copyright (C) 2001-2005 Timo Schulz |
* Copyright (C) 2001-2006 Timo Schulz |
3 |
* |
* |
4 |
* This file is part of WinPT. |
* This file is part of WinPT. |
5 |
* |
* |
47 |
static char *gpg_secring = NULL; |
static char *gpg_secring = NULL; |
48 |
|
|
49 |
|
|
50 |
/* Reload the key cache. */ |
int |
51 |
void |
keycache_not_available (void) |
52 |
keycache_reload (HWND dlg) |
{ |
53 |
{ |
return pub == NULL; |
|
refresh_cache_s rcs; |
|
|
|
|
|
memset (&rcs, 0, sizeof rcs); |
|
|
rcs.kr_reload = rcs.kr_update = 1; |
|
|
rcs.tr_update = 0; |
|
|
DialogBoxParam (glob_hinst, (LPCSTR)IDD_WINPT_KEYCACHE, dlg, |
|
|
keycache_dlg_proc, (LPARAM)&rcs); |
|
54 |
} |
} |
55 |
|
|
|
|
|
56 |
/* Release both key cache objects. If @cleanup is 1, |
/* Release both key cache objects. If @cleanup is 1, |
57 |
also release other global structs. */ |
also release other global structs. */ |
58 |
void |
void |
117 |
} |
} |
118 |
|
|
119 |
memset (&pfx, 0, sizeof (pfx)); |
memset (&pfx, 0, sizeof (pfx)); |
120 |
/* Release old contexts first. */ |
keycache_release (0); /* Release old contexts first. */ |
|
keycache_release (0); |
|
121 |
|
|
122 |
err = gpg_keycache_new (&pub); |
err = gpg_keycache_new (&pub); |
123 |
if (err) |
if (err) |
158 |
/* Get the GPG key with keyid @keyid from the cache. Return it |
/* Get the GPG key with keyid @keyid from the cache. Return it |
159 |
in @r_key on success. */ |
in @r_key on success. */ |
160 |
static gpgme_error_t |
static gpgme_error_t |
161 |
get_key_from_cache (const char *keyid, gpgme_key_t *r_key, |
get_key_from_cache (const char *keyid, int secret, gpgme_key_t *r_key, |
162 |
struct keycache_s **c, int secret) |
struct keycache_s **c) |
163 |
{ |
{ |
164 |
gpg_keycache_t cache; |
gpg_keycache_t cache; |
165 |
gpgme_error_t err; |
gpgme_error_t err; |
178 |
} |
} |
179 |
|
|
180 |
|
|
181 |
/* Get GPG key with keyid @keyid directly from GPG and return |
/* Release the internal key structure. |
182 |
it in @r_key on success. */ |
If allocated is 0, assume fixed cache item. */ |
183 |
static gpgme_error_t |
void |
184 |
get_key_directly (const char *keyid, gpgme_key_t *r_key, int secret) |
winpt_release_pubkey (winpt_key_s *k) |
185 |
{ |
{ |
186 |
gpgme_ctx_t ctx; |
/*log_box ("debug", 0, "alloc %d", k->allocated);*/ |
187 |
gpgme_error_t err; |
if (!k->allocated) |
188 |
|
return; |
189 |
err = gpgme_new (&ctx); |
gpg_keycache_item_release (k->ext); |
190 |
if (err) |
k->ext = NULL; |
191 |
return err; |
k->allocated = 0; |
|
err = gpgme_get_key (ctx, keyid, r_key, secret); |
|
|
gpgme_release (ctx); |
|
|
return err; |
|
192 |
} |
} |
193 |
|
|
194 |
|
|
197 |
gpgme_error_t |
gpgme_error_t |
198 |
winpt_get_pubkey (const char *keyid, winpt_key_s *k) |
winpt_get_pubkey (const char *keyid, winpt_key_s *k) |
199 |
{ |
{ |
200 |
gpgme_error_t err; |
gpgme_error_t err = gpg_error (GPG_ERR_NO_ERROR); |
201 |
|
|
202 |
err = get_key_from_cache (keyid, &k->ctx, &k->ext, 0); |
if (pub) |
203 |
|
err = get_key_from_cache (keyid, 0, &k->ctx, &k->ext); |
204 |
|
else |
205 |
|
err = gpg_keycache_fetch_key (keyid, 0, &k->ctx, &k->ext); |
206 |
if (err) |
if (err) |
207 |
return err; |
return err; |
208 |
k->is_v3 = k->ctx->subkeys->pubkey_algo == GPGME_PK_RSA && |
k->is_v3 = k->ctx->subkeys->pubkey_algo == GPGME_PK_RSA && |
210 |
k->is_protected = k->ext->gloflags.is_protected; |
k->is_protected = k->ext->gloflags.is_protected; |
211 |
k->keyid = k->ctx->subkeys->keyid+8; |
k->keyid = k->ctx->subkeys->keyid+8; |
212 |
k->uid = k->ext->uids->uid; |
k->uid = k->ext->uids->uid; |
213 |
|
k->allocated = pub? 0 : 1; |
214 |
return 0; |
return 0; |
215 |
} |
} |
216 |
|
|
220 |
{ |
{ |
221 |
gpgme_error_t err; |
gpgme_error_t err; |
222 |
|
|
223 |
err = get_key_from_cache (keyid, &k->ctx, &k->ext, 1); |
if (sec) |
224 |
|
err = get_key_from_cache (keyid, 1, &k->ctx, &k->ext); |
225 |
|
else |
226 |
|
err = gpg_keycache_fetch_key (keyid, 1, &k->ctx, &k->ext); |
227 |
if (err) |
if (err) |
228 |
return err; |
return err; |
229 |
k->is_v3 = k->ctx->subkeys->pubkey_algo == GPGME_PK_RSA && |
k->is_v3 = k->ctx->subkeys->pubkey_algo == GPGME_PK_RSA && |
231 |
k->is_protected = k->ext->gloflags.is_protected; |
k->is_protected = k->ext->gloflags.is_protected; |
232 |
k->keyid = k->ctx->subkeys->keyid+8; |
k->keyid = k->ctx->subkeys->keyid+8; |
233 |
k->uid = k->ext->uids->uid; |
k->uid = k->ext->uids->uid; |
234 |
|
k->allocated = sec? 0 : 1; |
235 |
return 0; |
return 0; |
236 |
} |
} |
237 |
|
|
239 |
gpgme_error_t |
gpgme_error_t |
240 |
get_pubkey (const char *keyid, gpgme_key_t *ret_key) |
get_pubkey (const char *keyid, gpgme_key_t *ret_key) |
241 |
{ |
{ |
242 |
gpgme_error_t rc; |
return get_key_from_cache (keyid, 0, ret_key, NULL); |
|
|
|
|
if (pub && sec) |
|
|
rc = get_key_from_cache (keyid, ret_key, NULL, 0); |
|
|
else |
|
|
rc = get_key_directly (keyid, ret_key, 0); |
|
|
return rc; |
|
243 |
} |
} |
244 |
|
|
245 |
|
|
246 |
gpgme_error_t |
gpgme_error_t |
247 |
get_seckey (const char *keyid, gpgme_key_t *ret_skey) |
get_seckey (const char *keyid, gpgme_key_t *ret_skey) |
248 |
{ |
{ |
249 |
gpgme_error_t rc; |
return get_key_from_cache (keyid, 1, ret_skey, NULL); |
|
|
|
|
if (pub && sec) |
|
|
rc = get_key_from_cache (keyid, ret_skey, NULL, 1); |
|
|
else |
|
|
rc = get_key_directly (keyid, ret_skey, 1); |
|
|
return rc; |
|
|
} |
|
|
|
|
|
|
|
|
/* Search for insecure ElGamal keys and return the |
|
|
number of founded keys. */ |
|
|
int |
|
|
count_insecure_elgkeys (void) |
|
|
{ |
|
|
gpg_keycache_t pc; |
|
|
gpgme_key_t key; |
|
|
int n=0; |
|
|
|
|
|
pc = keycache_get_ctx (1); |
|
|
while (!gpg_keycache_next_key (pc, 0, &key)) { |
|
|
if (key->subkeys->pubkey_algo == GPGME_PK_ELG) |
|
|
n++; |
|
|
} |
|
|
gpg_keycache_rewind (pc); |
|
|
return n; |
|
250 |
} |
} |
251 |
|
|
252 |
|
|
275 |
get_gpg_sigstat (gpgme_sigsum_t sum) |
get_gpg_sigstat (gpgme_sigsum_t sum) |
276 |
{ |
{ |
277 |
const char *gpg_sigstat[] = { |
const char *gpg_sigstat[] = { |
278 |
_("Error during verification process."), |
_("Error during verification process"), |
279 |
_("The signature is good."), |
_("The signature is good"), |
280 |
_("The signature is BAD!"), |
_("The signature is BAD!"), |
281 |
_("The signature could not be checked due to a missing key."), |
_("The signature could not be checked due to a missing key"), |
282 |
_("No valid OpenPGP signature."), |
_("No valid OpenPGP signature"), |
283 |
_("Signature Error"), |
_("Signature Error"), |
284 |
_("Good Signature (Expired Key)"), |
_("Good Signature (Expired Key)"), |
285 |
_("Good Signature (Revoked Key)"), |
_("Good Signature (Revoked Key)"), |
291 |
} |
} |
292 |
|
|
293 |
|
|
294 |
/* Check if the secret keyring contains at least one |
bool |
295 |
key with ultimate trust. |
secret_key_available (void) |
|
Return value: 0 on success. */ |
|
|
int |
|
|
check_ultimate_trusted_key (void) |
|
296 |
{ |
{ |
297 |
struct keycache_s *n; |
if (!sec || gpg_keycache_get_size (sec) == 0) |
298 |
|
return false; |
299 |
for (n = sec->item; n; n = n->next) { |
return true; |
|
if (n->pubpart && |
|
|
n->pubpart->key->owner_trust == GPGME_VALIDITY_ULTIMATE) |
|
|
return 0; |
|
|
} |
|
|
return -1; |
|
300 |
} |
} |