summaryrefslogtreecommitdiff
path: root/lib/opencdk/kbnode.c
blob: e2ebab2f3834bfca8026619c7eaf662d8c2c3eb1 (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
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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
/* kbnode.c -  keyblock node utility functions
 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2007, 2008, 2010
 * Free Software Foundation, Inc.
 *
 * Author: Timo Schulz
 *
 * This file is part of OpenCDK.
 *
 * The OpenCDK library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
 * USA
 *
 */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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


/**
 * cdk_kbnode_new:
 * @pkt: the packet to add
 *
 * Allocates a new key node and adds a 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)
{
  /* Mark the node as clone which means that the packet
     will not be freed, just the node itself. */
  if (node)
    node->is_cloned = 1;
}


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

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


/**
 * cdk_kbnode_delete:
 * @node: the key node
 *
 * Marks the given node as deleted.
 **/
void
cdk_kbnode_delete (cdk_kbnode_t node)
{
  if (node)
    node->is_deleted = 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
 *
 * Inserts @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,
                   cdk_packet_type_t 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
 *
 * Finds 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,
                      cdk_packet_type_t 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, cdk_packet_type_t 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
 *
 * Tries to find the next node with the packettype @pkttype.
 **/
cdk_kbnode_t
cdk_kbnode_find (cdk_kbnode_t node, cdk_packet_type_t 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, cdk_packet_type_t pkttype)
{
  cdk_kbnode_t res;

  res = cdk_kbnode_find (node, pkttype);
  return res ? res->pkt : NULL;
}


/**
 * cdk_kbnode_walk:
 * 
 * Walks 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 * ctx, int all)
{
  cdk_kbnode_t n;

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


/**
 * cdk_kbnode_commit:
 * @root: the nodes
 * 
 * Commits 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 (n->is_deleted)
        {
          if (n == *root)
            *root = nl = n->next;
          else
            nl->next = n->next;
          if (!n->is_cloned)
            cdk_pkt_release (n->pkt);
          cdk_free (n);
          changed = 1;
        }
      else
        nl = n;
    }
  return changed;
}


/**
 * cdk_kbnode_remove:
 * @root: the root node
 * @node: the node to delete
 * 
 * Removes a node from the root node.
 */
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 (!n->is_cloned)
            cdk_pkt_release (n->pkt);
          cdk_free (n);
        }
      else
        nl = n;
    }
}



/**
 * cdk_cdknode_move:
 * @root: root node
 * @node: the node to move
 * @where: destination place where to move the node.
 * 
 * Moves 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;
  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)
        return;
      prev->next = node->next;
      node->next = *root;
      *root = node;
      return;
    }
  if (node == where)            /* Move it after where. */
    return;
  tmp = node->next;
  node->next = where->next;
  where->next = node;
  prev->next = tmp;
}


/**
 * cdk_kbnode_get_packet:
 * @node: the key node
 *
 * Get packet in node.
 *
 * Returns: 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
 *
 * Tries 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;
  cdk_error_t rc;

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

  *ret_node = NULL;
  if (!buflen)
    return CDK_Too_Short;

  rc = cdk_stream_tmp_from_mem (buf, buflen, &inp);
  if (rc)
    return rc;
  rc = cdk_keydb_get_keyblock (inp, ret_node);
  if (rc)
    gnutls_assert ();
  cdk_stream_close (inp);
  return rc;
}


/**
 * cdk_kbnode_write_to_mem_alloc:
 * @node: the key node
 * @r_buf: buffer to hold the raw data
 * @r_buflen: buffer length of the allocated raw data.
 * 
 * The function acts similar to cdk_kbnode_write_to_mem but
 * it allocates the buffer to avoid the lengthy second run.
 */
cdk_error_t
cdk_kbnode_write_to_mem_alloc (cdk_kbnode_t node,
                               byte ** r_buf, size_t * r_buflen)
{
  cdk_kbnode_t n;
  cdk_stream_t s;
  cdk_error_t rc;
  size_t len;

  if (!node || !r_buf || !r_buflen)
    {
      gnutls_assert ();
      return CDK_Inv_Value;
    }

  *r_buf = NULL;
  *r_buflen = 0;

  rc = cdk_stream_tmp_new (&s);
  if (rc)
    {
      gnutls_assert ();
      return rc;
    }

  for (n = node; n; n = n->next)
    {
      /* Skip all packets which cannot occur in a key composition. */
      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 &&
          n->pkt->pkttype != CDK_PKT_ATTRIBUTE)
        continue;
      rc = cdk_pkt_write (s, n->pkt);
      if (rc)
        {
          cdk_stream_close (s);
          gnutls_assert ();
          return rc;
        }
    }

  cdk_stream_seek (s, 0);
  len = cdk_stream_get_length (s);
  *r_buf = cdk_calloc (1, len);
  *r_buflen = cdk_stream_read (s, *r_buf, len);
  cdk_stream_close (s);
  return 0;
}


/**
 * 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.
 *
 * Tries 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.
 * Whenever it is possible, the cdk_kbnode_write_to_mem_alloc should be used.
 **/
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;
  cdk_error_t rc;
  size_t len;

  if (!node || !r_nbytes)
    {
      gnutls_assert ();
      return CDK_Inv_Value;
    }

  rc = cdk_stream_tmp_new (&s);
  if (rc)
    {
      gnutls_assert ();
      return rc;
    }

  for (n = node; n; n = n->next)
    {
      /* Skip all packets which cannot occur in a key composition. */
      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 &&
          n->pkt->pkttype != CDK_PKT_ATTRIBUTE)
        continue;
      rc = cdk_pkt_write (s, n->pkt);
      if (rc)
        {
          cdk_stream_close (s);
          gnutls_assert ();
          return 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 0;
    }
  if (*r_nbytes < len)
    {
      *r_nbytes = len;
      rc = CDK_Too_Short;
    }
  if (!rc)
    *r_nbytes = cdk_stream_read (s, buf, len);
  else
    gnutls_assert ();
  cdk_stream_close (s);
  return rc;
}


/**
 * 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
 *
 * Hashes 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, digest_hd_st * md, int is_v4,
                 cdk_packet_type_t pkttype, int flags)
{
  cdk_packet_t pkt;

  if (!node || !md)
    {
      gnutls_assert ();
      return CDK_Inv_Value;
    }
  if (!pkttype)
    {
      pkt = cdk_kbnode_get_packet (node);
      pkttype = pkt->pkttype;
    }
  else
    {
      pkt = cdk_kbnode_find_packet (node, pkttype);
      if (!pkt)
        {
          gnutls_assert ();
          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:
      gnutls_assert ();
      return CDK_Inv_Mode;
    }
  return 0;
}