File: kbnode.c

package info (click to toggle)
gnutls13 1.4.4-3%2Betch5
  • links: PTS
  • area: main
  • in suites: etch
  • size: 24,052 kB
  • ctags: 12,507
  • sloc: ansic: 94,474; xml: 17,016; sh: 9,652; perl: 628; makefile: 607; sed: 16
file content (572 lines) | stat: -rw-r--r-- 14,463 bytes parent folder | download | duplicates (3)
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
/* -*- Mode: C; c-file-style: "bsd" -*-
 * kbnode.c -  keyblock node utility functions
 *        Copyright (C) 1998, 1999, 2000, 2001, 2006 Free Software Foundation, Inc.
 *        Copyright (C) 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 */
/* X-TODO-STATUS: OK */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

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

#define is_deleted_kbnode(a)  ((a)->private_flag & 1)
#define is_cloned_kbnode(a)   ((a)->private_flag & 2)


/**
 * cdk_kbnode_new:
 * @pkt: the packet to add
 *
 * Allocate a new key node and add the packet.
 **/
cdk_kbnode_t
cdk_kbnode_new( cdk_packet_t pkt )
{
    cdk_kbnode_t n;
    n = cdk_calloc( 1, sizeof * n );
    if( !n )
        return NULL;
    n->pkt = pkt;
    return n;
}


void
_cdk_kbnode_clone( cdk_kbnode_t node )
{
    if( node )
        node->private_flag |= 2; /* mark cloned */
}


/**
 * cdk_kbnode_release:
 * @n: the key node
 *
 * Release the memory of the node.
 **/
void
cdk_kbnode_release( cdk_kbnode_t node )
{
    cdk_kbnode_t n2;

    while( node ) {
        n2 = node->next;
        node->pkt->pkttype = 0;
        if( !is_cloned_kbnode( node ) )
            cdk_pkt_release( node->pkt );
        cdk_free( node );
        node = n2;
    }
}


/**
 * cdk_kbnode_delete:
 * @node: the ke keynode.
 *
 * Delete @node.
 **/
void
cdk_kbnode_delete( cdk_kbnode_t node )
{
    if( node )
        node->private_flag |= 1;
}


/****************
 * Append NODE to ROOT.  ROOT must exist!
 */
void
_cdk_kbnode_add( cdk_kbnode_t root, cdk_kbnode_t node )
{
    cdk_kbnode_t n1;

    for( n1 = root; n1->next; n1 = n1->next )
        ;
    n1->next = node;
}


/**
 * cdk_kbnode_insert:
 * @root: the root key node
 * @node: the node to add
 * @pkttype: packet type
 *
 * Insert @node into the list after @root but before a packet which is not of
 * type @pkttype (only if @pkttype != 0).
 **/
void
cdk_kbnode_insert( cdk_kbnode_t root, cdk_kbnode_t node, int pkttype )
{
    if( !pkttype ) {
        node->next = root->next;
        root->next = node;
    }
    else {
        cdk_kbnode_t n1;
        for( n1 = root; n1->next; n1 = n1->next )
            if( pkttype != n1->next->pkt->pkttype ) {
                node->next = n1->next;
                n1->next = node;
                return;
            }
        /* no such packet, append */
        node->next = NULL;
        n1->next = node;
    }
}


/**
 * cdk_kbnode_find_prev:
 * @root: the root key node
 * @node: the key node
 * @pkttype: packet type
 *
 * Find the previous node (if @pkttype = 0) or the previous node
 * with pkttype @pkttype in the list starting with @root of @node.
 **/
cdk_kbnode_t
cdk_kbnode_find_prev( cdk_kbnode_t root, cdk_kbnode_t node, int pkttype )
{
    cdk_kbnode_t n1;

    for( n1 = NULL; root && root != node; root = root->next ) {
        if( !pkttype || root->pkt->pkttype == pkttype )
            n1 = root;
    }
    return n1;
}


