[PATCH v5 (master)] filter and verify keyserver responses
Stefan Tomanek
tomanek at internet-sicherheit.de
Sun Jan 19 19:45:20 CET 2014
This changes introduces import functions that apply a constraining
filter to imported keys. These filters can verify the fingerprints of
the keys returned before importing them into the keyring, ensuring that
the keys fetched from the keyserver are in fact those selected by the
user beforehand. This prevents malicious keyservers and men in the middle
from creating confusion by planting unrequested keys in the keyrings.
Signed-off-by: Stefan Tomanek <tomanek at internet-sicherheit.de>
---
g10/import.c | 51 ++++++++++++++++++++++++++++++++++-----------------
g10/keyserver.c | 43 ++++++++++++++++++++++++++++++++++++++++---
g10/main.h | 7 +++++--
3 files changed, 79 insertions(+), 22 deletions(-)
diff --git a/g10/import.c b/g10/import.c
index 3846c21..df99174 100644
--- a/g10/import.c
+++ b/g10/import.c
@@ -62,15 +62,18 @@ struct stats_s {
static int import (ctrl_t ctrl,
IOBUF inp, const char* fname, struct stats_s *stats,
- unsigned char **fpr, size_t *fpr_len, unsigned int options);
+ unsigned char **fpr, size_t *fpr_len, unsigned int options,
+ import_filter filter, void *filter_arg);
static int read_block( IOBUF a, PACKET **pending_pkt, KBNODE *ret_root );
static void revocation_present (ctrl_t ctrl, kbnode_t keyblock);
static int import_one (ctrl_t ctrl,
const char *fname, KBNODE keyblock,struct stats_s *stats,
unsigned char **fpr,size_t *fpr_len,
- unsigned int options,int from_sk);
+ unsigned int options,int from_sk,
+ import_filter filter, void *filter_arg);
static int import_secret_one (ctrl_t ctrl, const char *fname, KBNODE keyblock,
- struct stats_s *stats, unsigned int options);
+ struct stats_s *stats, unsigned int options,
+ import_filter filter, void *filter_arg );
static int import_revoke_cert( const char *fname, KBNODE node,
struct stats_s *stats);
static int chk_self_sigs( const char *fname, KBNODE keyblock,
@@ -167,7 +170,8 @@ import_release_stats_handle (void *p)
static int
import_keys_internal (ctrl_t ctrl, iobuf_t inp, char **fnames, int nnames,
void *stats_handle, unsigned char **fpr, size_t *fpr_len,
- unsigned int options )
+ unsigned int options,
+ import_filter filter, void *filter_arg )
{
int i, rc = 0;
struct stats_s *stats = stats_handle;
@@ -176,7 +180,7 @@ import_keys_internal (ctrl_t ctrl, iobuf_t inp, char **fnames, int nnames,
stats = import_new_stats_handle ();
if (inp) {
- rc = import (ctrl, inp, "[stream]", stats, fpr, fpr_len, options);
+ rc = import (ctrl, inp, "[stream]", stats, fpr, fpr_len, options, filter, filter_arg);
}
else {
if( !fnames && !nnames )
@@ -197,7 +201,7 @@ import_keys_internal (ctrl_t ctrl, iobuf_t inp, char **fnames, int nnames,
log_error(_("can't open '%s': %s\n"), fname, strerror(errno) );
else
{
- rc = import (ctrl, inp2, fname, stats, fpr, fpr_len, options);
+ rc = import (ctrl, inp2, fname, stats, fpr, fpr_len, options, NULL, NULL);
iobuf_close(inp2);
/* Must invalidate that ugly cache to actually close it. */
iobuf_ioctl (NULL, IOBUF_IOCTL_INVALIDATE_CACHE,
@@ -232,15 +236,18 @@ import_keys (ctrl_t ctrl, char **fnames, int nnames,
void *stats_handle, unsigned int options )
{
import_keys_internal (ctrl, NULL, fnames, nnames, stats_handle,
- NULL, NULL, options);
+ NULL, NULL, options,
+ NULL, NULL);
}
int
import_keys_stream (ctrl_t ctrl, IOBUF inp, void *stats_handle,
- unsigned char **fpr, size_t *fpr_len,unsigned int options)
+ unsigned char **fpr, size_t *fpr_len,unsigned int options,
+ import_filter filter, void *filter_arg)
{
return import_keys_internal (ctrl, inp, NULL, 0, stats_handle,
- fpr, fpr_len, options);
+ fpr, fpr_len, options,
+ filter, filter_arg);
}
@@ -248,7 +255,8 @@ import_keys_stream (ctrl_t ctrl, IOBUF inp, void *stats_handle,
int
import_keys_es_stream (ctrl_t ctrl, estream_t fp, void *stats_handle,
unsigned char **fpr, size_t *fpr_len,
- unsigned int options)
+ unsigned int options,
+ import_filter filter, void *filter_arg)
{
int rc;
iobuf_t inp;
@@ -262,7 +270,8 @@ import_keys_es_stream (ctrl_t ctrl, estream_t fp, void *stats_handle,
}
rc = import_keys_internal (ctrl, inp, NULL, 0, stats_handle,
- fpr, fpr_len, options);
+ fpr, fpr_len, options,
+ filter, filter_arg);
iobuf_close (inp);
return rc;
@@ -271,7 +280,8 @@ import_keys_es_stream (ctrl_t ctrl, estream_t fp, void *stats_handle,
static int
import (ctrl_t ctrl, IOBUF inp, const char* fname,struct stats_s *stats,
- unsigned char **fpr,size_t *fpr_len,unsigned int options )
+ unsigned char **fpr,size_t *fpr_len,unsigned int options,
+ import_filter filter, void *filter_arg)
{
PACKET *pending_pkt = NULL;
KBNODE keyblock = NULL; /* Need to initialize because gcc can't
@@ -293,9 +303,10 @@ import (ctrl_t ctrl, IOBUF inp, const char* fname,struct stats_s *stats,
while( !(rc = read_block( inp, &pending_pkt, &keyblock) )) {
if( keyblock->pkt->pkttype == PKT_PUBLIC_KEY )
rc = import_one (ctrl, fname, keyblock,
- stats, fpr, fpr_len, options, 0);
+ stats, fpr, fpr_len, options, 0,
+ filter, filter_arg);
else if( keyblock->pkt->pkttype == PKT_SECRET_KEY )
- rc = import_secret_one (ctrl, fname, keyblock, stats, options);
+ rc = import_secret_one (ctrl, fname, keyblock, stats, options, filter, filter_arg);
else if( keyblock->pkt->pkttype == PKT_SIGNATURE
&& keyblock->pkt->pkt.signature->sig_class == 0x20 )
rc = import_revoke_cert( fname, keyblock, stats );
@@ -780,7 +791,7 @@ static int
import_one (ctrl_t ctrl,
const char *fname, KBNODE keyblock, struct stats_s *stats,
unsigned char **fpr,size_t *fpr_len,unsigned int options,
- int from_sk )
+ int from_sk, import_filter filter, void *filter_arg)
{
PKT_public_key *pk;
PKT_public_key *pk_orig;
@@ -823,6 +834,11 @@ import_one (ctrl_t ctrl,
return 0;
}
+ if (filter && filter(pk, filter_arg)) {
+ log_error( _("key %s: rejected by import filter\n"), keystr_from_pk(pk));
+ return 0;
+ }
+
if (opt.interactive) {
if(is_status_enabled())
print_import_check (pk, uidnode->pkt->pkt.user_id);
@@ -1529,7 +1545,8 @@ sec_to_pub_keyblock (kbnode_t sec_keyblock)
*/
static int
import_secret_one (ctrl_t ctrl, const char *fname, KBNODE keyblock,
- struct stats_s *stats, unsigned int options)
+ struct stats_s *stats, unsigned int options,
+ import_filter filter, void *filter_arg )
{
PKT_public_key *pk;
struct seckey_info *ski;
@@ -1611,7 +1628,7 @@ import_secret_one (ctrl_t ctrl, const char *fname, KBNODE keyblock,
public key block, and below we will output another one for
the secret keys. FIXME? */
import_one (ctrl, fname, pub_keyblock, stats,
- NULL, NULL, options, 1);
+ NULL, NULL, options, 1, filter, filter_arg);
/* Fixme: We should check for an invalid keyblock and
cancel the secret key import in this case. */
diff --git a/g10/keyserver.c b/g10/keyserver.c
index 0ec616b..7215c7c 100644
--- a/g10/keyserver.c
+++ b/g10/keyserver.c
@@ -829,6 +829,40 @@ show_prompt (ctrl_t ctrl, KEYDB_SEARCH_DESC *desc, int numdesc,
return err;
}
+static int
+keyserver_retrieval_filter(PKT_public_key *pk, void *arg)
+{
+ KEYDB_SEARCH_DESC *desc = arg;
+ u32 keyid[2];
+ byte fpr[MAX_FINGERPRINT_LEN];
+ size_t fpr_len = 0;
+
+ fingerprint_from_pk(pk, fpr, &fpr_len);
+ keyid_from_pk(pk, keyid);
+
+ /* compare requested and returned fingerprints if available */
+ if (desc->mode == KEYDB_SEARCH_MODE_FPR20) {
+ if (fpr_len != 20 || memcmp(fpr, desc->u.fpr, 20)) {
+ return 1;
+ }
+ }
+ else if (desc->mode == KEYDB_SEARCH_MODE_FPR16) {
+ if (fpr_len != 16 || memcmp(fpr, desc->u.fpr, 16)) {
+ return 1;
+ }
+ }
+ else if (desc->mode == KEYDB_SEARCH_MODE_LONG_KID) {
+ if (memcmp(keyid, desc->u.kid, sizeof(keyid))) {
+ return 1;
+ }
+ }
+ else if (desc->mode == KEYDB_SEARCH_MODE_SHORT_KID) {
+ if (memcmp(&keyid[1], &desc->u.kid[1], 1)) {
+ return 1;
+ }
+ }
+ return 0;
+}
/* This is a callback used by call-dirmngr.c to process the result of
KS_SEARCH command. LINE is the actual data line received with all
@@ -1581,7 +1615,8 @@ keyserver_get (ctrl_t ctrl, KEYDB_SEARCH_DESC *desc, int ndesc,
a temp iobuf for each key. */
import_keys_es_stream (ctrl, datastream, stats_handle, NULL, NULL,
- opt.keyserver_options.import_options);
+ opt.keyserver_options.import_options,
+ &keyserver_retrieval_filter, desc);
import_print_stats (stats_handle);
import_release_stats_handle (stats_handle);
@@ -1672,7 +1707,8 @@ keyserver_fetch (ctrl_t ctrl, strlist_t urilist)
stats_handle = import_new_stats_handle();
import_keys_es_stream (ctrl, datastream, stats_handle, NULL, NULL,
- opt.keyserver_options.import_options);
+ opt.keyserver_options.import_options,
+ NULL, NULL);
import_print_stats (stats_handle);
import_release_stats_handle (stats_handle);
@@ -1721,7 +1757,8 @@ keyserver_import_cert (ctrl_t ctrl,
opt.no_armor=1;
err = import_keys_es_stream (ctrl, key, NULL, fpr, fpr_len,
- opt.keyserver_options.import_options);
+ opt.keyserver_options.import_options,
+ NULL, NULL);
opt.no_armor=armor_status;
diff --git a/g10/main.h b/g10/main.h
index 1b619e0..90e6caa 100644
--- a/g10/main.h
+++ b/g10/main.h
@@ -271,15 +271,18 @@ gcry_mpi_t encode_md_value (PKT_public_key *pk,
gcry_md_hd_t md, int hash_algo );
/*-- import.c --*/
+typedef int (*import_filter)(PKT_public_key *pk, void *arg);
int parse_import_options(char *str,unsigned int *options,int noisy);
void import_keys (ctrl_t ctrl, char **fnames, int nnames,
void *stats_hd, unsigned int options);
int import_keys_stream (ctrl_t ctrl, iobuf_t inp, void *stats_hd,
unsigned char **fpr,
- size_t *fpr_len, unsigned int options);
+ size_t *fpr_len, unsigned int options,
+ import_filter constr, void *filter_arg);
int import_keys_es_stream (ctrl_t ctrl, estream_t fp, void *stats_handle,
unsigned char **fpr, size_t *fpr_len,
- unsigned int options);
+ unsigned int options,
+ import_filter constr, void *filter_arg);
void *import_new_stats_handle (void);
void import_release_stats_handle (void *p);
void import_print_stats (void *hd);
--
1.7.10.4
More information about the Gnupg-devel
mailing list