1998-01-02 21:40:10 +01:00
|
|
|
/* trustdb.c
|
1998-02-24 19:50:46 +01:00
|
|
|
* Copyright (C) 1998 Free Software Foundation, Inc.
|
1998-01-02 21:40:10 +01:00
|
|
|
*
|
1998-02-24 19:50:46 +01:00
|
|
|
* This file is part of GNUPG.
|
1998-01-02 21:40:10 +01:00
|
|
|
*
|
1998-02-24 19:50:46 +01:00
|
|
|
* GNUPG is free software; you can redistribute it and/or modify
|
1998-01-02 21:40:10 +01:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
1998-02-24 19:50:46 +01:00
|
|
|
* GNUPG is distributed in the hope that it will be useful,
|
1998-01-02 21:40:10 +01:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <assert.h>
|
1998-02-17 21:48:52 +01:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
1998-01-13 20:04:23 +01:00
|
|
|
#include <fcntl.h>
|
1998-02-17 21:48:52 +01:00
|
|
|
#include <unistd.h>
|
1998-01-02 21:40:10 +01:00
|
|
|
|
|
|
|
#include "errors.h"
|
|
|
|
#include "iobuf.h"
|
|
|
|
#include "keydb.h"
|
|
|
|
#include "memory.h"
|
|
|
|
#include "util.h"
|
1998-01-12 11:18:17 +01:00
|
|
|
#include "trustdb.h"
|
|
|
|
#include "options.h"
|
1998-01-16 22:15:24 +01:00
|
|
|
#include "packet.h"
|
|
|
|
#include "main.h"
|
1998-01-02 21:40:10 +01:00
|
|
|
|
|
|
|
|
1998-01-12 11:18:17 +01:00
|
|
|
#define TRUST_RECORD_LEN 40
|
1998-01-31 22:24:36 +01:00
|
|
|
#define SIGS_PER_RECORD ((TRUST_RECORD_LEN-10)/5)
|
|
|
|
#define ITEMS_PER_HTBL_RECORD ((TRUST_RECORD_LEN-2)/4)
|
|
|
|
#define ITEMS_PER_HLST_RECORD ((TRUST_RECORD_LEN-6)/5)
|
1998-01-16 22:15:24 +01:00
|
|
|
#define MAX_LIST_SIGS_DEPTH 20
|
1998-01-12 11:18:17 +01:00
|
|
|
|
1998-01-30 21:25:31 +01:00
|
|
|
|
|
|
|
#define RECTYPE_VER 1
|
|
|
|
#define RECTYPE_DIR 2
|
|
|
|
#define RECTYPE_KEY 3
|
|
|
|
#define RECTYPE_CTL 4
|
|
|
|
#define RECTYPE_SIG 5
|
1998-01-31 22:24:36 +01:00
|
|
|
#define RECTYPE_HTBL 6
|
|
|
|
#define RECTYPE_HLST 7
|
1998-01-30 21:25:31 +01:00
|
|
|
|
|
|
|
|
1998-01-12 11:18:17 +01:00
|
|
|
struct trust_record {
|
1998-01-30 21:25:31 +01:00
|
|
|
int rectype;
|
1998-01-12 11:18:17 +01:00
|
|
|
union {
|
|
|
|
struct { /* version record: */
|
|
|
|
byte version; /* should be 1 */
|
1998-01-16 22:15:24 +01:00
|
|
|
ulong locked; /* pid of process which holds a lock */
|
|
|
|
ulong created; /* timestamp of trustdb creation */
|
|
|
|
ulong modified; /* timestamp of last modification */
|
|
|
|
ulong validated; /* timestamp of last validation */
|
1998-01-12 11:18:17 +01:00
|
|
|
byte marginals_needed;
|
|
|
|
byte completes_needed;
|
|
|
|
byte max_cert_depth;
|
1998-01-30 21:25:31 +01:00
|
|
|
} ver;
|
|
|
|
struct { /* directory record */
|
1998-01-16 22:15:24 +01:00
|
|
|
ulong local_id;
|
1998-01-30 21:25:31 +01:00
|
|
|
u32 keyid[2];
|
|
|
|
ulong keyrec; /* recno of public key record */
|
1998-01-31 22:24:36 +01:00
|
|
|
ulong ctlrec; /* recno of control record */
|
1998-01-30 21:25:31 +01:00
|
|
|
ulong sigrec; /* recno of first signature record */
|
1998-01-31 22:24:36 +01:00
|
|
|
ulong link; /* to next dir record */
|
1998-01-30 21:25:31 +01:00
|
|
|
byte no_sigs; /* does not have sigature and checked */
|
|
|
|
} dir;
|
|
|
|
struct { /* public key record */
|
|
|
|
ulong owner;
|
|
|
|
u32 keyid[2];
|
1998-01-16 22:15:24 +01:00
|
|
|
byte pubkey_algo;
|
1998-01-12 11:18:17 +01:00
|
|
|
byte fingerprint[20];
|
|
|
|
byte ownertrust;
|
1998-01-30 21:25:31 +01:00
|
|
|
} key;
|
|
|
|
struct { /* control record */
|
1998-01-16 22:15:24 +01:00
|
|
|
ulong owner;
|
1998-01-12 11:18:17 +01:00
|
|
|
byte blockhash[20];
|
1998-01-30 21:25:31 +01:00
|
|
|
byte trustlevel; /* calculated trustlevel */
|
|
|
|
} ctl;
|
|
|
|
struct { /* signature record */
|
1998-01-16 22:15:24 +01:00
|
|
|
ulong owner; /* local_id of record owner (pubkey record) */
|
|
|
|
ulong chain; /* offset of next record or NULL for last one */
|
1998-01-14 13:34:05 +01:00
|
|
|
struct {
|
1998-01-16 22:15:24 +01:00
|
|
|
ulong local_id; /* of pubkey record of signator (0=unused) */
|
1998-01-14 13:34:05 +01:00
|
|
|
byte flag; /* reserved */
|
|
|
|
} sig[SIGS_PER_RECORD];
|
1998-01-30 21:25:31 +01:00
|
|
|
} sig;
|
1998-01-31 22:24:36 +01:00
|
|
|
struct {
|
|
|
|
ulong item[ITEMS_PER_HTBL_RECORD];
|
|
|
|
} htbl;
|
|
|
|
struct {
|
|
|
|
ulong chain;
|
|
|
|
struct {
|
|
|
|
byte hash;
|
|
|
|
ulong rnum;
|
|
|
|
} item[ITEMS_PER_HLST_RECORD];
|
|
|
|
} hlst;
|
1998-01-12 11:18:17 +01:00
|
|
|
} r;
|
|
|
|
};
|
1998-01-13 20:04:23 +01:00
|
|
|
typedef struct trust_record TRUSTREC;
|
1998-01-12 11:18:17 +01:00
|
|
|
|
1998-01-16 22:15:24 +01:00
|
|
|
typedef struct {
|
1998-01-30 21:25:31 +01:00
|
|
|
ulong local_id; /* localid of the pubkey */
|
1998-01-31 22:24:36 +01:00
|
|
|
ulong sigrec;
|
1998-01-16 22:15:24 +01:00
|
|
|
ulong sig_id; /* returned signature id */
|
1998-01-25 19:56:33 +01:00
|
|
|
unsigned sig_flag; /* returned signature record flag */
|
1998-01-16 22:15:24 +01:00
|
|
|
struct { /* internal data */
|
1998-01-31 22:24:36 +01:00
|
|
|
int init_done;
|
1998-01-16 22:15:24 +01:00
|
|
|
int eof;
|
|
|
|
TRUSTREC rec;
|
|
|
|
int index;
|
|
|
|
} ctl;
|
|
|
|
} SIGREC_CONTEXT;
|
|
|
|
|
|
|
|
typedef struct local_id_info *LOCAL_ID_INFO;
|
|
|
|
struct local_id_info {
|
|
|
|
LOCAL_ID_INFO next;
|
|
|
|
ulong lid;
|
|
|
|
unsigned flag;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
1998-01-24 17:32:27 +01:00
|
|
|
typedef struct trust_info TRUST_INFO;
|
|
|
|
struct trust_info {
|
|
|
|
ulong lid;
|
|
|
|
unsigned trust;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
1998-01-19 19:54:44 +01:00
|
|
|
typedef struct trust_seg_list *TRUST_SEG_LIST;
|
|
|
|
struct trust_seg_list {
|
|
|
|
TRUST_SEG_LIST next;
|
|
|
|
int nseg; /* number of segmens */
|
|
|
|
int dup;
|
1998-01-24 17:32:27 +01:00
|
|
|
TRUST_INFO seg[1]; /* segment list */
|
1998-01-19 19:54:44 +01:00
|
|
|
};
|
|
|
|
|
1998-01-12 11:18:17 +01:00
|
|
|
|
1998-01-24 17:32:27 +01:00
|
|
|
typedef struct {
|
|
|
|
TRUST_SEG_LIST tsl;
|
|
|
|
int index;
|
|
|
|
} ENUM_TRUST_WEB_CONTEXT;
|
|
|
|
|
|
|
|
|
1998-01-13 20:04:23 +01:00
|
|
|
static void create_db( const char *fname );
|
|
|
|
static void open_db(void);
|
1998-01-30 21:25:31 +01:00
|
|
|
static void dump_record( ulong rnum, TRUSTREC *rec, FILE *fp );
|
1998-01-31 22:24:36 +01:00
|
|
|
static int read_record( ulong recnum, TRUSTREC *rec, int expected );
|
1998-01-16 22:15:24 +01:00
|
|
|
static int write_record( ulong recnum, TRUSTREC *rec );
|
|
|
|
static ulong new_recnum(void);
|
1998-01-30 21:25:31 +01:00
|
|
|
static int search_record( PKT_public_cert *pkc, TRUSTREC *rec );
|
1998-01-25 19:56:33 +01:00
|
|
|
static int walk_sigrecs( SIGREC_CONTEXT *c, int create );
|
1998-01-16 22:15:24 +01:00
|
|
|
|
|
|
|
static LOCAL_ID_INFO *new_lid_table(void);
|
|
|
|
static void release_lid_table( LOCAL_ID_INFO *tbl );
|
1998-01-19 19:54:44 +01:00
|
|
|
static int ins_lid_table_item( LOCAL_ID_INFO *tbl, ulong lid, unsigned flag );
|
|
|
|
static int qry_lid_table_flag( LOCAL_ID_INFO *tbl, ulong lid, unsigned *flag );
|
|
|
|
static void upd_lid_table_flag( LOCAL_ID_INFO *tbl, ulong lid, unsigned flag );
|
1998-01-16 22:15:24 +01:00
|
|
|
|
|
|
|
static void print_user_id( const char *text, u32 *keyid );
|
1998-01-24 17:32:27 +01:00
|
|
|
static int do_list_path( TRUST_INFO *stack, int depth, int max_depth,
|
1998-01-19 19:54:44 +01:00
|
|
|
LOCAL_ID_INFO *lids, TRUST_SEG_LIST *tslist );
|
1998-01-16 22:15:24 +01:00
|
|
|
|
|
|
|
static int list_sigs( ulong pubkey_id );
|
1998-01-31 22:24:36 +01:00
|
|
|
static int build_sigrecs( ulong pubkeyid );
|
1998-01-24 17:32:27 +01:00
|
|
|
static int propagate_trust( TRUST_SEG_LIST tslist );
|
1998-01-31 22:24:36 +01:00
|
|
|
static int do_check( ulong pubkeyid, TRUSTREC *drec, unsigned *trustlevel );
|
1998-01-16 22:15:24 +01:00
|
|
|
|
1998-01-25 19:56:33 +01:00
|
|
|
static int update_no_sigs( ulong lid, int no_sigs );
|
1998-01-12 11:18:17 +01:00
|
|
|
|
|
|
|
static char *db_name;
|
1998-01-13 20:04:23 +01:00
|
|
|
static int db_fd = -1;
|
1998-01-16 22:15:24 +01:00
|
|
|
/* a table used to keep track of ultimately trusted keys
|
|
|
|
* which are the ones from our secrings */
|
|
|
|
static LOCAL_ID_INFO *ultikey_table;
|
1998-01-13 20:04:23 +01:00
|
|
|
|
1998-01-24 17:32:27 +01:00
|
|
|
static ulong last_trust_web_key;
|
|
|
|
static TRUST_SEG_LIST last_trust_web_tslist;
|
|
|
|
|
1998-01-16 22:15:24 +01:00
|
|
|
#define buftoulong( p ) ((*(byte*)(p) << 24) | (*((byte*)(p)+1)<< 16) | \
|
1998-01-13 20:04:23 +01:00
|
|
|
(*((byte*)(p)+2) << 8) | (*((byte*)(p)+3)))
|
1998-01-16 22:15:24 +01:00
|
|
|
#define buftoushort( p ) ((*((byte*)(p)) << 8) | (*((byte*)(p)+1)))
|
|
|
|
#define ulongtobuf( p, a ) do { \
|
1998-01-13 20:04:23 +01:00
|
|
|
((byte*)p)[0] = a >> 24; \
|
|
|
|
((byte*)p)[1] = a >> 16; \
|
|
|
|
((byte*)p)[2] = a >> 8; \
|
|
|
|
((byte*)p)[3] = a ; \
|
|
|
|
} while(0)
|
1998-01-16 22:15:24 +01:00
|
|
|
#define ushorttobuf( p, a ) do { \
|
1998-01-13 20:04:23 +01:00
|
|
|
((byte*)p)[0] = a >> 8; \
|
|
|
|
((byte*)p)[1] = a ; \
|
|
|
|
} while(0)
|
1998-01-16 22:15:24 +01:00
|
|
|
#define buftou32( p) buftoulong( (p) )
|
|
|
|
#define u32tobuf( p, a) ulongtobuf( (p), (a) )
|
1998-01-13 20:04:23 +01:00
|
|
|
|
1998-01-12 11:18:17 +01:00
|
|
|
|
|
|
|
/**************************************************
|
|
|
|
************** read and write helpers ************
|
|
|
|
**************************************************/
|
|
|
|
|
|
|
|
static void
|
|
|
|
fwrite_8(FILE *fp, byte a)
|
|
|
|
{
|
|
|
|
if( putc( a & 0xff, fp ) == EOF )
|
|
|
|
log_fatal("error writing byte to trustdb: %s\n", strerror(errno) );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-01-16 22:15:24 +01:00
|
|
|
static void
|
|
|
|
fwrite_32( FILE*fp, ulong a)
|
1998-01-12 11:18:17 +01:00
|
|
|
{
|
|
|
|
putc( (a>>24) & 0xff, fp );
|
|
|
|
putc( (a>>16) & 0xff, fp );
|
|
|
|
putc( (a>> 8) & 0xff, fp );
|
|
|
|
if( putc( a & 0xff, fp ) == EOF )
|
1998-01-16 22:15:24 +01:00
|
|
|
log_fatal("error writing ulong to trustdb: %s\n", strerror(errno) );
|
1998-01-12 11:18:17 +01:00
|
|
|
}
|
|
|
|
|
1998-01-16 22:15:24 +01:00
|
|
|
static void
|
1998-01-12 11:18:17 +01:00
|
|
|
fwrite_zeros( FILE *fp, size_t n)
|
|
|
|
{
|
|
|
|
while( n-- )
|
|
|
|
if( putc( 0, fp ) == EOF )
|
|
|
|
log_fatal("error writing zeros to trustdb: %s\n", strerror(errno) );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-01-30 21:25:31 +01:00
|
|
|
/**********************************************
|
|
|
|
************* list helpers *******************
|
|
|
|
**********************************************/
|
|
|
|
|
|
|
|
static LOCAL_ID_INFO *
|
|
|
|
new_lid_table(void)
|
|
|
|
{
|
|
|
|
return m_alloc_clear( 16 * sizeof(LOCAL_ID_INFO));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
release_lid_table( LOCAL_ID_INFO *tbl )
|
|
|
|
{
|
|
|
|
LOCAL_ID_INFO a, a2;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for(i=0; i < 16; i++ ) {
|
|
|
|
for(a=tbl[i]; a; a = a2 ) {
|
|
|
|
a2 = a->next;
|
|
|
|
m_free(a);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m_free(tbl);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************
|
|
|
|
* Add a new item to the table or return 1 if we already have this item
|
|
|
|
* fixme: maybe its a good idea to take items from an unused item list.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
ins_lid_table_item( LOCAL_ID_INFO *tbl, ulong lid, unsigned flag )
|
|
|
|
{
|
|
|
|
LOCAL_ID_INFO a;
|
|
|
|
|
|
|
|
for( a = tbl[lid & 0x0f]; a; a = a->next )
|
|
|
|
if( a->lid == lid )
|
|
|
|
return 1;
|
|
|
|
a = m_alloc( sizeof *a );
|
|
|
|
a->lid = lid;
|
|
|
|
a->flag = flag;
|
|
|
|
a->next = tbl[lid & 0x0f];
|
|
|
|
tbl[lid & 0x0f] = a;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
qry_lid_table_flag( LOCAL_ID_INFO *tbl, ulong lid, unsigned *flag )
|
|
|
|
{
|
|
|
|
LOCAL_ID_INFO a;
|
|
|
|
|
|
|
|
for( a = tbl[lid & 0x0f]; a; a = a->next )
|
|
|
|
if( a->lid == lid ) {
|
|
|
|
if( flag )
|
|
|
|
*flag = a->flag;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
upd_lid_table_flag( LOCAL_ID_INFO *tbl, ulong lid, unsigned flag )
|
|
|
|
{
|
|
|
|
LOCAL_ID_INFO a;
|
|
|
|
|
|
|
|
for( a = tbl[lid & 0x0f]; a; a = a->next )
|
|
|
|
if( a->lid == lid ) {
|
|
|
|
a->flag = flag;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
BUG();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-01-12 11:18:17 +01:00
|
|
|
/**************************************************
|
|
|
|
************** read and write stuff **************
|
|
|
|
**************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
/****************
|
|
|
|
* Create a new trustdb
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
create_db( const char *fname )
|
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
fp =fopen( fname, "w" );
|
|
|
|
if( !fp )
|
|
|
|
log_fatal("can't create %s: %s\n", fname, strerror(errno) );
|
|
|
|
fwrite_8( fp, 1 );
|
|
|
|
fwrite_8( fp, 'g' );
|
1998-02-24 19:50:46 +01:00
|
|
|
fwrite_8( fp, 'p' );
|
|
|
|
fwrite_8( fp, 'g' );
|
1998-01-12 11:18:17 +01:00
|
|
|
fwrite_8( fp, 1 ); /* version */
|
|
|
|
fwrite_zeros( fp, 3 ); /* reserved */
|
|
|
|
fwrite_32( fp, 0 ); /* not locked */
|
|
|
|
fwrite_32( fp, make_timestamp() ); /* created */
|
|
|
|
fwrite_32( fp, 0 ); /* not yet modified */
|
|
|
|
fwrite_32( fp, 0 ); /* not yet validated*/
|
1998-01-30 21:25:31 +01:00
|
|
|
fwrite_32( fp, 0 ); /* reserved */
|
1998-01-12 11:18:17 +01:00
|
|
|
fwrite_8( fp, 3 ); /* marginals needed */
|
|
|
|
fwrite_8( fp, 1 ); /* completes needed */
|
|
|
|
fwrite_8( fp, 4 ); /* max_cet_depth */
|
|
|
|
fwrite_zeros( fp, 9 ); /* filler */
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
|
1998-01-13 20:04:23 +01:00
|
|
|
static void
|
|
|
|
open_db()
|
|
|
|
{
|
|
|
|
TRUSTREC rec;
|
|
|
|
assert( db_fd == -1 );
|
|
|
|
|
|
|
|
db_fd = open( db_name, O_RDWR );
|
|
|
|
if( db_fd == -1 )
|
|
|
|
log_fatal("can't open %s: %s\n", db_name, strerror(errno) );
|
1998-01-31 22:24:36 +01:00
|
|
|
if( read_record( 0, &rec, RECTYPE_VER ) )
|
1998-01-13 20:04:23 +01:00
|
|
|
log_fatal("TrustDB %s is invalid\n", db_name );
|
|
|
|
/* fixme: check ->locked and other stuff */
|
|
|
|
}
|
1998-01-12 11:18:17 +01:00
|
|
|
|
|
|
|
|
1998-01-30 21:25:31 +01:00
|
|
|
static void
|
|
|
|
dump_record( ulong rnum, TRUSTREC *rec, FILE *fp )
|
|
|
|
{
|
|
|
|
int i, any;
|
|
|
|
|
|
|
|
fprintf(fp, "trust record %lu, type=", rnum );
|
|
|
|
|
|
|
|
switch( rec->rectype ) {
|
|
|
|
case 0: fprintf(fp, "free\n");
|
|
|
|
break;
|
|
|
|
case RECTYPE_VER: fprintf(fp, "version\n");
|
|
|
|
break;
|
|
|
|
case RECTYPE_DIR:
|
1998-02-03 13:09:20 +01:00
|
|
|
fprintf(fp, "dir keyid=%08lX, key=%lu, ctl=%lu, sig=%lu",
|
|
|
|
(ulong)rec->r.dir.keyid[1],
|
1998-02-02 15:36:06 +01:00
|
|
|
rec->r.dir.keyrec, rec->r.dir.ctlrec, rec->r.dir.sigrec );
|
|
|
|
if( rec->r.dir.no_sigs == 1 )
|
|
|
|
fputs(", (none)", fp );
|
|
|
|
else if( rec->r.dir.no_sigs == 2 )
|
|
|
|
fputs(", (invalid)", fp );
|
1998-02-18 14:58:46 +01:00
|
|
|
else if( rec->r.dir.no_sigs == 3 )
|
1998-02-02 15:36:06 +01:00
|
|
|
fputs(", (revoked)", fp );
|
1998-02-18 14:58:46 +01:00
|
|
|
else if( rec->r.dir.no_sigs )
|
|
|
|
fputs(", (??)", fp );
|
1998-02-02 15:36:06 +01:00
|
|
|
putc('\n', fp);
|
1998-01-30 21:25:31 +01:00
|
|
|
break;
|
1998-02-03 13:09:20 +01:00
|
|
|
case RECTYPE_KEY: fprintf(fp, "key keyid=%08lX, own=%lu, ownertrust=%02x\n",
|
|
|
|
(ulong)rec->r.key.keyid[1],
|
1998-01-30 21:25:31 +01:00
|
|
|
rec->r.key.owner, rec->r.key.ownertrust );
|
|
|
|
break;
|
|
|
|
case RECTYPE_CTL: fprintf(fp, "ctl\n");
|
|
|
|
break;
|
|
|
|
case RECTYPE_SIG:
|
1998-01-31 22:24:36 +01:00
|
|
|
fprintf(fp, "sigrec, owner=%lu, chain=%lu\n",
|
|
|
|
rec->r.sig.owner, rec->r.sig.chain );
|
1998-01-30 21:25:31 +01:00
|
|
|
for(i=any=0; i < SIGS_PER_RECORD; i++ ) {
|
1998-01-31 22:24:36 +01:00
|
|
|
if( rec->r.sig.sig[i].local_id ) {
|
1998-01-30 21:25:31 +01:00
|
|
|
if( !any ) {
|
|
|
|
putc('\t', fp);
|
|
|
|
any++;
|
|
|
|
}
|
1998-01-31 22:24:36 +01:00
|
|
|
fprintf(fp, " %lu:%02x", rec->r.sig.sig[i].local_id,
|
|
|
|
rec->r.sig.sig[i].flag );
|
1998-01-30 21:25:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if( any )
|
|
|
|
putc('\n', fp);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fprintf(fp, "%d (unknown)\n", rec->rectype );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-01-13 20:04:23 +01:00
|
|
|
/****************
|
|
|
|
* read the record with number recnum
|
|
|
|
* returns: -1 on error, 0 on success
|
|
|
|
*/
|
|
|
|
static int
|
1998-01-31 22:24:36 +01:00
|
|
|
read_record( ulong recnum, TRUSTREC *rec, int expected )
|
1998-01-13 20:04:23 +01:00
|
|
|
{
|
|
|
|
byte buf[TRUST_RECORD_LEN], *p;
|
|
|
|
int rc = 0;
|
1998-01-14 13:34:05 +01:00
|
|
|
int n, i;
|
1998-01-13 20:04:23 +01:00
|
|
|
|
|
|
|
if( db_fd == -1 )
|
|
|
|
open_db();
|
|
|
|
if( lseek( db_fd, recnum * TRUST_RECORD_LEN, SEEK_SET ) == -1 ) {
|
|
|
|
log_error("trustdb: lseek failed: %s\n", strerror(errno) );
|
|
|
|
return G10ERR_READ_FILE;
|
|
|
|
}
|
|
|
|
n = read( db_fd, buf, TRUST_RECORD_LEN);
|
|
|
|
if( !n ) {
|
|
|
|
return -1; /* eof */
|
|
|
|
}
|
|
|
|
else if( n != TRUST_RECORD_LEN ) {
|
|
|
|
log_error("trustdb: read failed (n=%d): %s\n", n, strerror(errno) );
|
|
|
|
return G10ERR_READ_FILE;
|
|
|
|
}
|
|
|
|
p = buf;
|
|
|
|
rec->rectype = *p++;
|
1998-01-31 22:24:36 +01:00
|
|
|
if( expected && rec->rectype != expected ) {
|
|
|
|
log_error("%lu: read expected rec type %d, got %d\n",
|
|
|
|
recnum, expected, rec->rectype );
|
|
|
|
return G10ERR_TRUSTDB;
|
|
|
|
}
|
1998-01-30 21:25:31 +01:00
|
|
|
p++;
|
1998-01-13 20:04:23 +01:00
|
|
|
switch( rec->rectype ) {
|
|
|
|
case 0: /* unused record */
|
|
|
|
break;
|
1998-01-30 21:25:31 +01:00
|
|
|
case RECTYPE_VER: /* version record */
|
1998-02-24 19:50:46 +01:00
|
|
|
/* g10 was the original name */
|
|
|
|
if( memcmp(buf+1, "gpg", 3 ) && memcmp(buf+1, "g10", 3 ) ) {
|
1998-01-30 21:25:31 +01:00
|
|
|
log_error("%s: not a trustdb file\n", db_name );
|
|
|
|
rc = G10ERR_TRUSTDB;
|
|
|
|
}
|
|
|
|
p += 2; /* skip magic */
|
|
|
|
rec->r.ver.version = *p++;
|
|
|
|
rec->r.ver.locked = buftoulong(p); p += 4;
|
|
|
|
rec->r.ver.created = buftoulong(p); p += 4;
|
|
|
|
rec->r.ver.modified = buftoulong(p); p += 4;
|
|
|
|
rec->r.ver.validated= buftoulong(p); p += 4;
|
|
|
|
rec->r.ver.marginals_needed = *p++;
|
|
|
|
rec->r.ver.completes_needed = *p++;
|
|
|
|
rec->r.ver.max_cert_depth = *p++;
|
1998-01-13 20:04:23 +01:00
|
|
|
if( recnum ) {
|
|
|
|
log_error("%s: version record with recnum %lu\n",
|
|
|
|
db_name, (ulong)recnum );
|
|
|
|
rc = G10ERR_TRUSTDB;
|
|
|
|
}
|
1998-01-30 21:25:31 +01:00
|
|
|
if( rec->r.ver.version != 1 ) {
|
1998-01-13 20:04:23 +01:00
|
|
|
log_error("%s: invalid file version %d\n",
|
1998-01-31 22:24:36 +01:00
|
|
|
db_name, rec->r.ver.version );
|
1998-01-13 20:04:23 +01:00
|
|
|
rc = G10ERR_TRUSTDB;
|
|
|
|
}
|
|
|
|
break;
|
1998-01-30 21:25:31 +01:00
|
|
|
case RECTYPE_DIR: /*directory record */
|
|
|
|
rec->r.dir.local_id = buftoulong(p); p += 4;
|
|
|
|
rec->r.dir.keyid[0] = buftou32(p); p += 4;
|
|
|
|
rec->r.dir.keyid[1] = buftou32(p); p += 4;
|
|
|
|
rec->r.dir.keyrec = buftoulong(p); p += 4;
|
|
|
|
rec->r.dir.ctlrec = buftoulong(p); p += 4;
|
|
|
|
rec->r.dir.sigrec = buftoulong(p); p += 4;
|
|
|
|
rec->r.dir.no_sigs = *p++;
|
|
|
|
if( rec->r.dir.local_id != recnum ) {
|
|
|
|
log_error("%s: dir local_id != recnum (%lu,%lu)\n",
|
1998-01-13 20:04:23 +01:00
|
|
|
db_name,
|
1998-01-30 21:25:31 +01:00
|
|
|
(ulong)rec->r.dir.local_id,
|
1998-01-13 20:04:23 +01:00
|
|
|
(ulong)recnum );
|
|
|
|
rc = G10ERR_TRUSTDB;
|
|
|
|
}
|
|
|
|
break;
|
1998-01-30 21:25:31 +01:00
|
|
|
case RECTYPE_KEY: /* public key record */
|
|
|
|
rec->r.key.owner = buftoulong(p); p += 4;
|
|
|
|
rec->r.dir.keyid[0] = buftou32(p); p += 4;
|
|
|
|
rec->r.dir.keyid[1] = buftou32(p); p += 4;
|
|
|
|
rec->r.key.pubkey_algo = *p++; p++;
|
1998-01-31 22:24:36 +01:00
|
|
|
memcpy( rec->r.key.fingerprint, p, 20); p += 20;
|
|
|
|
rec->r.key.ownertrust = *p++;
|
1998-01-30 21:25:31 +01:00
|
|
|
break;
|
|
|
|
case RECTYPE_CTL: /* control record */
|
|
|
|
rec->r.ctl.owner = buftoulong(p); p += 4;
|
|
|
|
memcpy(rec->r.ctl.blockhash, p, 20); p += 20;
|
|
|
|
rec->r.ctl.trustlevel = *p++;
|
1998-01-13 20:04:23 +01:00
|
|
|
break;
|
1998-01-30 21:25:31 +01:00
|
|
|
case RECTYPE_SIG:
|
|
|
|
rec->r.sig.owner = buftoulong(p); p += 4;
|
|
|
|
rec->r.sig.chain = buftoulong(p); p += 4;
|
1998-01-14 13:34:05 +01:00
|
|
|
for(i=0; i < SIGS_PER_RECORD; i++ ) {
|
1998-01-30 21:25:31 +01:00
|
|
|
rec->r.sig.sig[i].local_id = buftoulong(p); p += 4;
|
|
|
|
rec->r.sig.sig[i].flag = *p++;
|
1998-01-14 13:34:05 +01:00
|
|
|
}
|
|
|
|
break;
|
1998-01-13 20:04:23 +01:00
|
|
|
default:
|
|
|
|
log_error("%s: invalid record type %d at recnum %lu\n",
|
|
|
|
db_name, rec->rectype, (ulong)recnum );
|
|
|
|
rc = G10ERR_TRUSTDB;
|
|
|
|
break;
|
|
|
|
}
|
1998-01-12 11:18:17 +01:00
|
|
|
|
1998-01-13 20:04:23 +01:00
|
|
|
return rc;
|
|
|
|
}
|
1998-01-12 11:18:17 +01:00
|
|
|
|
1998-01-13 20:04:23 +01:00
|
|
|
/****************
|
|
|
|
* Write the record at RECNUM
|
|
|
|
*/
|
|
|
|
static int
|
1998-01-16 22:15:24 +01:00
|
|
|
write_record( ulong recnum, TRUSTREC *rec )
|
1998-01-13 20:04:23 +01:00
|
|
|
{
|
|
|
|
byte buf[TRUST_RECORD_LEN], *p;
|
|
|
|
int rc = 0;
|
1998-01-14 13:34:05 +01:00
|
|
|
int i, n;
|
1998-01-13 20:04:23 +01:00
|
|
|
|
|
|
|
if( db_fd == -1 )
|
|
|
|
open_db();
|
|
|
|
|
|
|
|
memset(buf, 0, TRUST_RECORD_LEN);
|
|
|
|
p = buf;
|
1998-01-31 22:24:36 +01:00
|
|
|
*p++ = rec->rectype; p++;
|
1998-01-13 20:04:23 +01:00
|
|
|
switch( rec->rectype ) {
|
|
|
|
case 0: /* unused record */
|
|
|
|
break;
|
|
|
|
case 1: /* version record */
|
1998-01-16 22:15:24 +01:00
|
|
|
BUG();
|
1998-01-13 20:04:23 +01:00
|
|
|
break;
|
1998-01-30 21:25:31 +01:00
|
|
|
|
|
|
|
case RECTYPE_DIR: /*directory record */
|
|
|
|
ulongtobuf(p, rec->r.dir.local_id); p += 4;
|
|
|
|
u32tobuf(p, rec->r.key.keyid[0]); p += 4;
|
|
|
|
u32tobuf(p, rec->r.key.keyid[1]); p += 4;
|
|
|
|
ulongtobuf(p, rec->r.dir.keyrec); p += 4;
|
|
|
|
ulongtobuf(p, rec->r.dir.ctlrec); p += 4;
|
|
|
|
ulongtobuf(p, rec->r.dir.sigrec); p += 4;
|
|
|
|
*p++ = rec->r.dir.no_sigs;
|
|
|
|
assert( rec->r.dir.local_id == recnum );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RECTYPE_KEY:
|
|
|
|
ulongtobuf(p, rec->r.key.owner); p += 4;
|
|
|
|
u32tobuf(p, rec->r.key.keyid[0]); p += 4;
|
|
|
|
u32tobuf(p, rec->r.key.keyid[1]); p += 4;
|
1998-01-31 22:24:36 +01:00
|
|
|
*p++ = rec->r.key.pubkey_algo; p++;
|
1998-01-30 21:25:31 +01:00
|
|
|
memcpy( p, rec->r.key.fingerprint, 20); p += 20;
|
1998-01-31 22:24:36 +01:00
|
|
|
*p++ = rec->r.key.ownertrust;
|
1998-01-13 20:04:23 +01:00
|
|
|
break;
|
1998-01-30 21:25:31 +01:00
|
|
|
|
|
|
|
case RECTYPE_CTL: /* control record */
|
|
|
|
ulongtobuf(p, rec->r.ctl.owner); p += 4;
|
|
|
|
memcpy(p, rec->r.ctl.blockhash, 20); p += 20;
|
|
|
|
*p++ = rec->r.ctl.trustlevel;
|
1998-01-13 20:04:23 +01:00
|
|
|
break;
|
1998-01-30 21:25:31 +01:00
|
|
|
|
|
|
|
case RECTYPE_SIG:
|
|
|
|
ulongtobuf(p, rec->r.sig.owner); p += 4;
|
|
|
|
ulongtobuf(p, rec->r.sig.chain); p += 4;
|
1998-01-14 13:34:05 +01:00
|
|
|
for(i=0; i < SIGS_PER_RECORD; i++ ) {
|
1998-01-30 21:25:31 +01:00
|
|
|
ulongtobuf(p, rec->r.sig.sig[i].local_id); p += 4;
|
|
|
|
*p++ = rec->r.sig.sig[i].flag;
|
1998-01-14 13:34:05 +01:00
|
|
|
}
|
|
|
|
break;
|
1998-01-13 20:04:23 +01:00
|
|
|
default:
|
1998-01-16 22:15:24 +01:00
|
|
|
BUG();
|
1998-01-13 20:04:23 +01:00
|
|
|
}
|
1998-01-12 11:18:17 +01:00
|
|
|
|
1998-01-13 20:04:23 +01:00
|
|
|
if( lseek( db_fd, recnum * TRUST_RECORD_LEN, SEEK_SET ) == -1 ) {
|
|
|
|
log_error("trustdb: lseek failed: %s\n", strerror(errno) );
|
|
|
|
return G10ERR_WRITE_FILE;
|
|
|
|
}
|
|
|
|
n = write( db_fd, buf, TRUST_RECORD_LEN);
|
|
|
|
if( n != TRUST_RECORD_LEN ) {
|
|
|
|
log_error("trustdb: write failed (n=%d): %s\n", n, strerror(errno) );
|
|
|
|
return G10ERR_WRITE_FILE;
|
|
|
|
}
|
1998-01-12 11:18:17 +01:00
|
|
|
|
1998-01-13 20:04:23 +01:00
|
|
|
return rc;
|
|
|
|
}
|
1998-01-12 11:18:17 +01:00
|
|
|
|
1998-01-16 22:15:24 +01:00
|
|
|
|
|
|
|
/****************
|
|
|
|
* create a new record and return its record number
|
|
|
|
*/
|
|
|
|
static ulong
|
|
|
|
new_recnum()
|
1998-01-13 20:04:23 +01:00
|
|
|
{
|
|
|
|
off_t offset;
|
1998-01-16 22:15:24 +01:00
|
|
|
ulong recnum;
|
|
|
|
TRUSTREC rec;
|
|
|
|
int rc;
|
1998-01-13 20:04:23 +01:00
|
|
|
|
|
|
|
/* fixme: look for unused records */
|
|
|
|
offset = lseek( db_fd, 0, SEEK_END );
|
|
|
|
if( offset == -1 )
|
|
|
|
log_fatal("trustdb: lseek to end failed: %s\n", strerror(errno) );
|
|
|
|
recnum = offset / TRUST_RECORD_LEN;
|
|
|
|
assert(recnum); /* this is will never be the first record */
|
1998-01-16 22:15:24 +01:00
|
|
|
|
|
|
|
/* we must write a record, so that the next call to this function
|
|
|
|
* returns another recnum */
|
|
|
|
memset( &rec, 0, sizeof rec );
|
|
|
|
rec.rectype = 0; /* free record */
|
|
|
|
rc = write_record(recnum, &rec );
|
|
|
|
if( rc )
|
|
|
|
log_fatal("%s: failed to append a record: %s\n",
|
|
|
|
db_name, g10_errstr(rc));
|
1998-01-13 20:04:23 +01:00
|
|
|
return recnum ;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************
|
1998-01-30 21:25:31 +01:00
|
|
|
* Search the trustdb for a key which matches PKC and return the dir record
|
|
|
|
* The local_id of PKC is set to the correct value
|
|
|
|
*
|
|
|
|
* Note: To increase performance, we could use a index search here.
|
1998-01-13 20:04:23 +01:00
|
|
|
*/
|
|
|
|
static int
|
1998-01-30 21:25:31 +01:00
|
|
|
search_record( PKT_public_cert *pkc, TRUSTREC *rec )
|
1998-01-13 20:04:23 +01:00
|
|
|
{
|
1998-01-16 22:15:24 +01:00
|
|
|
ulong recnum;
|
1998-01-13 20:04:23 +01:00
|
|
|
u32 keyid[2];
|
|
|
|
byte *fingerprint;
|
|
|
|
size_t fingerlen;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
keyid_from_pkc( pkc, keyid );
|
|
|
|
fingerprint = fingerprint_from_pkc( pkc, &fingerlen );
|
|
|
|
assert( fingerlen == 20 || fingerlen == 16 );
|
|
|
|
|
1998-01-31 22:24:36 +01:00
|
|
|
for(recnum=1; !(rc=read_record( recnum, rec, 0)); recnum++ ) {
|
1998-01-30 21:25:31 +01:00
|
|
|
if( rec->rectype != RECTYPE_DIR )
|
1998-01-13 20:04:23 +01:00
|
|
|
continue;
|
1998-01-30 21:25:31 +01:00
|
|
|
if( rec->r.dir.keyid[0] == keyid[0]
|
|
|
|
&& rec->r.dir.keyid[1] == keyid[1]){
|
|
|
|
TRUSTREC keyrec;
|
|
|
|
|
1998-01-31 22:24:36 +01:00
|
|
|
if( read_record( rec->r.dir.keyrec, &keyrec, RECTYPE_KEY ) ) {
|
1998-02-02 15:36:06 +01:00
|
|
|
log_error("%lu: ooops: invalid key record\n", recnum );
|
1998-01-30 21:25:31 +01:00
|
|
|
break;
|
|
|
|
}
|
1998-01-31 22:24:36 +01:00
|
|
|
if( keyrec.r.key.pubkey_algo == pkc->pubkey_algo
|
1998-01-30 21:25:31 +01:00
|
|
|
&& !memcmp(keyrec.r.key.fingerprint, fingerprint, fingerlen) ){
|
1998-01-13 20:04:23 +01:00
|
|
|
if( pkc->local_id && pkc->local_id != recnum )
|
|
|
|
log_error("%s: found record, but local_id from mem does "
|
1998-01-30 21:25:31 +01:00
|
|
|
"not match recnum (%lu,%lu)\n", db_name,
|
|
|
|
(ulong)pkc->local_id, (ulong)recnum );
|
1998-01-13 20:04:23 +01:00
|
|
|
pkc->local_id = recnum;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
1998-01-14 13:34:05 +01:00
|
|
|
}
|
|
|
|
if( rc != -1 )
|
1998-01-30 21:25:31 +01:00
|
|
|
log_error("%s: search_db failed: %s\n",db_name, g10_errstr(rc) );
|
1998-01-14 13:34:05 +01:00
|
|
|
return rc;
|
|
|
|
}
|
1998-01-12 11:18:17 +01:00
|
|
|
|
|
|
|
|
1998-01-16 22:15:24 +01:00
|
|
|
/****************
|
1998-01-30 21:25:31 +01:00
|
|
|
* If we do not have a local_id in a signature packet, find the owner of
|
|
|
|
* the signature packet in our trustdb or insert him into the trustdb
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
set_signature_packets_local_id( PKT_signature *sig )
|
|
|
|
{
|
|
|
|
PKT_public_cert *pkc = m_alloc_clear( sizeof *pkc );
|
|
|
|
TRUSTREC rec;
|
|
|
|
int rc;
|
1998-01-19 19:54:44 +01:00
|
|
|
|
1998-01-30 21:25:31 +01:00
|
|
|
rc = get_pubkey( pkc, sig->keyid );
|
|
|
|
if( rc)
|
|
|
|
goto leave;
|
|
|
|
if( !pkc->local_id ) {
|
|
|
|
rc = search_record( pkc, &rec );
|
|
|
|
if( rc == -1 )
|
|
|
|
rc = insert_trust_record( pkc );
|
|
|
|
if( rc )
|
|
|
|
goto leave;
|
|
|
|
/* fixme: we should propagate the local_id to all copies of the PKC */
|
1998-01-16 22:15:24 +01:00
|
|
|
}
|
1998-01-30 21:25:31 +01:00
|
|
|
sig->local_id = pkc->local_id;
|
1998-01-16 22:15:24 +01:00
|
|
|
|
1998-01-30 21:25:31 +01:00
|
|
|
leave:
|
1998-01-16 22:15:24 +01:00
|
|
|
free_public_cert( pkc );
|
1998-01-30 21:25:31 +01:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
keyid_from_local_id( ulong lid, u32 *keyid )
|
|
|
|
{
|
|
|
|
TRUSTREC rec;
|
|
|
|
int rc;
|
|
|
|
|
1998-01-31 22:24:36 +01:00
|
|
|
rc = read_record( lid, &rec, RECTYPE_DIR );
|
1998-01-30 21:25:31 +01:00
|
|
|
if( rc ) {
|
|
|
|
log_error("error reading record with local_id %lu: %s\n",
|
|
|
|
lid, g10_errstr(rc));
|
|
|
|
return G10ERR_TRUSTDB;
|
|
|
|
}
|
|
|
|
if( rec.rectype != RECTYPE_DIR ) {
|
|
|
|
log_error("record with local_id %lu is not a dir record\n", lid);
|
|
|
|
return G10ERR_TRUSTDB;
|
|
|
|
}
|
|
|
|
keyid[0] = rec.r.dir.keyid[0];
|
|
|
|
keyid[1] = rec.r.dir.keyid[1];
|
|
|
|
return 0;
|
1998-01-16 22:15:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************
|
|
|
|
* Walk throug the signatures of a public key.
|
|
|
|
* The caller must provide a context structure, with all fields set
|
1998-01-30 21:25:31 +01:00
|
|
|
* to zero, but the local_id field set to the requested key;
|
1998-01-16 22:15:24 +01:00
|
|
|
* This function does not change this field. On return the context
|
|
|
|
* is filled with the local-id of the signature and the signature flag.
|
|
|
|
* No fields should be changed (clearing all fields and setting
|
|
|
|
* pubkeyid is okay to continue with an other pubkey)
|
|
|
|
* Returns: 0 - okay, -1 for eof (no more sigs) or any other errorcode
|
|
|
|
*/
|
|
|
|
static int
|
1998-01-25 19:56:33 +01:00
|
|
|
walk_sigrecs( SIGREC_CONTEXT *c, int create )
|
1998-01-16 22:15:24 +01:00
|
|
|
{
|
|
|
|
int rc=0;
|
|
|
|
TRUSTREC *r;
|
|
|
|
ulong rnum;
|
|
|
|
|
|
|
|
if( c->ctl.eof )
|
|
|
|
return -1;
|
|
|
|
r = &c->ctl.rec;
|
1998-01-31 22:24:36 +01:00
|
|
|
if( !c->ctl.init_done ) {
|
|
|
|
c->ctl.init_done = 1;
|
|
|
|
if( !c->sigrec ) {
|
|
|
|
rc = read_record( c->local_id, r, RECTYPE_DIR );
|
1998-01-25 19:56:33 +01:00
|
|
|
if( rc ) {
|
1998-01-31 22:24:36 +01:00
|
|
|
log_error("%lu: error reading dir record: %s\n",
|
|
|
|
c->local_id, g10_errstr(rc));
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
c->sigrec = r->r.dir.sigrec;
|
|
|
|
if( !c->sigrec && create && !r->r.dir.no_sigs ) {
|
|
|
|
rc = build_sigrecs( c->local_id );
|
|
|
|
if( rc ) {
|
1998-02-02 15:36:06 +01:00
|
|
|
if( rc == G10ERR_BAD_CERT )
|
|
|
|
rc = -1; /* maybe no selcficnature */
|
1998-01-31 22:24:36 +01:00
|
|
|
if( rc != -1 )
|
|
|
|
log_info("%lu: error building sigs on the fly: %s\n",
|
|
|
|
c->local_id, g10_errstr(rc) );
|
|
|
|
c->ctl.eof = 1;
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
rc = read_record( c->local_id, r, RECTYPE_DIR );
|
|
|
|
if( rc ) {
|
|
|
|
log_error("%lu: error re-reading dir record: %s\n",
|
|
|
|
c->local_id, g10_errstr(rc));
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
c->sigrec = r->r.dir.sigrec;
|
|
|
|
}
|
|
|
|
if( !c->sigrec ) {
|
|
|
|
c->ctl.eof = 1;
|
|
|
|
return -1;
|
1998-01-25 19:56:33 +01:00
|
|
|
}
|
1998-01-16 22:15:24 +01:00
|
|
|
}
|
1998-01-31 22:24:36 +01:00
|
|
|
/* force a read */
|
|
|
|
c->ctl.index = SIGS_PER_RECORD;
|
|
|
|
r->r.sig.chain = c->sigrec;
|
1998-01-16 22:15:24 +01:00
|
|
|
}
|
1998-01-31 22:24:36 +01:00
|
|
|
|
1998-01-16 22:15:24 +01:00
|
|
|
/* enter loop to skip deleted sigs */
|
|
|
|
do {
|
|
|
|
if( c->ctl.index >= SIGS_PER_RECORD ) {
|
1998-01-31 22:24:36 +01:00
|
|
|
/* read the record */
|
|
|
|
rnum = r->r.sig.chain;
|
|
|
|
if( !rnum ) {
|
1998-01-16 22:15:24 +01:00
|
|
|
c->ctl.eof = 1;
|
|
|
|
return -1; /* return eof */
|
|
|
|
}
|
1998-01-31 22:24:36 +01:00
|
|
|
rc = read_record( rnum, r, RECTYPE_SIG );
|
1998-01-16 22:15:24 +01:00
|
|
|
if( rc ) {
|
1998-01-31 22:24:36 +01:00
|
|
|
log_error("error reading sigrec: %s\n", g10_errstr(rc));
|
1998-01-16 22:15:24 +01:00
|
|
|
c->ctl.eof = 1;
|
|
|
|
return rc;
|
|
|
|
}
|
1998-01-31 22:24:36 +01:00
|
|
|
if( r->r.sig.owner != c->local_id ) {
|
1998-01-16 22:15:24 +01:00
|
|
|
log_error("chained sigrec %lu has a wrong owner\n", rnum );
|
|
|
|
c->ctl.eof = 1;
|
|
|
|
return G10ERR_TRUSTDB;
|
|
|
|
}
|
|
|
|
c->ctl.index = 0;
|
|
|
|
}
|
1998-01-31 22:24:36 +01:00
|
|
|
} while( !r->r.sig.sig[c->ctl.index++].local_id );
|
|
|
|
c->sig_id = r->r.sig.sig[c->ctl.index-1].local_id;
|
|
|
|
c->sig_flag = r->r.sig.sig[c->ctl.index-1].flag;
|
1998-01-16 22:15:24 +01:00
|
|
|
return 0;
|
|
|
|
}
|
1998-01-12 11:18:17 +01:00
|
|
|
|
1998-01-16 22:15:24 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
1998-01-30 21:25:31 +01:00
|
|
|
/***********************************************
|
|
|
|
************* Trust stuff ******************
|
|
|
|
***********************************************/
|
1998-01-16 22:15:24 +01:00
|
|
|
|
|
|
|
|
1998-01-13 20:04:23 +01:00
|
|
|
/****************
|
1998-01-24 17:32:27 +01:00
|
|
|
* Verify, that all our public keys are in the trustDB.
|
1998-01-13 20:04:23 +01:00
|
|
|
*/
|
|
|
|
static int
|
|
|
|
verify_own_certs()
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
void *enum_context = NULL;
|
|
|
|
PKT_secret_cert *skc = m_alloc_clear( sizeof *skc );
|
|
|
|
PKT_public_cert *pkc = m_alloc_clear( sizeof *pkc );
|
|
|
|
u32 keyid[2];
|
|
|
|
|
|
|
|
while( !(rc=enum_secret_keys( &enum_context, skc) ) ) {
|
1998-01-16 22:15:24 +01:00
|
|
|
/* fixed: to be sure that it is a secret key of our own,
|
1998-01-13 20:04:23 +01:00
|
|
|
* we should check it, but this needs a passphrase
|
|
|
|
* for every key and this boring for the user.
|
|
|
|
* Solution: Sign the secring and the trustring
|
|
|
|
* and verify this signature during
|
|
|
|
* startup
|
|
|
|
*/
|
|
|
|
|
|
|
|
keyid_from_skc( skc, keyid );
|
|
|
|
|
|
|
|
if( DBG_TRUST )
|
|
|
|
log_debug("checking secret key %08lX\n", (ulong)keyid[1] );
|
|
|
|
|
|
|
|
/* look wether we can access the public key of this secret key */
|
|
|
|
rc = get_pubkey( pkc, keyid );
|
|
|
|
if( rc ) {
|
|
|
|
log_error("keyid %08lX: secret key without public key\n",
|
|
|
|
(ulong)keyid[1] );
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
if( cmp_public_secret_cert( pkc, skc ) ) {
|
|
|
|
log_error("keyid %08lX: secret and public key don't match\n",
|
|
|
|
(ulong)keyid[1] );
|
|
|
|
rc = G10ERR_GENERAL;
|
|
|
|
goto leave;
|
|
|
|
}
|
1998-01-24 17:32:27 +01:00
|
|
|
|
|
|
|
/* make sure that the pubkey is in the trustdb */
|
|
|
|
rc = query_trust_record( pkc );
|
|
|
|
if( rc == -1 ) { /* put it into the trustdb */
|
1998-01-16 22:15:24 +01:00
|
|
|
rc = insert_trust_record( pkc );
|
1998-01-24 17:32:27 +01:00
|
|
|
if( rc ) {
|
|
|
|
log_error("keyid %08lX: can't put it into the trustdb\n",
|
|
|
|
(ulong)keyid[1] );
|
|
|
|
goto leave;
|
|
|
|
}
|
1998-01-13 20:04:23 +01:00
|
|
|
}
|
1998-01-24 17:32:27 +01:00
|
|
|
else if( rc ) {
|
|
|
|
log_error("keyid %08lX: query record failed\n", (ulong)keyid[1] );
|
|
|
|
goto leave;
|
|
|
|
|
1998-01-16 22:15:24 +01:00
|
|
|
}
|
|
|
|
|
1998-01-19 19:54:44 +01:00
|
|
|
if( DBG_TRUST )
|
|
|
|
log_debug("putting %08lX(%lu) into ultikey_table\n",
|
|
|
|
(ulong)keyid[1], pkc->local_id );
|
|
|
|
if( ins_lid_table_item( ultikey_table, pkc->local_id, 0 ) )
|
|
|
|
log_error("keyid %08lX: already in ultikey_table\n",
|
|
|
|
(ulong)keyid[1]);
|
1998-01-13 20:04:23 +01:00
|
|
|
|
1998-01-24 17:32:27 +01:00
|
|
|
|
1998-01-13 20:04:23 +01:00
|
|
|
release_secret_cert_parts( skc );
|
|
|
|
release_public_cert_parts( pkc );
|
|
|
|
}
|
|
|
|
if( rc != -1 )
|
|
|
|
log_error("enum_secret_keys failed: %s\n", g10_errstr(rc) );
|
|
|
|
else
|
|
|
|
rc = 0;
|
|
|
|
|
|
|
|
leave:
|
|
|
|
free_secret_cert( skc );
|
|
|
|
free_public_cert( pkc );
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
1998-01-30 21:25:31 +01:00
|
|
|
|
1998-01-16 22:15:24 +01:00
|
|
|
static void
|
|
|
|
print_user_id( const char *text, u32 *keyid )
|
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
size_t n;
|
|
|
|
|
|
|
|
p = get_user_id( keyid, &n );
|
|
|
|
if( *text ) {
|
|
|
|
fputs( text, stdout);
|
|
|
|
putchar(' ');
|
|
|
|
}
|
|
|
|
putchar('\"');
|
|
|
|
print_string( stdout, p, n );
|
|
|
|
putchar('\"');
|
|
|
|
putchar('\n');
|
|
|
|
m_free(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (a non-recursive algorithm would be easier) */
|
|
|
|
static int
|
|
|
|
do_list_sigs( ulong root, ulong pubkey, int depth,
|
|
|
|
LOCAL_ID_INFO *lids, unsigned *lineno )
|
|
|
|
{
|
|
|
|
SIGREC_CONTEXT sx;
|
|
|
|
int rc;
|
|
|
|
u32 keyid[2];
|
|
|
|
|
|
|
|
memset( &sx, 0, sizeof sx );
|
1998-01-30 21:25:31 +01:00
|
|
|
sx.local_id = pubkey;
|
1998-01-16 22:15:24 +01:00
|
|
|
for(;;) {
|
1998-01-25 19:56:33 +01:00
|
|
|
rc = walk_sigrecs( &sx, 0 );
|
1998-01-16 22:15:24 +01:00
|
|
|
if( rc )
|
|
|
|
break;
|
|
|
|
rc = keyid_from_local_id( sx.sig_id, keyid );
|
|
|
|
if( rc ) {
|
|
|
|
printf("%6u: %*s????????(%lu:%02x)\n", *lineno, depth*4, "",
|
|
|
|
sx.sig_id, sx.sig_flag );
|
|
|
|
++*lineno;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
printf("%6u: %*s%08lX(%lu:%02x) ", *lineno, depth*4, "",
|
|
|
|
(ulong)keyid[1], sx.sig_id, sx.sig_flag );
|
|
|
|
/* check wether we already checked this pubkey */
|
1998-01-19 19:54:44 +01:00
|
|
|
if( !qry_lid_table_flag( ultikey_table, sx.sig_id, NULL ) ) {
|
1998-01-16 22:15:24 +01:00
|
|
|
print_user_id("[ultimately trusted]", keyid);
|
|
|
|
++*lineno;
|
|
|
|
}
|
|
|
|
else if( sx.sig_id == pubkey ) {
|
|
|
|
printf("[self-signature]\n");
|
|
|
|
++*lineno;
|
|
|
|
}
|
|
|
|
else if( sx.sig_id == root ) {
|
|
|
|
printf("[closed]\n");
|
|
|
|
++*lineno;
|
|
|
|
}
|
1998-01-19 19:54:44 +01:00
|
|
|
else if( ins_lid_table_item( lids, sx.sig_id, *lineno ) ) {
|
1998-01-16 22:15:24 +01:00
|
|
|
unsigned refline;
|
1998-01-19 19:54:44 +01:00
|
|
|
qry_lid_table_flag( lids, sx.sig_id, &refline );
|
1998-01-16 22:15:24 +01:00
|
|
|
printf("[see line %u]\n", refline);
|
|
|
|
++*lineno;
|
|
|
|
}
|
|
|
|
else if( depth+1 >= MAX_LIST_SIGS_DEPTH ) {
|
|
|
|
print_user_id( "[too deeply nested]", keyid );
|
|
|
|
++*lineno;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
print_user_id( "", keyid );
|
|
|
|
++*lineno;
|
|
|
|
rc = do_list_sigs( root, sx.sig_id, depth+1, lids, lineno );
|
|
|
|
if( rc )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rc==-1? 0 : rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************
|
|
|
|
* List all signatures of a public key
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
list_sigs( ulong pubkey_id )
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
u32 keyid[2];
|
|
|
|
LOCAL_ID_INFO *lids;
|
|
|
|
unsigned lineno = 1;
|
|
|
|
|
|
|
|
rc = keyid_from_local_id( pubkey_id, keyid );
|
|
|
|
if( rc ) {
|
|
|
|
log_error("Hmmm, no pubkey record for local_id %lu\n", pubkey_id);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
printf("Signatures of %08lX(%lu) ", (ulong)keyid[1], pubkey_id );
|
|
|
|
print_user_id("", keyid);
|
|
|
|
printf("----------------------\n");
|
|
|
|
|
|
|
|
lids = new_lid_table();
|
|
|
|
rc = do_list_sigs( pubkey_id, pubkey_id, 0, lids, &lineno );
|
|
|
|
putchar('\n');
|
|
|
|
release_lid_table(lids);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
1998-01-30 21:25:31 +01:00
|
|
|
/****************
|
|
|
|
* Function to collect all trustpaths
|
|
|
|
*/
|
1998-01-16 22:15:24 +01:00
|
|
|
static int
|
1998-01-24 17:32:27 +01:00
|
|
|
do_list_path( TRUST_INFO *stack, int depth, int max_depth,
|
1998-01-19 19:54:44 +01:00
|
|
|
LOCAL_ID_INFO *lids, TRUST_SEG_LIST *tslist )
|
1998-01-16 22:15:24 +01:00
|
|
|
{
|
|
|
|
SIGREC_CONTEXT sx;
|
1998-01-19 19:54:44 +01:00
|
|
|
unsigned last_depth;
|
1998-01-16 22:15:24 +01:00
|
|
|
int rc;
|
|
|
|
|
1998-01-19 19:54:44 +01:00
|
|
|
assert(depth);
|
|
|
|
|
|
|
|
/*printf("%2lu/%d: scrutinizig\n", stack[depth-1], depth);*/
|
|
|
|
if( depth >= max_depth || depth >= MAX_LIST_SIGS_DEPTH-1 ) {
|
|
|
|
/*printf("%2lu/%d: to deeply nested\n", stack[depth-1], depth);*/
|
1998-01-16 22:15:24 +01:00
|
|
|
return 0;
|
1998-01-19 19:54:44 +01:00
|
|
|
}
|
|
|
|
memset( &sx, 0, sizeof sx );
|
1998-01-30 21:25:31 +01:00
|
|
|
sx.local_id = stack[depth-1].lid;
|
1998-01-25 19:56:33 +01:00
|
|
|
/* loop over all signatures. If we do not have any, try to
|
|
|
|
* create them */
|
|
|
|
while( !(rc = walk_sigrecs( &sx, 1 )) ) {
|
1998-01-19 19:54:44 +01:00
|
|
|
TRUST_SEG_LIST tsl, t2, tl;
|
1998-01-16 22:15:24 +01:00
|
|
|
int i;
|
|
|
|
|
1998-01-24 17:32:27 +01:00
|
|
|
stack[depth].lid = sx.sig_id;
|
|
|
|
stack[depth].trust = 0;
|
1998-01-19 19:54:44 +01:00
|
|
|
if( qry_lid_table_flag( lids, sx.sig_id, &last_depth) ) {
|
|
|
|
/*printf("%2lu/%d: marked\n", sx.sig_id, depth );*/
|
|
|
|
ins_lid_table_item( lids, sx.sig_id, depth);
|
|
|
|
last_depth = depth;
|
|
|
|
}
|
|
|
|
else if( depth < last_depth ) {
|
|
|
|
/*printf("%2lu/%d: last_depth=%u - updated\n", sx.sig_id, depth, last_depth);*/
|
|
|
|
last_depth = depth;
|
|
|
|
upd_lid_table_flag( lids, sx.sig_id, depth);
|
|
|
|
}
|
|
|
|
|
|
|
|
if( last_depth < depth )
|
|
|
|
/*printf("%2lu/%d: already visited\n", sx.sig_id, depth)*/;
|
|
|
|
else if( !qry_lid_table_flag( ultikey_table, sx.sig_id, NULL ) ) {
|
|
|
|
/* found end of path; store it, ordered by path length */
|
1998-01-24 17:32:27 +01:00
|
|
|
tsl = m_alloc( sizeof *tsl + depth*sizeof(TRUST_INFO) );
|
1998-01-19 19:54:44 +01:00
|
|
|
tsl->nseg = depth+1;
|
|
|
|
tsl->dup = 0;
|
|
|
|
for(i=0; i <= depth; i++ )
|
|
|
|
tsl->seg[i] = stack[i];
|
|
|
|
for(t2=*tslist,tl=NULL; t2; tl=t2, t2 = t2->next )
|
|
|
|
if( depth < t2->nseg )
|
|
|
|
break;
|
|
|
|
if( !tl ) {
|
|
|
|
tsl->next = t2;
|
|
|
|
*tslist = tsl;
|
|
|
|
}
|
1998-01-16 22:15:24 +01:00
|
|
|
else {
|
1998-01-19 19:54:44 +01:00
|
|
|
tsl->next = t2;
|
|
|
|
tl->next = tsl;
|
1998-01-16 22:15:24 +01:00
|
|
|
}
|
1998-01-19 19:54:44 +01:00
|
|
|
/*putchar('.'); fflush(stdout);*/
|
|
|
|
/*printf("%2lu/%d: found\n", sx.sig_id, depth);*/
|
1998-01-16 22:15:24 +01:00
|
|
|
}
|
1998-01-19 19:54:44 +01:00
|
|
|
else {
|
|
|
|
rc = do_list_path( stack, depth+1, max_depth, lids, tslist);
|
|
|
|
if( rc && rc != -1 )
|
|
|
|
break;
|
1998-01-16 22:15:24 +01:00
|
|
|
}
|
1998-01-19 19:54:44 +01:00
|
|
|
}
|
1998-01-16 22:15:24 +01:00
|
|
|
return rc==-1? 0 : rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-01-12 11:18:17 +01:00
|
|
|
|
1998-01-13 20:04:23 +01:00
|
|
|
/****************
|
|
|
|
* Check all the sigs of the given keyblock and mark them
|
1998-01-30 21:25:31 +01:00
|
|
|
* as checked. Valid signatures which are duplicates are
|
|
|
|
* also marked [shall we check them at all?]
|
|
|
|
* FIXME: what shall we do if we have duplicate signatures were only
|
|
|
|
* some of them are bad?
|
1998-01-13 20:04:23 +01:00
|
|
|
*/
|
|
|
|
static int
|
1998-02-18 14:58:46 +01:00
|
|
|
check_sigs( KBNODE keyblock, int *selfsig_okay, int *revoked )
|
1998-01-13 20:04:23 +01:00
|
|
|
{
|
|
|
|
KBNODE node;
|
|
|
|
int rc;
|
1998-01-30 21:25:31 +01:00
|
|
|
LOCAL_ID_INFO *dups = NULL;
|
1998-01-13 20:04:23 +01:00
|
|
|
|
1998-01-16 22:15:24 +01:00
|
|
|
*selfsig_okay = 0;
|
1998-02-18 14:58:46 +01:00
|
|
|
*revoked = 0;
|
|
|
|
for( node=keyblock; node; node = node->next ) {
|
1998-01-13 20:04:23 +01:00
|
|
|
if( node->pkt->pkttype == PKT_SIGNATURE
|
1998-02-18 14:58:46 +01:00
|
|
|
&& ( (node->pkt->pkt.signature->sig_class&~3) == 0x10
|
|
|
|
|| node->pkt->pkt.signature->sig_class == 0x20
|
|
|
|
|| node->pkt->pkt.signature->sig_class == 0x30) ) {
|
1998-01-16 22:15:24 +01:00
|
|
|
int selfsig;
|
|
|
|
rc = check_key_signature( keyblock, node, &selfsig );
|
|
|
|
if( !rc ) {
|
1998-02-02 15:36:06 +01:00
|
|
|
rc = set_signature_packets_local_id( node->pkt->pkt.signature );
|
|
|
|
if( rc )
|
|
|
|
log_fatal("set_signature_packets_local_id failed: %s\n",
|
|
|
|
g10_errstr(rc));
|
1998-01-16 22:15:24 +01:00
|
|
|
if( selfsig ) {
|
|
|
|
node->flag |= 2; /* mark signature valid */
|
|
|
|
*selfsig_okay = 1;
|
|
|
|
}
|
1998-02-18 14:58:46 +01:00
|
|
|
else if( node->pkt->pkt.signature->sig_class == 0x20 )
|
|
|
|
*revoked = 1;
|
1998-01-16 22:15:24 +01:00
|
|
|
else
|
|
|
|
node->flag |= 1; /* mark signature valid */
|
1998-02-18 14:58:46 +01:00
|
|
|
|
|
|
|
if( node->pkt->pkt.signature->sig_class != 0x20 ) {
|
|
|
|
if( !dups )
|
|
|
|
dups = new_lid_table();
|
|
|
|
if( ins_lid_table_item( dups,
|
|
|
|
node->pkt->pkt.signature->local_id, 0) )
|
|
|
|
node->flag |= 4; /* mark as duplicate */
|
|
|
|
}
|
1998-01-16 22:15:24 +01:00
|
|
|
}
|
1998-01-13 20:04:23 +01:00
|
|
|
if( DBG_TRUST )
|
1998-02-02 15:36:06 +01:00
|
|
|
log_debug("trustdb: sig from %08lX(%lu): %s%s\n",
|
1998-01-16 22:15:24 +01:00
|
|
|
(ulong)node->pkt->pkt.signature->keyid[1],
|
1998-02-02 15:36:06 +01:00
|
|
|
node->pkt->pkt.signature->local_id,
|
|
|
|
g10_errstr(rc), (node->flag&4)?" (dup)":"" );
|
1998-01-13 20:04:23 +01:00
|
|
|
}
|
|
|
|
}
|
1998-01-30 21:25:31 +01:00
|
|
|
if( dups )
|
|
|
|
release_lid_table(dups);
|
1998-01-13 20:04:23 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-01-14 13:34:05 +01:00
|
|
|
/****************
|
|
|
|
* If we do not have sigrecs for the given key, build them and write them
|
|
|
|
* to the trustdb
|
|
|
|
*/
|
|
|
|
static int
|
1998-01-31 22:24:36 +01:00
|
|
|
build_sigrecs( ulong pubkeyid )
|
1998-01-14 13:34:05 +01:00
|
|
|
{
|
1998-01-30 21:25:31 +01:00
|
|
|
TRUSTREC rec, krec, rec2;
|
1998-01-16 22:15:24 +01:00
|
|
|
PUBKEY_FIND_INFO finfo=NULL;
|
|
|
|
KBPOS kbpos;
|
|
|
|
KBNODE keyblock = NULL;
|
|
|
|
KBNODE node;
|
|
|
|
int rc=0;
|
1998-02-18 14:58:46 +01:00
|
|
|
int i, selfsig, revoked;
|
1998-01-16 22:15:24 +01:00
|
|
|
ulong rnum, rnum2;
|
1998-02-02 15:36:06 +01:00
|
|
|
ulong first_sigrec = 0;
|
1998-01-16 22:15:24 +01:00
|
|
|
|
|
|
|
if( DBG_TRUST )
|
|
|
|
log_debug("trustdb: build_sigrecs for pubkey %lu\n", (ulong)pubkeyid );
|
|
|
|
|
|
|
|
/* get the keyblock */
|
1998-01-31 22:24:36 +01:00
|
|
|
if( (rc=read_record( pubkeyid, &rec, RECTYPE_DIR )) ) {
|
1998-01-30 21:25:31 +01:00
|
|
|
log_error("%lu: build_sigrecs: can't read dir record\n", pubkeyid );
|
1998-01-16 22:15:24 +01:00
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
finfo = m_alloc_clear( sizeof *finfo );
|
1998-01-30 21:25:31 +01:00
|
|
|
finfo->keyid[0] = rec.r.dir.keyid[0];
|
|
|
|
finfo->keyid[1] = rec.r.dir.keyid[1];
|
1998-01-31 22:24:36 +01:00
|
|
|
if( (rc=read_record( rec.r.dir.keyrec, &krec, RECTYPE_KEY )) ) {
|
1998-01-30 21:25:31 +01:00
|
|
|
log_error("%lu: build_sigrecs: can't read key record\n", pubkeyid);
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
finfo->pubkey_algo = krec.r.key.pubkey_algo;
|
|
|
|
memcpy( finfo->fingerprint, krec.r.key.fingerprint, 20);
|
1998-01-16 22:15:24 +01:00
|
|
|
rc = find_keyblock( finfo, &kbpos );
|
|
|
|
if( rc ) {
|
|
|
|
log_error("build_sigrecs: find_keyblock failed\n" );
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
rc = read_keyblock( &kbpos, &keyblock );
|
|
|
|
if( rc ) {
|
|
|
|
log_error("build_sigrecs: read_keyblock failed\n" );
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
/* check all key signatures */
|
1998-02-18 14:58:46 +01:00
|
|
|
rc = check_sigs( keyblock, &selfsig, &revoked );
|
1998-01-16 22:15:24 +01:00
|
|
|
if( rc ) {
|
|
|
|
log_error("build_sigrecs: check_sigs failed\n" );
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
if( !selfsig ) {
|
|
|
|
log_error("build_sigrecs: self-certificate missing\n" );
|
1998-02-02 15:36:06 +01:00
|
|
|
update_no_sigs( pubkeyid, 2 );
|
1998-01-16 22:15:24 +01:00
|
|
|
rc = G10ERR_BAD_CERT;
|
|
|
|
goto leave;
|
|
|
|
}
|
1998-02-18 14:58:46 +01:00
|
|
|
if( revoked ) {
|
|
|
|
log_info("build_sigrecs: key has been revoked\n" );
|
|
|
|
update_no_sigs( pubkeyid, 3 );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
update_no_sigs( pubkeyid, 0 ); /* assume we have sigs */
|
1998-01-31 22:24:36 +01:00
|
|
|
|
1998-01-16 22:15:24 +01:00
|
|
|
/* valid key signatures are now marked; we can now build the
|
|
|
|
* sigrecs */
|
|
|
|
memset( &rec, 0, sizeof rec );
|
1998-01-30 21:25:31 +01:00
|
|
|
rec.rectype = RECTYPE_SIG;
|
1998-01-16 22:15:24 +01:00
|
|
|
i = 0;
|
|
|
|
rnum = rnum2 = 0;
|
1998-02-18 14:58:46 +01:00
|
|
|
for( node=keyblock; node; node = node->next ) {
|
1998-01-30 21:25:31 +01:00
|
|
|
/* insert sigs which are not a selfsig nor a duplicate */
|
|
|
|
if( (node->flag & 1) && !(node->flag & 4) ) {
|
1998-01-16 22:15:24 +01:00
|
|
|
assert( node->pkt->pkttype == PKT_SIGNATURE );
|
|
|
|
if( !node->pkt->pkt.signature->local_id ) {
|
|
|
|
/* the next function should always succeed, because
|
|
|
|
* we have already checked the signature, and for this
|
|
|
|
* it was necessary to have the pubkey. The only reason
|
1998-01-25 19:56:33 +01:00
|
|
|
* this can fail are I/o errors of the trustdb or a
|
|
|
|
* remove operation on the pubkey database - which should
|
|
|
|
* not disturb us, because we have to chace them anyway. */
|
1998-01-16 22:15:24 +01:00
|
|
|
rc = set_signature_packets_local_id( node->pkt->pkt.signature );
|
|
|
|
if( rc )
|
|
|
|
log_fatal("set_signature_packets_local_id failed: %s\n",
|
|
|
|
g10_errstr(rc));
|
|
|
|
}
|
|
|
|
if( i == SIGS_PER_RECORD ) {
|
|
|
|
/* write the record */
|
|
|
|
rnum = new_recnum();
|
|
|
|
if( rnum2 ) { /* write the stored record */
|
1998-01-30 21:25:31 +01:00
|
|
|
rec2.r.sig.owner = pubkeyid;
|
|
|
|
rec2.r.sig.chain = rnum; /* the next record number */
|
1998-01-16 22:15:24 +01:00
|
|
|
rc = write_record( rnum2, &rec2 );
|
|
|
|
if( rc ) {
|
|
|
|
log_error("build_sigrecs: write_record failed\n" );
|
|
|
|
goto leave;
|
|
|
|
}
|
1998-02-02 15:36:06 +01:00
|
|
|
if( !first_sigrec )
|
|
|
|
first_sigrec = rnum2;
|
1998-01-16 22:15:24 +01:00
|
|
|
}
|
|
|
|
rec2 = rec;
|
|
|
|
rnum2 = rnum;
|
|
|
|
memset( &rec, 0, sizeof rec );
|
1998-01-30 21:25:31 +01:00
|
|
|
rec.rectype = RECTYPE_SIG;
|
1998-01-16 22:15:24 +01:00
|
|
|
i = 0;
|
|
|
|
}
|
1998-01-30 21:25:31 +01:00
|
|
|
rec.r.sig.sig[i].local_id = node->pkt->pkt.signature->local_id;
|
|
|
|
rec.r.sig.sig[i].flag = 0;
|
1998-01-16 22:15:24 +01:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if( i || rnum2 ) {
|
|
|
|
/* write the record */
|
|
|
|
rnum = new_recnum();
|
|
|
|
if( rnum2 ) { /* write the stored record */
|
1998-01-30 21:25:31 +01:00
|
|
|
rec2.r.sig.owner = pubkeyid;
|
|
|
|
rec2.r.sig.chain = rnum;
|
1998-01-16 22:15:24 +01:00
|
|
|
rc = write_record( rnum2, &rec2 );
|
|
|
|
if( rc ) {
|
|
|
|
log_error("build_sigrecs: write_record failed\n" );
|
|
|
|
goto leave;
|
|
|
|
}
|
1998-02-02 15:36:06 +01:00
|
|
|
if( !first_sigrec )
|
|
|
|
first_sigrec = rnum2;
|
1998-01-16 22:15:24 +01:00
|
|
|
}
|
|
|
|
if( i ) { /* write the pending record */
|
1998-01-30 21:25:31 +01:00
|
|
|
rec.r.sig.owner = pubkeyid;
|
|
|
|
rec.r.sig.chain = 0;
|
1998-01-16 22:15:24 +01:00
|
|
|
rc = write_record( rnum, &rec );
|
|
|
|
if( rc ) {
|
|
|
|
log_error("build_sigrecs: write_record failed\n" );
|
|
|
|
goto leave;
|
|
|
|
}
|
1998-02-02 15:36:06 +01:00
|
|
|
if( !first_sigrec )
|
|
|
|
first_sigrec = rnum;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if( first_sigrec ) {
|
|
|
|
/* update the dir record */
|
|
|
|
if( (rc =read_record( pubkeyid, &rec, RECTYPE_DIR )) ) {
|
|
|
|
log_error("update_dir_record: read failed\n");
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
rec.r.dir.sigrec = first_sigrec;
|
|
|
|
if( (rc=write_record( pubkeyid, &rec )) ) {
|
|
|
|
log_error("update_dir_record: write failed\n");
|
|
|
|
goto leave;
|
1998-01-16 22:15:24 +01:00
|
|
|
}
|
|
|
|
}
|
1998-02-02 15:36:06 +01:00
|
|
|
else
|
1998-02-18 14:58:46 +01:00
|
|
|
update_no_sigs( pubkeyid, revoked? 3:1 ); /* no signatures */
|
1998-01-16 22:15:24 +01:00
|
|
|
|
|
|
|
leave:
|
|
|
|
m_free( finfo );
|
|
|
|
release_kbnode( keyblock );
|
|
|
|
if( DBG_TRUST )
|
|
|
|
log_debug("trustdb: build_sigrecs: %s\n", g10_errstr(rc) );
|
|
|
|
return rc;
|
1998-01-14 13:34:05 +01:00
|
|
|
}
|
|
|
|
|
1998-01-24 17:32:27 +01:00
|
|
|
/****************
|
|
|
|
* Make a list of trust paths
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
make_tsl( ulong pubkey_id, TRUST_SEG_LIST *ret_tslist )
|
|
|
|
{
|
|
|
|
int i, rc;
|
|
|
|
LOCAL_ID_INFO *lids = new_lid_table();
|
|
|
|
TRUST_INFO stack[MAX_LIST_SIGS_DEPTH];
|
|
|
|
TRUST_SEG_LIST tsl, tslist;
|
|
|
|
int max_depth = 4;
|
|
|
|
|
|
|
|
tslist = *ret_tslist = NULL;
|
|
|
|
|
|
|
|
if( !qry_lid_table_flag( ultikey_table, pubkey_id, NULL ) ) {
|
|
|
|
tslist = m_alloc( sizeof *tslist );
|
|
|
|
tslist->nseg = 1;
|
|
|
|
tslist->dup = 0;
|
|
|
|
tslist->seg[0].lid = pubkey_id;
|
|
|
|
tslist->seg[0].trust = 0;
|
|
|
|
tslist->next = NULL;
|
|
|
|
rc = 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
stack[0].lid = pubkey_id;
|
|
|
|
stack[0].trust = 0;
|
|
|
|
rc = do_list_path( stack, 1, max_depth, lids, &tslist );
|
|
|
|
}
|
|
|
|
if( !rc ) { /* wipe out duplicates */
|
|
|
|
LOCAL_ID_INFO *work = new_lid_table();
|
|
|
|
for( tsl=tslist; tsl; tsl = tsl->next ) {
|
|
|
|
for(i=1; i < tsl->nseg-1; i++ ) {
|
|
|
|
if( ins_lid_table_item( work, tsl->seg[i].lid, 0 ) ) {
|
|
|
|
tsl->dup = 1; /* mark as duplicate */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
release_lid_table(work);
|
|
|
|
*ret_tslist = tslist;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
; /* FIXME: release tslist */
|
|
|
|
release_lid_table(lids);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
1998-01-14 13:34:05 +01:00
|
|
|
|
1998-01-24 17:32:27 +01:00
|
|
|
/****************
|
|
|
|
* Given a trust segment list tslist, walk over all paths and fill in
|
|
|
|
* the trust information for each segment. What this function does is
|
|
|
|
* to assign a trustvalue to the first segment (which is the requested key)
|
|
|
|
* of each path.
|
|
|
|
*
|
|
|
|
* FIXME: We have to do more thinks here. e.g. we should never increase
|
|
|
|
* the trust value.
|
|
|
|
*
|
|
|
|
* Do not do it for duplicates.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
propagate_trust( TRUST_SEG_LIST tslist )
|
|
|
|
{
|
|
|
|
int i, rc;
|
1998-02-02 15:36:06 +01:00
|
|
|
unsigned trust, tr;
|
1998-01-24 17:32:27 +01:00
|
|
|
TRUST_SEG_LIST tsl;
|
1998-01-14 13:34:05 +01:00
|
|
|
|
1998-01-24 17:32:27 +01:00
|
|
|
for(tsl = tslist; tsl; tsl = tsl->next ) {
|
|
|
|
if( tsl->dup )
|
|
|
|
continue;
|
|
|
|
assert( tsl->nseg );
|
|
|
|
/* the last segment is always a ultimately trusted one, so we can
|
|
|
|
* assign a fully trust to the next one */
|
|
|
|
i = tsl->nseg-1;
|
|
|
|
tsl->seg[i].trust = TRUST_ULTIMATE;
|
|
|
|
trust = TRUST_FULLY;
|
|
|
|
for(i-- ; i >= 0; i-- ) {
|
|
|
|
tsl->seg[i].trust = trust;
|
|
|
|
if( i > 0 ) {
|
|
|
|
/* get the trust of this pubkey */
|
1998-02-02 15:36:06 +01:00
|
|
|
rc = get_ownertrust( tsl->seg[i].lid, &tr );
|
1998-01-24 17:32:27 +01:00
|
|
|
if( rc )
|
|
|
|
return rc;
|
1998-02-02 15:36:06 +01:00
|
|
|
if( tr < trust )
|
|
|
|
trust = tr;
|
1998-01-24 17:32:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
1998-01-14 13:34:05 +01:00
|
|
|
|
1998-01-13 20:04:23 +01:00
|
|
|
|
|
|
|
/****************
|
1998-01-24 17:32:27 +01:00
|
|
|
* we have the pubkey record but nothing more is known
|
1998-01-31 22:24:36 +01:00
|
|
|
* function may re-read dr.
|
1998-01-13 20:04:23 +01:00
|
|
|
*/
|
|
|
|
static int
|
1998-01-31 22:24:36 +01:00
|
|
|
do_check( ulong pubkeyid, TRUSTREC *dr, unsigned *trustlevel )
|
1998-01-13 20:04:23 +01:00
|
|
|
{
|
1998-01-24 17:32:27 +01:00
|
|
|
int i, rc=0;
|
|
|
|
TRUST_SEG_LIST tsl, tsl2, tslist;
|
|
|
|
int marginal, fully;
|
1998-01-25 19:56:33 +01:00
|
|
|
int fully_needed = opt.completes_needed;
|
|
|
|
int marginal_needed = opt.marginals_needed;
|
1998-02-18 14:58:46 +01:00
|
|
|
unsigned tflags = 0;
|
1998-01-25 19:56:33 +01:00
|
|
|
|
|
|
|
assert( fully_needed > 0 && marginal_needed > 1 );
|
|
|
|
|
1998-01-24 17:32:27 +01:00
|
|
|
|
|
|
|
*trustlevel = TRUST_UNDEFINED;
|
1998-01-16 22:15:24 +01:00
|
|
|
|
|
|
|
/* verify the cache */
|
|
|
|
|
|
|
|
/* do we have sigrecs */
|
1998-01-31 22:24:36 +01:00
|
|
|
if( !dr->r.dir.sigrec && !dr->r.dir.no_sigs) {
|
|
|
|
/* no sigrecs, so build them */
|
|
|
|
rc = build_sigrecs( pubkeyid );
|
1998-01-16 22:15:24 +01:00
|
|
|
if( !rc ) /* and read again */
|
1998-01-31 22:24:36 +01:00
|
|
|
rc = read_record( pubkeyid, dr, RECTYPE_DIR );
|
1998-01-16 22:15:24 +01:00
|
|
|
}
|
1998-02-18 14:58:46 +01:00
|
|
|
|
|
|
|
if( dr->r.dir.no_sigs == 3 )
|
|
|
|
tflags |= TRUST_FLAG_REVOKED;
|
|
|
|
|
1998-02-02 15:36:06 +01:00
|
|
|
if( !rc && !dr->r.dir.sigrec ) {
|
|
|
|
/* See wether this is our own key */
|
1998-03-03 09:43:28 +01:00
|
|
|
if( !qry_lid_table_flag( ultikey_table, pubkeyid, NULL ) )
|
1998-02-18 14:58:46 +01:00
|
|
|
*trustlevel = tflags | TRUST_ULTIMATE;
|
1998-03-03 09:43:28 +01:00
|
|
|
return 0;
|
1998-02-02 15:36:06 +01:00
|
|
|
}
|
1998-01-16 22:15:24 +01:00
|
|
|
if( rc )
|
|
|
|
return rc; /* error while looking for sigrec or building sigrecs */
|
1998-01-14 13:34:05 +01:00
|
|
|
|
1998-01-24 17:32:27 +01:00
|
|
|
/* fixme: take it from the cache if it is valid */
|
|
|
|
|
|
|
|
/* Make a list of all possible trust-paths */
|
|
|
|
rc = make_tsl( pubkeyid, &tslist );
|
|
|
|
if( rc )
|
|
|
|
return rc;
|
|
|
|
rc = propagate_trust( tslist );
|
|
|
|
if( rc )
|
|
|
|
return rc;
|
|
|
|
for(tsl = tslist; tsl; tsl = tsl->next ) {
|
|
|
|
if( tsl->dup )
|
|
|
|
continue;
|
|
|
|
|
1998-02-12 00:22:09 +01:00
|
|
|
if( opt.verbose ) {
|
|
|
|
log_info("tslist segs:" );
|
|
|
|
for(i=0; i < tsl->nseg; i++ )
|
|
|
|
fprintf(stderr, " %lu/%02x", tsl->seg[i].lid,
|
|
|
|
tsl->seg[i].trust );
|
|
|
|
putc('\n',stderr);
|
|
|
|
}
|
1998-01-24 17:32:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* and look wether there is a trusted path.
|
|
|
|
* We only have to look at the first segment, because
|
|
|
|
* propagate_trust has investigated all other segments */
|
|
|
|
marginal = fully = 0;
|
|
|
|
for(tsl = tslist; tsl; tsl = tsl->next ) {
|
|
|
|
if( tsl->dup )
|
|
|
|
continue;
|
|
|
|
if( tsl->seg[0].trust == TRUST_ULTIMATE ) {
|
1998-02-18 14:58:46 +01:00
|
|
|
*trustlevel = tflags | TRUST_ULTIMATE; /* our own key */
|
1998-01-24 17:32:27 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if( tsl->seg[0].trust == TRUST_FULLY ) {
|
|
|
|
marginal++;
|
|
|
|
fully++;
|
|
|
|
}
|
|
|
|
else if( tsl->seg[0].trust == TRUST_MARGINAL )
|
|
|
|
marginal++;
|
|
|
|
|
|
|
|
if( fully >= fully_needed ) {
|
1998-02-18 14:58:46 +01:00
|
|
|
*trustlevel = tflags | TRUST_FULLY;
|
1998-01-24 17:32:27 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if( !tsl && marginal >= marginal_needed )
|
1998-02-18 14:58:46 +01:00
|
|
|
*trustlevel = tflags | TRUST_MARGINAL;
|
1998-01-24 17:32:27 +01:00
|
|
|
|
|
|
|
/* cache the tslist */
|
|
|
|
if( last_trust_web_key ) {
|
|
|
|
for( tsl = last_trust_web_tslist; tsl; tsl = tsl2 ) {
|
|
|
|
tsl2 = tsl->next;
|
|
|
|
m_free(tsl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
last_trust_web_key = pubkeyid;
|
|
|
|
last_trust_web_tslist = tslist;
|
1998-01-16 22:15:24 +01:00
|
|
|
return 0;
|
1998-01-13 20:04:23 +01:00
|
|
|
}
|
|
|
|
|
1998-01-12 11:18:17 +01:00
|
|
|
|
|
|
|
/*********************************************************
|
|
|
|
**************** API Interface ************************
|
|
|
|
*********************************************************/
|
|
|
|
|
|
|
|
/****************
|
|
|
|
* Perform some checks over the trustdb
|
1998-01-16 22:15:24 +01:00
|
|
|
* level 0: only open the db
|
|
|
|
* 1: used for initial program startup
|
1998-01-12 11:18:17 +01:00
|
|
|
*/
|
|
|
|
int
|
1998-02-02 15:36:06 +01:00
|
|
|
init_trustdb( int level, const char *dbname )
|
1998-01-12 11:18:17 +01:00
|
|
|
{
|
1998-01-13 20:04:23 +01:00
|
|
|
int rc=0;
|
|
|
|
|
1998-01-16 22:15:24 +01:00
|
|
|
if( !ultikey_table )
|
|
|
|
ultikey_table = new_lid_table();
|
|
|
|
|
|
|
|
if( !level || level==1 ) {
|
1998-02-02 15:36:06 +01:00
|
|
|
char *fname = dbname? m_strdup( dbname )
|
1998-02-24 19:50:46 +01:00
|
|
|
: make_filename(opt.homedir, "trustdb.gpg", NULL );
|
1998-01-12 11:18:17 +01:00
|
|
|
if( access( fname, R_OK ) ) {
|
|
|
|
if( errno != ENOENT ) {
|
|
|
|
log_error("can't access %s: %s\n", fname, strerror(errno) );
|
|
|
|
m_free(fname);
|
|
|
|
return G10ERR_TRUSTDB;
|
|
|
|
}
|
1998-02-17 21:48:52 +01:00
|
|
|
if( level ) {
|
|
|
|
char *p = strrchr( fname, '/' );
|
|
|
|
assert(p);
|
|
|
|
*p = 0;
|
|
|
|
if( access( fname, F_OK ) ) {
|
1998-02-24 19:50:46 +01:00
|
|
|
#if __MINGW32__
|
|
|
|
if( mkdir( fname ) )
|
|
|
|
#else
|
1998-02-17 21:48:52 +01:00
|
|
|
if( mkdir( fname, S_IRUSR|S_IWUSR|S_IXUSR ) )
|
1998-02-24 19:50:46 +01:00
|
|
|
#endif
|
1998-02-17 21:48:52 +01:00
|
|
|
log_fatal("can't create directory '%s': %s\n",
|
|
|
|
fname, strerror(errno) );
|
|
|
|
}
|
|
|
|
*p = '/';
|
1998-01-16 22:15:24 +01:00
|
|
|
create_db( fname );
|
1998-02-17 21:48:52 +01:00
|
|
|
}
|
1998-01-12 11:18:17 +01:00
|
|
|
}
|
|
|
|
m_free(db_name);
|
|
|
|
db_name = fname;
|
|
|
|
|
1998-01-16 22:15:24 +01:00
|
|
|
if( !level )
|
|
|
|
return 0;
|
|
|
|
|
1998-01-12 11:18:17 +01:00
|
|
|
/* we can verify a signature about our local data (secring and trustdb)
|
1998-01-13 20:04:23 +01:00
|
|
|
* in ~/.g10/ here */
|
|
|
|
rc = verify_private_data();
|
|
|
|
if( !rc ) {
|
|
|
|
/* verify, that our own certificates are in the trustDB
|
|
|
|
* or move them to the trustdb. */
|
|
|
|
rc = verify_own_certs();
|
|
|
|
|
|
|
|
/* should we check wether there is no other ultimately trusted
|
|
|
|
* key in the database? */
|
|
|
|
|
|
|
|
}
|
1998-01-12 11:18:17 +01:00
|
|
|
}
|
|
|
|
else
|
1998-01-16 22:15:24 +01:00
|
|
|
BUG();
|
1998-01-12 11:18:17 +01:00
|
|
|
|
1998-01-13 20:04:23 +01:00
|
|
|
return rc;
|
1998-01-12 11:18:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-01-30 21:25:31 +01:00
|
|
|
void
|
|
|
|
list_trustdb( const char *username )
|
|
|
|
{
|
|
|
|
TRUSTREC rec;
|
|
|
|
|
|
|
|
if( username ) {
|
|
|
|
PKT_public_cert *pkc = m_alloc_clear( sizeof *pkc );
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
if( (rc = get_pubkey_byname( pkc, username )) )
|
|
|
|
log_error("user '%s' not found: %s\n", username, g10_errstr(rc) );
|
|
|
|
else if( (rc=search_record( pkc, &rec )) && rc != -1 )
|
|
|
|
log_error("problem finding '%s' in trustdb: %s\n",
|
|
|
|
username, g10_errstr(rc));
|
|
|
|
else if( rc == -1 )
|
|
|
|
log_error("user '%s' not in trustdb\n", username);
|
|
|
|
else if( (rc = list_sigs( pkc->local_id )) )
|
|
|
|
log_error("user '%s' list problem: %s\n", username, g10_errstr(rc));
|
|
|
|
free_public_cert( pkc );
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ulong recnum;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
printf("TrustDB: %s\n", db_name );
|
|
|
|
for(i=9+strlen(db_name); i > 0; i-- )
|
|
|
|
putchar('-');
|
|
|
|
putchar('\n');
|
1998-01-31 22:24:36 +01:00
|
|
|
for(recnum=0; !read_record( recnum, &rec, 0); recnum++ )
|
1998-01-30 21:25:31 +01:00
|
|
|
dump_record( recnum, &rec, stdout );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
list_trust_path( int max_depth, const char *username )
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
int wipe=0;
|
|
|
|
int i;
|
|
|
|
TRUSTREC rec;
|
|
|
|
PKT_public_cert *pkc = m_alloc_clear( sizeof *pkc );
|
|
|
|
|
|
|
|
if( max_depth < 0 ) {
|
|
|
|
wipe = 1;
|
|
|
|
max_depth = -max_depth;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( (rc = get_pubkey_byname( pkc, username )) )
|
|
|
|
log_error("user '%s' not found: %s\n", username, g10_errstr(rc) );
|
|
|
|
else if( (rc=search_record( pkc, &rec )) && rc != -1 )
|
|
|
|
log_error("problem finding '%s' in trustdb: %s\n",
|
|
|
|
username, g10_errstr(rc));
|
|
|
|
else if( rc == -1 ) {
|
|
|
|
log_info("user '%s' not in trustdb - inserting\n", username);
|
|
|
|
rc = insert_trust_record( pkc );
|
|
|
|
if( rc )
|
|
|
|
log_error("failed to put '%s' into trustdb: %s\n", username, g10_errstr(rc));
|
|
|
|
else {
|
|
|
|
assert( pkc->local_id );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !rc ) {
|
|
|
|
TRUST_SEG_LIST tsl, tslist = NULL;
|
|
|
|
|
|
|
|
if( !qry_lid_table_flag( ultikey_table, pkc->local_id, NULL ) ) {
|
|
|
|
tslist = m_alloc( sizeof *tslist );
|
|
|
|
tslist->nseg = 1;
|
|
|
|
tslist->dup = 0;
|
|
|
|
tslist->seg[0].lid = pkc->local_id;
|
|
|
|
tslist->seg[0].trust = 0;
|
|
|
|
tslist->next = NULL;
|
|
|
|
rc = 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
LOCAL_ID_INFO *lids = new_lid_table();
|
|
|
|
TRUST_INFO stack[MAX_LIST_SIGS_DEPTH];
|
|
|
|
|
|
|
|
stack[0].lid = pkc->local_id;
|
|
|
|
stack[0].trust = 0;
|
|
|
|
rc = do_list_path( stack, 1, max_depth, lids, &tslist );
|
|
|
|
if( wipe ) { /* wipe out duplicates */
|
|
|
|
LOCAL_ID_INFO *work;
|
|
|
|
|
|
|
|
work = new_lid_table();
|
|
|
|
for( tsl=tslist; tsl; tsl = tsl->next ) {
|
|
|
|
for(i=1; i < tsl->nseg-1; i++ ) {
|
|
|
|
if( ins_lid_table_item( work, tsl->seg[i].lid, 0 ) ) {
|
|
|
|
tsl->dup = 1; /* mark as duplicate */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
release_lid_table(work);
|
|
|
|
}
|
|
|
|
release_lid_table(lids);
|
1998-01-31 22:24:36 +01:00
|
|
|
}
|
1998-01-30 21:25:31 +01:00
|
|
|
if( rc )
|
|
|
|
log_error("user '%s' list problem: %s\n", username, g10_errstr(rc));
|
|
|
|
rc = propagate_trust( tslist );
|
|
|
|
if( rc )
|
|
|
|
log_error("user '%s' trust problem: %s\n", username, g10_errstr(rc));
|
|
|
|
for(tsl = tslist; tsl; tsl = tsl->next ) {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if( tsl->dup )
|
|
|
|
continue;
|
|
|
|
printf("trust path:" );
|
|
|
|
for(i=0; i < tsl->nseg; i++ )
|
|
|
|
printf(" %lu/%02x", tsl->seg[i].lid, tsl->seg[i].trust );
|
|
|
|
putchar('\n');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free_public_cert( pkc );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-01-12 11:18:17 +01:00
|
|
|
/****************
|
|
|
|
* Get the trustlevel for this PKC.
|
|
|
|
* Note: This does not ask any questions
|
|
|
|
* Returns: 0 okay of an errorcode
|
|
|
|
*
|
|
|
|
* It operates this way:
|
|
|
|
* locate the pkc in the trustdb
|
|
|
|
* found:
|
|
|
|
* Do we have a valid cache record for it?
|
|
|
|
* yes: return trustlevel from cache
|
1998-01-14 13:34:05 +01:00
|
|
|
* no: make a cache record and all the other stuff
|
1998-01-12 11:18:17 +01:00
|
|
|
* not found:
|
|
|
|
* Return with a trustlevel, saying that we do not have
|
|
|
|
* a trust record for it. The caller may use insert_trust_record()
|
|
|
|
* and then call this function here again.
|
|
|
|
*
|
|
|
|
* Problems: How do we get the complete keyblock to check that the
|
|
|
|
* cache record is actually valid? Think we need a clever
|
|
|
|
* cache in getkey.c to keep track of this stuff. Maybe it
|
|
|
|
* is not necessary to check this if we use a local pubring. Hmmmm.
|
|
|
|
*/
|
|
|
|
int
|
1998-01-24 17:32:27 +01:00
|
|
|
check_trust( PKT_public_cert *pkc, unsigned *r_trustlevel )
|
1998-01-12 11:18:17 +01:00
|
|
|
{
|
1998-01-13 20:04:23 +01:00
|
|
|
TRUSTREC rec;
|
1998-01-24 17:32:27 +01:00
|
|
|
unsigned trustlevel = TRUST_UNKNOWN;
|
1998-01-13 20:04:23 +01:00
|
|
|
int rc=0;
|
1998-01-12 11:18:17 +01:00
|
|
|
|
1998-01-19 19:54:44 +01:00
|
|
|
if( DBG_TRUST )
|
|
|
|
log_info("check_trust() called.\n");
|
1998-01-12 11:18:17 +01:00
|
|
|
|
1998-01-13 20:04:23 +01:00
|
|
|
/* get the pubkey record */
|
|
|
|
if( pkc->local_id ) {
|
1998-01-31 22:24:36 +01:00
|
|
|
if( read_record( pkc->local_id, &rec, RECTYPE_DIR ) ) {
|
1998-01-19 19:54:44 +01:00
|
|
|
log_error("check_trust: read record failed\n");
|
1998-01-13 20:04:23 +01:00
|
|
|
return G10ERR_TRUSTDB;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else { /* no local_id: scan the trustdb */
|
1998-01-31 22:24:36 +01:00
|
|
|
if( (rc=search_record( pkc, &rec )) && rc != -1 ) {
|
|
|
|
log_error("check_trust: search_record failed: %s\n",
|
1998-01-13 20:04:23 +01:00
|
|
|
g10_errstr(rc));
|
1998-01-16 22:15:24 +01:00
|
|
|
return rc;
|
1998-01-13 20:04:23 +01:00
|
|
|
}
|
|
|
|
else if( rc == -1 ) {
|
1998-01-25 19:56:33 +01:00
|
|
|
rc = insert_trust_record( pkc );
|
|
|
|
if( rc ) {
|
|
|
|
log_error("failed to insert pubkey into trustdb: %s\n",
|
|
|
|
g10_errstr(rc));
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
log_info("pubkey not in trustdb - inserted as %lu\n",
|
|
|
|
pkc->local_id );
|
1998-01-13 20:04:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* fixme: do some additional checks on the pubkey record */
|
|
|
|
|
1998-01-31 22:24:36 +01:00
|
|
|
rc = do_check( pkc->local_id, &rec, &trustlevel );
|
1998-01-16 22:15:24 +01:00
|
|
|
if( rc ) {
|
1998-01-19 19:54:44 +01:00
|
|
|
log_error("check_trust: do_check failed: %s\n", g10_errstr(rc));
|
1998-01-16 22:15:24 +01:00
|
|
|
return rc;
|
|
|
|
}
|
1998-01-14 13:34:05 +01:00
|
|
|
|
1998-01-13 20:04:23 +01:00
|
|
|
|
|
|
|
leave:
|
1998-01-19 19:54:44 +01:00
|
|
|
if( DBG_TRUST )
|
|
|
|
log_info("check_trust() returns trustlevel %04x.\n", trustlevel);
|
1998-01-12 11:18:17 +01:00
|
|
|
*r_trustlevel = trustlevel;
|
|
|
|
return 0;
|
|
|
|
}
|
1998-01-02 21:40:10 +01:00
|
|
|
|
1998-01-13 20:04:23 +01:00
|
|
|
|
1998-01-24 17:32:27 +01:00
|
|
|
|
|
|
|
|
|
|
|
/****************
|
|
|
|
* Enumerate all keys, which are needed to build all trust paths for
|
1998-01-31 22:24:36 +01:00
|
|
|
* the given key. This function does not return the key itself or
|
1998-01-24 17:32:27 +01:00
|
|
|
* the ultimate key.
|
|
|
|
*
|
|
|
|
* 1) create a void pointer and initialize it to NULL
|
|
|
|
* 2) pass this void pointer by reference to this function.
|
|
|
|
* Set lid to the key you want to enumerate and pass it by reference.
|
|
|
|
* 3) call this function as long as it does not return -1
|
|
|
|
* to indicate EOF. LID does contain the next key used to build the web
|
|
|
|
* 4) Always call this function a last time with LID set to NULL,
|
|
|
|
* so that it can free it's context.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
enum_trust_web( void **context, ulong *lid )
|
|
|
|
{
|
|
|
|
ENUM_TRUST_WEB_CONTEXT *c = *context;
|
|
|
|
|
|
|
|
if( !c ) { /* make a new context */
|
|
|
|
c = m_alloc_clear( sizeof *c );
|
|
|
|
*context = c;
|
1998-03-03 09:43:28 +01:00
|
|
|
if( *lid != last_trust_web_key && last_trust_web_key )
|
|
|
|
log_bug("enum_trust_web: nyi\n"); /* <--- FIXME */
|
1998-01-24 17:32:27 +01:00
|
|
|
c->tsl = last_trust_web_tslist;
|
|
|
|
c->index = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !lid ) { /* free the context */
|
|
|
|
m_free( c );
|
|
|
|
*context = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
while( c->tsl ) {
|
|
|
|
if( !c->tsl->dup && c->index < c->tsl->nseg-1 ) {
|
|
|
|
*lid = c->tsl->seg[c->index].lid;
|
|
|
|
c->index++;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
c->index = 1;
|
|
|
|
c->tsl = c->tsl->next;
|
|
|
|
}
|
|
|
|
return -1; /* eof */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/****************
|
|
|
|
* Return the assigned ownertrust value for the given LID
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
get_ownertrust( ulong lid, unsigned *r_otrust )
|
|
|
|
{
|
|
|
|
TRUSTREC rec;
|
|
|
|
|
1998-01-31 22:24:36 +01:00
|
|
|
if( read_record( lid, &rec, RECTYPE_DIR ) ) {
|
|
|
|
log_error("get_ownertrust: read dir record failed\n");
|
|
|
|
return G10ERR_TRUSTDB;
|
|
|
|
}
|
|
|
|
if( read_record( rec.r.dir.keyrec, &rec, RECTYPE_KEY ) ) {
|
|
|
|
log_error("get_ownertrust: read key record failed\n");
|
1998-01-24 17:32:27 +01:00
|
|
|
return G10ERR_TRUSTDB;
|
|
|
|
}
|
|
|
|
if( r_otrust )
|
1998-01-31 22:24:36 +01:00
|
|
|
*r_otrust = rec.r.key.ownertrust;
|
1998-01-24 17:32:27 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
keyid_from_trustdb( ulong lid, u32 *keyid )
|
|
|
|
{
|
|
|
|
TRUSTREC rec;
|
|
|
|
|
1998-01-31 22:24:36 +01:00
|
|
|
if( read_record( lid, &rec, RECTYPE_DIR ) ) {
|
1998-01-24 17:32:27 +01:00
|
|
|
log_error("keyid_from_trustdb: read record failed\n");
|
|
|
|
return G10ERR_TRUSTDB;
|
|
|
|
}
|
|
|
|
if( keyid ) {
|
1998-01-31 22:24:36 +01:00
|
|
|
keyid[0] = rec.r.dir.keyid[0];
|
|
|
|
keyid[1] = rec.r.dir.keyid[1];
|
1998-01-24 17:32:27 +01:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-01-31 22:24:36 +01:00
|
|
|
/****************
|
|
|
|
* This function simply looks for the key in the trustdb
|
|
|
|
* and sets PKC->local_id.
|
|
|
|
* Return: 0 = found
|
|
|
|
* -1 = not found
|
|
|
|
* other = error
|
|
|
|
*/
|
1998-01-16 22:15:24 +01:00
|
|
|
int
|
1998-01-24 17:32:27 +01:00
|
|
|
query_trust_record( PKT_public_cert *pkc )
|
1998-01-16 22:15:24 +01:00
|
|
|
{
|
|
|
|
TRUSTREC rec;
|
1998-01-24 17:32:27 +01:00
|
|
|
int rc=0;
|
1998-01-16 22:15:24 +01:00
|
|
|
|
|
|
|
if( pkc->local_id ) {
|
1998-01-31 22:24:36 +01:00
|
|
|
if( read_record( pkc->local_id, &rec, RECTYPE_DIR ) ) {
|
1998-01-24 17:32:27 +01:00
|
|
|
log_error("query_trust_record: read record failed\n");
|
1998-01-16 22:15:24 +01:00
|
|
|
return G10ERR_TRUSTDB;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else { /* no local_id: scan the trustdb */
|
1998-01-31 22:24:36 +01:00
|
|
|
if( (rc=search_record( pkc, &rec )) && rc != -1 ) {
|
|
|
|
log_error("query_trust_record: search_record failed: %s\n",
|
1998-01-16 22:15:24 +01:00
|
|
|
g10_errstr(rc));
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
}
|
1998-01-31 22:24:36 +01:00
|
|
|
return rc;
|
1998-01-16 22:15:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-01-13 20:04:23 +01:00
|
|
|
/****************
|
|
|
|
* Insert a trust record into the TrustDB
|
|
|
|
* This function failes if this record already exists.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
insert_trust_record( PKT_public_cert *pkc )
|
|
|
|
{
|
|
|
|
TRUSTREC rec;
|
|
|
|
u32 keyid[2];
|
1998-01-31 22:24:36 +01:00
|
|
|
ulong knum, dnum;
|
1998-01-13 20:04:23 +01:00
|
|
|
byte *fingerprint;
|
|
|
|
size_t fingerlen;
|
|
|
|
|
|
|
|
|
1998-01-16 22:15:24 +01:00
|
|
|
if( pkc->local_id )
|
|
|
|
log_bug("pkc->local_id=%lu\n", (ulong)pkc->local_id );
|
1998-01-13 20:04:23 +01:00
|
|
|
|
|
|
|
keyid_from_pkc( pkc, keyid );
|
|
|
|
fingerprint = fingerprint_from_pkc( pkc, &fingerlen );
|
|
|
|
|
|
|
|
/* FIXME: check that we do not have this record. */
|
|
|
|
|
1998-01-31 22:24:36 +01:00
|
|
|
dnum = new_recnum();
|
|
|
|
knum = new_recnum();
|
|
|
|
/* build dir record */
|
1998-01-13 20:04:23 +01:00
|
|
|
memset( &rec, 0, sizeof rec );
|
1998-01-31 22:24:36 +01:00
|
|
|
rec.rectype = RECTYPE_DIR;
|
|
|
|
rec.r.dir.local_id = dnum;
|
|
|
|
rec.r.dir.keyid[0] = keyid[0];
|
|
|
|
rec.r.dir.keyid[1] = keyid[1];
|
|
|
|
rec.r.dir.keyrec = knum;
|
|
|
|
rec.r.dir.no_sigs = 0;
|
|
|
|
if( write_record( dnum, &rec ) ) {
|
|
|
|
log_error("writing dir record failed\n");
|
|
|
|
return G10ERR_TRUSTDB;
|
|
|
|
}
|
|
|
|
/* and the key record */
|
|
|
|
memset( &rec, 0, sizeof rec );
|
|
|
|
rec.rectype = RECTYPE_KEY;
|
|
|
|
rec.r.key.owner = dnum;
|
|
|
|
rec.r.key.keyid[0] = keyid[0];
|
|
|
|
rec.r.key.keyid[1] = keyid[1];
|
|
|
|
rec.r.key.pubkey_algo = pkc->pubkey_algo;
|
|
|
|
memcpy(rec.r.key.fingerprint, fingerprint, fingerlen );
|
|
|
|
rec.r.key.ownertrust = 0;
|
|
|
|
if( write_record( knum, &rec ) ) {
|
|
|
|
log_error("wrinting key record failed\n");
|
1998-01-13 20:04:23 +01:00
|
|
|
return G10ERR_TRUSTDB;
|
|
|
|
}
|
|
|
|
|
1998-01-31 22:24:36 +01:00
|
|
|
pkc->local_id = dnum;
|
1998-01-13 20:04:23 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
1998-01-24 17:32:27 +01:00
|
|
|
update_ownertrust( ulong lid, unsigned new_trust )
|
1998-01-13 20:04:23 +01:00
|
|
|
{
|
|
|
|
TRUSTREC rec;
|
1998-01-31 22:24:36 +01:00
|
|
|
ulong recnum;
|
1998-01-13 20:04:23 +01:00
|
|
|
|
1998-01-31 22:24:36 +01:00
|
|
|
if( read_record( lid, &rec, RECTYPE_DIR ) ) {
|
|
|
|
log_error("update_ownertrust: read dir failed\n");
|
1998-01-13 20:04:23 +01:00
|
|
|
return G10ERR_TRUSTDB;
|
|
|
|
}
|
1998-01-31 22:24:36 +01:00
|
|
|
recnum = rec.r.dir.keyrec;
|
|
|
|
if( read_record( recnum, &rec, RECTYPE_KEY ) ) {
|
|
|
|
log_error("update_ownertrust: read key failed\n");
|
1998-01-24 17:32:27 +01:00
|
|
|
return G10ERR_TRUSTDB;
|
|
|
|
}
|
1998-01-31 22:24:36 +01:00
|
|
|
/* check keyid, fingerprint etc ? */
|
1998-01-13 20:04:23 +01:00
|
|
|
|
1998-01-31 22:24:36 +01:00
|
|
|
rec.r.key.ownertrust = new_trust;
|
|
|
|
if( write_record( recnum, &rec ) ) {
|
1998-01-24 17:32:27 +01:00
|
|
|
log_error("update_ownertrust: write failed\n");
|
1998-01-25 19:56:33 +01:00
|
|
|
return G10ERR_TRUSTDB;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/****************
|
|
|
|
* Kludge to prevent duplicate build_sigrecs() due to an invalid
|
|
|
|
* certificate (no selfsignature or something like this)
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
update_no_sigs( ulong lid, int no_sigs )
|
|
|
|
{
|
|
|
|
TRUSTREC rec;
|
|
|
|
|
1998-01-31 22:24:36 +01:00
|
|
|
if( read_record( lid, &rec, RECTYPE_DIR ) ) {
|
1998-01-25 19:56:33 +01:00
|
|
|
log_error("update_no_sigs: read failed\n");
|
|
|
|
return G10ERR_TRUSTDB;
|
|
|
|
}
|
|
|
|
|
1998-02-02 15:36:06 +01:00
|
|
|
rec.r.dir.no_sigs = no_sigs;
|
1998-01-25 19:56:33 +01:00
|
|
|
if( write_record( lid, &rec ) ) {
|
|
|
|
log_error("update_no_sigs: write failed\n");
|
1998-01-13 20:04:23 +01:00
|
|
|
return G10ERR_TRUSTDB;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
verify_private_data()
|
|
|
|
{
|
|
|
|
int rc = 0;
|
1998-02-24 19:50:46 +01:00
|
|
|
char *sigfile = make_filename(opt.homedir, "gnupg.sig", NULL );
|
1998-01-13 20:04:23 +01:00
|
|
|
|
|
|
|
if( access( sigfile, R_OK ) ) {
|
|
|
|
if( errno != ENOENT ) {
|
|
|
|
log_error("can't access %s: %s\n", sigfile, strerror(errno) );
|
|
|
|
rc = G10ERR_TRUSTDB;
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
log_info("private data signature missing; creating ...\n");
|
|
|
|
rc = sign_private_data();
|
|
|
|
if( rc ) {
|
|
|
|
log_error("error creating %s: %s\n", sigfile, g10_errstr(rc) );
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* FIXME: verify this signature */
|
|
|
|
|
|
|
|
leave:
|
|
|
|
m_free(sigfile);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
sign_private_data()
|
|
|
|
{
|
|
|
|
int rc;
|
1998-02-24 19:50:46 +01:00
|
|
|
char *sigfile = make_filename(opt.homedir, "gnupg.sig", NULL );
|
|
|
|
char *secring = make_filename(opt.homedir, "secring.gpg", NULL );
|
1998-01-13 20:04:23 +01:00
|
|
|
STRLIST list = NULL;
|
|
|
|
|
|
|
|
add_to_strlist( &list, db_name );
|
|
|
|
add_to_strlist( &list, secring );
|
|
|
|
|
|
|
|
rc = sign_file( list, 1, NULL, 0, NULL, sigfile);
|
|
|
|
|
|
|
|
m_free(sigfile);
|
|
|
|
m_free(secring);
|
|
|
|
free_strlist(list);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|