/**
 * cdk_kbnode_find_next:
 * @node: the key node
 * @pkttype: packet type
 *
 * Ditto, but find the next packet.  The behaviour is trivial if
 * @pkttype is 0 but if it is specified, the next node with a packet
 * of this type is returned.  The function has some knowledge about
 * the valid ordering of packets: e.g. if the next signature packet
 * is requested, the function will not return one if it encounters
 * a user-id.
 **/
cdk_kbnode_t
cdk_kbnode_find_next( cdk_kbnode_t node, int pkttype )
{
    for( node = node->next; node; node = node->next ) {
        if( !pkttype )
            return node;
        else if( pkttype == CDK_PKT_USER_ID
                 && (node->pkt->pkttype == CDK_PKT_PUBLIC_KEY
                     || node->pkt->pkttype == CDK_PKT_SECRET_KEY))
            return NULL;
        else if (pkttype == CDK_PKT_SIGNATURE
                 && (node->pkt->pkttype == CDK_PKT_USER_ID
                     || node->pkt->pkttype == CDK_PKT_PUBLIC_KEY
                     || node->pkt->pkttype == CDK_PKT_SECRET_KEY))
            return NULL;
        else if (node->pkt->pkttype == pkttype)
            return node;
    }
    return NULL;
}


/**
 * cdk_kbnode_find:
 * @node: the key node
 * @pkttype: packet type
 *
 * Try to find the next node with the packettype @pkttype.
 **/
cdk_kbnode_t
cdk_kbnode_find( cdk_kbnode_t node, int pkttype )
{
    for( ; node; node = node->next ) {
        if( node->pkt->pkttype == pkttype )
            return node;
    }
    return NULL;
}


/**
 * cdk_kbnode_find_packet:
 * @node: the key node
 * @pkttype: packet type
 *
 * Same as cdk_kbnode_find but it returns the packet instead of the node.
 **/
cdk_packet_t
cdk_kbnode_find_packet( cdk_kbnode_t node, int pkttype )
{
    cdk_kbnode_t res;
  
    res = cdk_kbnode_find( node, pkttype );
    if( res )
        return res->pkt;
    return NULL;
}


/****************
 * Walk through a list of kbnodes. This function returns
 * the next kbnode for each call; before using the function the first
 * time, the caller must set CONTEXT to NULL (This has simply the effect
 * to start with ROOT).
 */
cdk_kbnode_t
cdk_kbnode_walk( cdk_kbnode_t root, cdk_kbnode_t * context, int all )
{
    cdk_kbnode_t n;

    do {
        if( !*context ) {
            *context = root;
            n = root;
	}
        else {
            n = (*context)->next;
            *context = n;
	}
    }
    while( !all && n && is_deleted_kbnode( n ) );
    return n;
}


/****************
 * Commit changes made to the kblist at ROOT. Note that ROOT my change,
 * and it is therefore passed by reference.
 * The function has the effect of removing all nodes marked as deleted.
 * returns true if any node has been changed
 */
int
cdk_kbnode_commit( cdk_kbnode_t * root )
{
    cdk_kbnode_t n, nl;
    int changed = 0;

    for( n = *root, nl = NULL; n; n = nl->next ) {
        if (is_deleted_kbnode (n)) {
            if( n == *root )
                *root = nl = n->next;
            else
                nl->next = n->next;
            if( !is_cloned_kbnode( n ) ) {
                cdk_pkt_release( n->pkt );
                cdk_free( n->pkt );
	    }
            cdk_free( n );
            changed = 1;
	}
        else
            nl = n;
    }
    return changed;
}


void
cdk_kbnode_remove( cdk_kbnode_t * root, cdk_kbnode_t node )
{
    cdk_kbnode_t n, nl;

    for( n = *root, nl = NULL; n; n = nl->next ) {
        if( n == node ) {
            if( n == *root )
                *root = nl = n->next;
            else
                nl->next = n->next;
            if( !is_cloned_kbnode( n ) ) {
                cdk_pkt_release( n->pkt );
                cdk_free( n->pkt);
	    }
            cdk_free( n );
	}
        else
            nl = n;
    }
}


/****************
 * Move NODE behind right after WHERE or to the beginning if WHERE is NULL.
 */
