summaryrefslogtreecommitdiff
path: root/libextra/opencdk/trustdb.c
blob: cce54183248777df351d35965e3c2f94b496f56c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/* -*- Mode: C; c-file-style: "bsd" -*- */
/* trustdb.c - High level interface for ownertrust handling
 *        Copyright (C) 2001, 2002, 2003 Timo Schulz 
 *
 * This file is part of OpenCDK.
 *
 * OpenCDK is free software; you can redistribute it and/or modify 
 * 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. 
 *  
 * OpenCDK is distributed in the hope that it will be useful, 
 * 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 OpenCDK; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdio.h>

#include "opencdk.h"
#include "main.h"

#define TRUST_MASK      15
#define RECORD_SIZE     40
#define MIN_TRUSTDB_VER  3


enum {
    TDB_RECORD_TRUST = 12,
    TDB_RECORD_VALID = 13,
};

struct tdb_record_s {
    int recno;
    union {
        struct {
            byte reserved;
            byte fpr[20];
            int ownertrust;
            byte depth;
            u32 validlist;
        } trust;
        struct {
            byte reserved;
            byte fpr[20];
            u32 next;
            int valid;
        } valid;
    } u;
};
typedef struct tdb_record_s *tdb_record_t;


static void
trustdb_rec_release( tdb_record_t rec )
{
    if( rec ) {
        rec->recno = 0;
        cdk_free( rec );
    }
}


static tdb_record_t
trustdb_rec_new( void )
{
    tdb_record_t rec;

    rec = cdk_calloc( 1, sizeof *rec );
    if( !rec )
        return NULL;
    return rec;
}


static int
trustdb_check( cdk_stream_t a, int req_ver )
{
    int rc = 0;
    int c = 0, nread;
    byte magic[3];

    cdk_stream_seek( a, 0 );
    c = cdk_stream_getc( a );
    if( c == EOF || c != 1 )
        return CDK_General_Error;
    nread = cdk_stream_read( a, magic, 3 );
    if( nread == EOF )
        return CDK_File_Error;
    c = cdk_stream_getc( a );
    if( c == EOF )
        rc = CDK_General_Error;
    else if( c < req_ver )
        rc = CDK_Wrong_Format;
    return rc;
}


static int
trustdb_rec_parse( cdk_stream_t buf, tdb_record_t r )
{
    size_t n;
    int recno;

    if( !buf || !r )
        return CDK_Inv_Value;

    recno = cdk_stream_getc( buf );
    if( recno == EOF )
        return EOF;

    switch( recno ) {
    case TDB_RECORD_TRUST:	/* trust record: new */
        r->recno = 12;
        r->u.trust.reserved = cdk_stream_getc (buf);
        n = cdk_stream_read (buf, r->u.trust.fpr, 20);
        r->u.trust.ownertrust = cdk_stream_getc (buf);
        r->u.trust.depth = cdk_stream_getc (buf);
        r->u.trust.validlist = 0;
        n = 4;
        while (n--)
            cdk_stream_getc (buf);
        n = RECORD_SIZE - 28;
        while (n--)
            cdk_stream_getc (buf);
        if (r->u.trust.ownertrust == EOF)
            return CDK_EOF;
        break;

    case TDB_RECORD_VALID:	/* valid record: new */
        r->recno = 13;
        r->u.valid.reserved = cdk_stream_getc (buf);
        n = cdk_stream_read (buf, r->u.valid.fpr, 20);
        r->u.valid.valid = cdk_stream_getc (buf);
        r->u.valid.next = 0;
        n = 4;
        while (n--)
            cdk_stream_getc (buf);
        n = RECORD_SIZE - 27;
        while (n--)
            cdk_stream_getc (buf);
        if (r->u.valid.valid == EOF)
            return CDK_EOF;
        break;

    default:
        n = RECORD_SIZE - 1;
        while (n--)
            cdk_stream_getc (buf);
        break;
    }
    r->recno = recno;
    return 0;
}


tdb_record_t
trustdb_rec_byfpr( cdk_stream_t buf, int type,
                   const byte * fpr, size_t fprlen )
{
    tdb_record_t rec;

    if (!fpr || !buf)
        return NULL;

    rec = trustdb_rec_new ();
    if( !rec )
        return NULL;

    while( trustdb_rec_parse( buf, rec ) != EOF ) {
        if( rec->recno != type )
            continue;
        switch( type ) {
	case TDB_RECORD_VALID:
            if( !memcmp( fpr, rec->u.valid.fpr, fprlen ) )
                return rec;
            break;

	case TDB_RECORD_TRUST:
            if( !memcmp( rec->u.trust.fpr, fpr, fprlen ) )
                return rec;
            break;
	}
    }
    trustdb_rec_release ( rec );
    rec = NULL;
    return rec;
}


int
cdk_trustdb_get_ownertrust( cdk_stream_t inp, cdk_pkt_pubkey_t pk,
			    int *r_val, int *r_flags )
{
    tdb_record_t rec = NULL;
    byte fpr[20];
    int flags = 0;
    int rc;

    if( !inp || !r_val || !r_flags || !pk )
        return CDK_Inv_Value;

    rc = trustdb_check( inp, MIN_TRUSTDB_VER );
    if( rc )
        return rc;
  
    *r_val = CDK_TRUST_UNKNOWN;
    cdk_pk_get_fingerprint( pk, fpr );
    cdk_stream_seek( inp, 0 );

    rec = trustdb_rec_byfpr( inp, TDB_RECORD_TRUST, fpr, 20 );
    if( rec ) {
        *r_val = rec->u.trust.ownertrust & TRUST_MASK;
        if( *r_val & CDK_TFLAG_REVOKED )
            flags |= CDK_TFLAG_REVOKED;
        if( *r_val & CDK_TFLAG_SUB_REVOKED )
            flags |= CDK_TFLAG_SUB_REVOKED;
        if( *r_val & CDK_TFLAG_DISABLED )
            flags |= CDK_TFLAG_DISABLED;
        *r_flags = flags;
        rc = 0;
    }
    trustdb_rec_release( rec );
    return rc;
}


int
cdk_trustdb_get_validity( cdk_stream_t inp, cdk_pkt_userid_t id, int *r_val )
{
    cdk_md_hd_t rmd;
    tdb_record_t rec;
    byte * fpr;
    int rc;

    if( !inp || !r_val || !id )
        return CDK_Inv_Value;

    rc = trustdb_check( inp, MIN_TRUSTDB_VER );
    if( rc )
        return rc;
  
    *r_val = CDK_TRUST_UNKNOWN;
    rmd = cdk_md_open( CDK_MD_RMD160, 0 );
    if( !rmd )
        return CDK_Gcry_Error;

    cdk_md_write( rmd, id->name, id->len );
    cdk_md_final( rmd );
    fpr = cdk_md_read( rmd, CDK_MD_RMD160 );

    cdk_stream_seek( inp, 0 );
    rec = trustdb_rec_byfpr( inp, TDB_RECORD_VALID, fpr, 20 );
    if( rec ) {
        *r_val = rec->u.valid.valid;
        rc = 0;
    }
    
    trustdb_rec_release( rec );
    cdk_md_close( rmd );
    return rc;
}