void
cdk_kbnode_move (cdk_kbnode_t * root, cdk_kbnode_t node, cdk_kbnode_t where)
{
    cdk_kbnode_t tmp, prev;

    if (!root || !*root || !node)
        return;			/* sanity check */
    for (prev = *root; prev && prev->next != node; prev = prev->next)
        ;
    if (!prev)
        return;			/* node is not in the list */

    if (!where) {		/* move node before root */
        if (node == *root)	/* move to itself */
            return;
        prev->next = node->next;
        node->next = *root;
        *root = node;
        return;
    }
    /* move it after where */
    if (node == where)
        return;
    tmp = node->next;
    node->next = where->next;
    where->next = node;
    prev->next = tmp;
}


/**
 * cdk_kbnode_get_packet:
 * @node: the key node
 *
 * Return the packet which is stored inside the node in @node.
 **/
cdk_packet_t
cdk_kbnode_get_packet( cdk_kbnode_t node )
{
    if( node )
        return node->pkt;
    return NULL;
}


/**
 * cdk_kbnode_read_from_mem:
 * @ret_node: the new key node
 * @buf: the buffer which stores the key sequence
 * @buflen: the length of the buffer
 *
 * Try to read a key node from the memory buffer @buf.
 **/
cdk_error_t
cdk_kbnode_read_from_mem( cdk_kbnode_t * ret_node,
                          const byte * buf, size_t buflen )
{
    cdk_stream_t inp;
    int rc;

    if( !buflen || !ret_node )
        return CDK_Inv_Value;

    *ret_node = NULL;
    inp = cdk_stream_tmp_from_mem( buf, buflen );
    if( !inp )
        return CDK_Out_Of_Core;
    rc = cdk_keydb_get_keyblock( inp, ret_node );
    if( rc == CDK_EOF && *ret_node )
        rc = 0;
    cdk_stream_close( inp );
    return rc;
}


/**
 * cdk_kbnode_write_to_mem:
 * @node: the key node
 * @buf: the buffer to store the node data
 * @r_nbytes: the new length of the buffer.
 *
 * Try to write the contents of the key node to the buffer @buf and
 * return the length of it in @r_nbytes. If buf is zero, only the
 * length of the node is calculated and returned in @r_nbytes.
 **/
cdk_error_t
cdk_kbnode_write_to_mem( cdk_kbnode_t node, byte * buf, size_t * r_nbytes )
{
    cdk_kbnode_t n;
    cdk_stream_t s;
    int rc = 0, len;

    if( !node )
        return CDK_Inv_Value;
    
    s = cdk_stream_tmp( );
    if( !s )
        return CDK_Out_Of_Core;
  
    for( n = node; n; n = n->next ) {
        if( n->pkt->pkttype != CDK_PKT_PUBLIC_KEY
            && n->pkt->pkttype != CDK_PKT_PUBLIC_SUBKEY
            && n->pkt->pkttype != CDK_PKT_SECRET_KEY
            && n->pkt->pkttype != CDK_PKT_SECRET_SUBKEY
            && n->pkt->pkttype != CDK_PKT_SIGNATURE
            && n->pkt->pkttype != CDK_PKT_USER_ID )
            continue;
        rc = cdk_pkt_write( s, n->pkt );
        if( rc )
            break;
    }
    if( !rc ) {
        cdk_stream_seek( s, 0 );
        len = cdk_stream_get_length( s );
        if( !buf ) {
            *r_nbytes = len; /* only return the length of the buffer */
            cdk_stream_close( s );
            return CDK_Too_Short;
        }
        if( *r_nbytes < len )
            rc = CDK_Too_Short;
        if( !rc )
            *r_nbytes = cdk_stream_read( s, buf, len );
    }
    cdk_stream_close( s );
    return rc;
}


/**
 * cdk_kbnode_get_attr:
 * @node: the key node
 * @pkttype: the packet type which the attribute should be retrieved from
 * @attr: the attribute to retrive
 *
 * Extract a single attribute from the specified packet type. If no
 * packet type is given, it is assumed that the public key is meant.
 * If the attr was found, it is returned as a pointer which can be cast
 * to a proper type.
 **/
void *
cdk_kbnode_get_attr( cdk_kbnode_t node, int pkttype, int attr )
{
    cdk_packet_t pkt;
    cdk_pkt_pubkey_t pk;
    cdk_pkt_userid_t id;
    cdk_pkt_signature_t sig;
    
    if( !node || !attr )
        return NULL;
    if( !pkttype )
        pkttype = CDK_PKT_PUBLIC_KEY;
    pkt = cdk_kbnode_find_packet( node, pkttype );
    if( !pkt )
        return NULL;
    switch( pkttype ) {
    case CDK_PKT_SECRET_KEY:
    case CDK_PKT_PUBLIC_KEY:
        if( pkttype == CDK_PKT_PUBLIC_KEY )
            pk = pkt->pkt.public_key;
        else
            pk = pkt->pkt.secret_key->pk;
        assert( pk );
        switch( attr ) {
        case CDK_ATTR_CREATED: return (long *)pk->timestamp;
        case CDK_ATTR_EXPIRE : return (long *)pk->expiredate;
        case CDK_ATTR_VERSION: return (byte *)pk->version;
        case CDK_ATTR_LEN    : return (long *)cdk_pk_get_nbits( pk );
        case CDK_ATTR_KEYID:
            if( !pk->keyid[0] || !pk->keyid[1] )
                cdk_pk_get_keyid( pk, pk->keyid );
            return pk->keyid;
        case CDK_ATTR_FPR:
            if( !pk->fpr[0] )
                cdk_pk_get_fingerprint( pk, pk->fpr );
            return pk->fpr;
        case CDK_ATTR_ALGO_PK: return (byte *)pk->pubkey_algo;
        default: return NULL;
        }
        break;

    case CDK_PKT_USER_ID:
        id = pkt->pkt.user_id;
        switch( attr ) {
        case CDK_ATTR_LEN : return (long *)id->len;
        case CDK_ATTR_NAME: return id->name;
        default: return NULL;
        }
        break;

    case CDK_PKT_SIGNATURE:
        sig = pkt->pkt.signature;
        switch( attr ) {
        case CDK_ATTR_ALGO_MD: return (byte *)sig->digest_algo;
        case CDK_ATTR_ALGO_PK: return (byte *)sig->pubkey_algo;
        case CDK_ATTR_VERSION: return (byte *)sig->version;
        case CDK_ATTR_KEYID  : return (u32 *)cdk_sig_get_keyid( sig, NULL );
        default: return NULL;
        }
        break;

    default:
        return NULL;
    }
    return NULL;
}


/**
 * cdk_kbnode_hash:
 * @node: the key node
 * @hashctx: opaque pointer to the hash context
 * @is_v4: OpenPGP signature (yes=1, no=0)
 * @pkttype: packet type to hash (if zero use the packet type from the node)
 * @flags: flags which depend on the operation
 *
 * Hash the key node contents. Two modes are supported. If the packet
 * type is used (!= 0) then the function searches the first node with
 * this type. Otherwise the node is seen as a single node and the type
 * is extracted from it.
 **/
cdk_error_t
cdk_kbnode_hash( cdk_kbnode_t node, cdk_md_hd_t md, int is_v4,
                 int pkttype, int flags )
{
    cdk_packet_t pkt;

    if( !node || !md )
        return CDK_Inv_Value;
    if( !pkttype )
        pkttype = node->pkt->pkttype;
    pkt = cdk_kbnode_find_packet( node, pkttype );
    if( !pkt )
        return CDK_Inv_Packet;
    switch( pkttype ) {
    case CDK_PKT_PUBLIC_KEY:
    case CDK_PKT_PUBLIC_SUBKEY:
        _cdk_hash_pubkey( pkt->pkt.public_key, md, flags & 1 ); break;
    case CDK_PKT_USER_ID:
        _cdk_hash_userid( pkt->pkt.user_id, is_v4, md ); break;
    case CDK_PKT_SIGNATURE:
        _cdk_hash_sig_data( pkt->pkt.signature, md ); break;
    default:
        return CDK_Inv_Mode;
    }
    return 0;
}