summaryrefslogtreecommitdiff
path: root/lib/opencdk/new-packet.c
blob: 5bd452e3e23936b3cf650e585e4cd3a0746ed1cf (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
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
/* new-packet.c - packet handling (freeing, copying, ...)
 * Copyright (C) 2001, 2002, 2003, 2007, 2008 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 <string.h>
#include <stdio.h>
#include <assert.h>

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


/* Release an array of MPI values. */
void
_cdk_free_mpibuf (size_t n, gcry_mpi_t *array)
{
  while (n--) 
    {
      gcry_mpi_release (array[n]);
      array[n] = NULL;
    }
}


/**
 * cdk_pkt_new:
 * @r_pkt: the new packet
 * 
 * Allocate a new packet.
 **/
cdk_error_t
cdk_pkt_new (cdk_packet_t *r_pkt)
{
  cdk_packet_t pkt;
  
  if (!r_pkt)
    return CDK_Inv_Value;
  pkt = cdk_calloc (1, sizeof *pkt);
  if (!pkt)
    return CDK_Out_Of_Core;
  *r_pkt = pkt;
  return 0;
}


static void
free_symkey_enc (cdk_pkt_symkey_enc_t enc)
{
  if (!enc)
    return;
  cdk_s2k_free (enc->s2k);
  cdk_free (enc);
}


static void
free_pubkey_enc (cdk_pkt_pubkey_enc_t enc)
{
  size_t nenc;
  
  if (!enc)
    return;
  
  nenc = cdk_pk_get_nenc (enc->pubkey_algo);
  _cdk_free_mpibuf (nenc, enc->mpi);
  cdk_free (enc);
}


static void
free_literal (cdk_pkt_literal_t pt)
{
  if (!pt)
    return;
  /* The buffer which is referenced in this packet is closed
     elsewhere. To close it here would cause a double close. */
  cdk_free (pt);
}


void
_cdk_free_userid (cdk_pkt_userid_t uid)
{
  if (!uid)
    return;
  
  cdk_free (uid->prefs);
  uid->prefs = NULL;
  cdk_free (uid->attrib_img);
  uid->attrib_img = NULL;      
  cdk_free (uid);
}


void
_cdk_free_signature (cdk_pkt_signature_t sig)
{
  cdk_desig_revoker_t r;
  size_t nsig;
  
  if (!sig)
    return;
  
  nsig = cdk_pk_get_nsig (sig->pubkey_algo);
  _cdk_free_mpibuf (nsig, sig->mpi);

  cdk_subpkt_free (sig->hashed);
  sig->hashed = NULL;
  cdk_subpkt_free (sig->unhashed);
  sig->unhashed = NULL;
  while (sig->revkeys)
    {
      r = sig->revkeys->next;
      cdk_free (sig->revkeys);
      sig->revkeys = r;
    }
  cdk_free (sig);
}


void
cdk_pk_release (cdk_pubkey_t pk)
{
  size_t npkey;
  
  if (!pk)
    return;
  
  npkey = cdk_pk_get_npkey (pk->pubkey_algo);
  _cdk_free_userid (pk->uid);
  pk->uid = NULL;
  cdk_free (pk->prefs);
  pk->prefs = NULL;
  _cdk_free_mpibuf (npkey, pk->mpi);
  cdk_free (pk);
}


void
cdk_sk_release (cdk_seckey_t sk)
{
  size_t nskey;
  
  if (!sk)
    return;
  
  nskey = cdk_pk_get_nskey (sk->pubkey_algo);
  _cdk_free_mpibuf (nskey, sk->mpi);
  cdk_free (sk->encdata);
  sk->encdata = NULL;
  cdk_pk_release (sk->pk);
  sk->pk = NULL;
  cdk_s2k_free (sk->protect.s2k);
  sk->protect.s2k = NULL;
  cdk_free (sk);
}


static void
free_encrypted (cdk_pkt_encrypted_t enc)
{
  if (!enc)
    return;
  
  /* This is just a reference for the filters to know where
     the encrypted data starts and to read from the sream. It
     us closed elsewhere and to close it here would double close it. */
  /*cdk_stream_close (enc->buf);*/
  enc->buf = NULL;
  cdk_free (enc);
}


/* Detach the openpgp packet from the packet structure
   and release the packet structure itself. */
void
_cdk_pkt_detach_free (cdk_packet_t pkt, int *r_pkttype, void **ctx)
{
  /* For now we just allow this for keys. */
  switch (pkt->pkttype)
    {
    case CDK_PKT_PUBLIC_KEY:
    case CDK_PKT_PUBLIC_SUBKEY:
      *ctx = pkt->pkt.public_key;
      break;
      
    case CDK_PKT_SECRET_KEY:
    case CDK_PKT_SECRET_SUBKEY:
      *ctx = pkt->pkt.secret_key;
      break;
      
    default:
      *r_pkttype = 0;
      return;
    }
  
  /* The caller might expect a specific packet type and
     is not interested to store it for later use. */
  if (r_pkttype)
    *r_pkttype = pkt->pkttype;
  
  cdk_free (pkt);
}


void
cdk_pkt_free (cdk_packet_t pkt)
{
  if (!pkt)
    return;
  
  switch (pkt->pkttype) 
    {
    case CDK_PKT_ATTRIBUTE    :
    case CDK_PKT_USER_ID      : _cdk_free_userid (pkt->pkt.user_id); break;
    case CDK_PKT_PUBLIC_KEY   :
    case CDK_PKT_PUBLIC_SUBKEY: cdk_pk_release (pkt->pkt.public_key); break;
    case CDK_PKT_SECRET_KEY   :
    case CDK_PKT_SECRET_SUBKEY: cdk_sk_release (pkt->pkt.secret_key); break;
    case CDK_PKT_SIGNATURE    : _cdk_free_signature (pkt->pkt.signature);break;
    case CDK_PKT_PUBKEY_ENC   : free_pubkey_enc (pkt->pkt.pubkey_enc); break;
    case CDK_PKT_SYMKEY_ENC   : free_symkey_enc (pkt->pkt.symkey_enc); break;
    case CDK_PKT_MDC          : cdk_free (pkt->pkt.mdc); break;
    case CDK_PKT_ENCRYPTED    :
    case CDK_PKT_ENCRYPTED_MDC: free_encrypted (pkt->pkt.encrypted); break;
    case CDK_PKT_ONEPASS_SIG  : cdk_free (pkt->pkt.onepass_sig); break;
    case CDK_PKT_LITERAL      : free_literal (pkt->pkt.literal); break;
    case CDK_PKT_COMPRESSED   : cdk_free (pkt->pkt.compressed); break;
    default                   : break;
    }
  
  /* Reset the packet type to avoid, when cdk_pkt_release() will be
     used, that the second cdk_pkt_free() call will double free the data. */
  pkt->pkttype = 0;
}


/**
 * cdk_pkt_release:
 * @pkt: the packet
 * 
 * Free the contents of the given package and
 * release the memory of the structure.
 **/
void
cdk_pkt_release (cdk_packet_t pkt)
{
  if (!pkt)
    return;
  cdk_pkt_free (pkt);
  cdk_free (pkt);
}


/**
 * cdk_pkt_alloc:
 * @r_pkt: output is the new packet
 * @pkttype: the requested packet type
 * 
 * Allocate a new packet structure with the given packet type.
 **/
cdk_error_t
cdk_pkt_alloc (cdk_packet_t *r_pkt, int pkttype)
{
  cdk_packet_t pkt;
  int rc;

  if (!r_pkt)
    return CDK_Inv_Value;
  
  rc = cdk_pkt_new (&pkt);
  if (rc)
    return rc;

  switch (pkttype) 
    {
    case CDK_PKT_USER_ID:
      pkt->pkt.user_id = cdk_calloc (1, sizeof pkt->pkt.user_id);
      if (!pkt->pkt.user_id)
	return CDK_Out_Of_Core;
      break;
      
    case CDK_PKT_PUBLIC_KEY:
    case CDK_PKT_PUBLIC_SUBKEY:
      pkt->pkt.public_key = cdk_calloc (1, sizeof *pkt->pkt.public_key);
      if (!pkt->pkt.public_key)
	return CDK_Out_Of_Core;
      break;
      
    case CDK_PKT_SECRET_KEY:
    case CDK_PKT_SECRET_SUBKEY:
      pkt->pkt.secret_key = cdk_calloc (1, sizeof *pkt->pkt.secret_key);
      pkt->pkt.secret_key->pk =
	cdk_calloc (1, sizeof *pkt->pkt.secret_key->pk);
      if (!pkt->pkt.secret_key || !pkt->pkt.secret_key->pk)
	return CDK_Out_Of_Core;
      break;
      
    case CDK_PKT_SIGNATURE:
      pkt->pkt.signature = cdk_calloc (1, sizeof *pkt->pkt.signature);
      if (!pkt->pkt.signature)
	return CDK_Out_Of_Core;
      break;
      
    case CDK_PKT_SYMKEY_ENC:
      pkt->pkt.symkey_enc = cdk_calloc (1, sizeof *pkt->pkt.symkey_enc);
      if (!pkt->pkt.symkey_enc)
	return CDK_Out_Of_Core;
      break;
      
    case CDK_PKT_PUBKEY_ENC:
      pkt->pkt.pubkey_enc = cdk_calloc (1, sizeof *pkt->pkt.pubkey_enc);
      if (!pkt->pkt.pubkey_enc)
	return CDK_Out_Of_Core;
      break;
      
    case CDK_PKT_MDC:
      pkt->pkt.mdc = cdk_calloc (1, sizeof *pkt->pkt.mdc);
      if (!pkt->pkt.mdc)
	return CDK_Out_Of_Core;
      break;
      
    case CDK_PKT_ENCRYPTED_MDC:
    case CDK_PKT_ENCRYPTED:
      pkt->pkt.symkey_enc = cdk_calloc (1, sizeof *pkt->pkt.symkey_enc);
      if (!pkt->pkt.symkey_enc)
	return CDK_Out_Of_Core;
      break;
      
    case CDK_PKT_ONEPASS_SIG:
      pkt->pkt.onepass_sig = cdk_calloc (1, sizeof *pkt->pkt.onepass_sig);
      if (!pkt->pkt.onepass_sig)
	return CDK_Out_Of_Core;
      break;
      
    case CDK_PKT_LITERAL:
      /* FIXME: We would need the size of the file name to allocate extra
                bytes, otherwise the result would be useless. */
      pkt->pkt.literal = cdk_calloc (1, sizeof *pkt->pkt.literal);
      if (!pkt->pkt.literal)
	return CDK_Out_Of_Core;
      break;
    }
  pkt->pkttype = pkttype;
  *r_pkt = pkt;
  return 0;
}


cdk_prefitem_t
_cdk_copy_prefs (const cdk_prefitem_t prefs)
{
  size_t n = 0;
  struct cdk_prefitem_s *new_prefs;
  
  if (!prefs)
    return NULL;
  
  for (n = 0; prefs[n].type; n++)
    ;
  new_prefs = cdk_calloc (1, sizeof *new_prefs * (n + 1));
  if (!new_prefs)
    return NULL;
  for (n = 0; prefs[n].type; n++) 
    {    
      new_prefs[n].type = prefs[n].type;
      new_prefs[n].value = prefs[n].value;
    }
  new_prefs[n].type = CDK_PREFTYPE_NONE;
  new_prefs[n].value = 0;  
  return new_prefs;
}


cdk_error_t
_cdk_copy_userid (cdk_pkt_userid_t* dst, cdk_pkt_userid_t src)
{
  cdk_pkt_userid_t u;
  
  if (!dst || !src)
    return CDK_Inv_Value;
  
  *dst = NULL;
  u = cdk_calloc (1, sizeof *u + strlen (src->name) + 1);
  if (!u)
    return CDK_Out_Of_Core;
  memcpy (u, src, sizeof *u);
  memcpy (u->name, src->name, strlen (src->name));
  u->prefs = _cdk_copy_prefs (src->prefs);
  if (src->selfsig)
    _cdk_copy_signature (&u->selfsig, src->selfsig);
  *dst = u;
  
  return 0;
}


cdk_error_t
_cdk_copy_pubkey (cdk_pkt_pubkey_t* dst, cdk_pkt_pubkey_t src)
{
  cdk_pkt_pubkey_t k;
  int i;
  
  if (!dst || !src)
    return CDK_Inv_Value;
  
  *dst = NULL;
  k = cdk_calloc (1, sizeof *k);
  if (!k)
    return CDK_Out_Of_Core;
  memcpy (k, src, sizeof *k);
  if (src->uid)
    _cdk_copy_userid (&k->uid, src->uid);
  if (src->prefs)
    k->prefs = _cdk_copy_prefs (src->prefs);
  for (i = 0; i < cdk_pk_get_npkey (src->pubkey_algo); i++)
    k->mpi[i] = gcry_mpi_copy (src->mpi[i]);
  *dst = k;
  
  return 0;
}


cdk_error_t
_cdk_copy_seckey (cdk_pkt_seckey_t* dst, cdk_pkt_seckey_t src)
{
  cdk_pkt_seckey_t k;
  int i;
  
  if (!dst || !src)
    return CDK_Inv_Value;
  
  *dst = NULL;
  k = cdk_calloc (1, sizeof *k);
  if (!k)
    return CDK_Out_Of_Core;
  memcpy (k, src, sizeof *k);
  _cdk_copy_pubkey (&k->pk, src->pk);
  
  if (src->encdata) 
    {
      k->encdata = cdk_calloc (1, src->enclen + 1);
      if (!k->encdata)
	return CDK_Out_Of_Core;
      memcpy (k->encdata, src->encdata, src->enclen);
    }
  
  _cdk_s2k_copy (&k->protect.s2k, src->protect.s2k);
  
  for (i = 0; i < cdk_pk_get_nskey (src->pubkey_algo); i++) 
    {
      k->mpi[i] = gcry_mpi_copy (src->mpi[i]);
      gcry_mpi_set_flag (k->mpi[i], GCRYMPI_FLAG_SECURE);
    }
  
  *dst = k;  
  return 0;
}


cdk_error_t
_cdk_copy_pk_to_sk (cdk_pkt_pubkey_t pk, cdk_pkt_seckey_t sk)
{
  if (!pk || !sk)
    return CDK_Inv_Value;
  
  sk->version = pk->version;
  sk->expiredate = pk->expiredate;
  sk->pubkey_algo = pk->pubkey_algo;
  sk->has_expired = pk->has_expired;
  sk->is_revoked = pk->is_revoked;
  sk->main_keyid[0] = pk->main_keyid[0];
  sk->main_keyid[1] = pk->main_keyid[1];
  sk->keyid[0] = pk->keyid[0];
  sk->keyid[1] = pk->keyid[1];
  
  return 0;
}


cdk_error_t
_cdk_copy_signature (cdk_pkt_signature_t * dst, cdk_pkt_signature_t src)
{
  cdk_pkt_signature_t s;
  
  if (!dst || !src)
    return CDK_Inv_Value;

  *dst = NULL;
  s = cdk_calloc (1, sizeof *s);
  if (!s)
    return CDK_Out_Of_Core;
  memcpy (s, src, sizeof *src);
  _cdk_subpkt_copy (&s->hashed, src->hashed);
  _cdk_subpkt_copy (&s->unhashed, src->unhashed);
  /* FIXME: Copy MPI parts */
  *dst = s;
  
  return 0;
}


cdk_error_t
_cdk_pubkey_compare (cdk_pkt_pubkey_t a, cdk_pkt_pubkey_t b)
{
  int na, nb, i;
  
  if (a->timestamp != b->timestamp || a->pubkey_algo != b->pubkey_algo)
    return -1;
  if (a->version < 4 && a->expiredate != b->expiredate)
    return -1;
  na = cdk_pk_get_npkey (a->pubkey_algo);
  nb = cdk_pk_get_npkey (b->pubkey_algo);
  if (na != nb)
    return -1;
  
  for (i = 0; i < na; i++) 
    {
      if (gcry_mpi_cmp (a->mpi[i], b->mpi[i]))
	return -1;
    }
  
  return 0;
}


/**
 * cdk_subpkt_free:
 * @ctx: the sub packet node to free
 *
 * Release the context.
 **/
void
cdk_subpkt_free (cdk_subpkt_t ctx)
{
  cdk_subpkt_t s;
  
  while (ctx)
    {
      s = ctx->next;
      cdk_free (ctx);
      ctx = s;
    }
}


/**
 * cdk_subpkt_find:
 * @ctx: the sub packet node
 * @type: the packet type to find
 *
 * Find the given packet type in the node. If no packet with this
 * type was found, return null otherwise pointer to the node.
 **/
cdk_subpkt_t
cdk_subpkt_find (cdk_subpkt_t ctx, size_t type)
{
  return cdk_subpkt_find_nth (ctx, type, 0);
}

/**
 * cdk_subpkt_type_count:
 * @ctx: The sub packet context
 * @type: The sub packet type.
 * 
 * Return the amount of sub packets with this type.
 **/
size_t
cdk_subpkt_type_count (cdk_subpkt_t ctx, size_t type)
{
  cdk_subpkt_t s;
  size_t count;
  
  count = 0;
  for (s = ctx; s; s = s->next)
    {
      if (s->type == type)
	count++;
    }
  
  return count;
}


/**
 * cdk_subpkt_find_nth:
 * @ctx: The sub packet context
 * @type: The sub packet type
 * @index: The nth packet to retrieve, 0 means the first
 * 
 * Return the nth sub packet of the given type.
 **/
cdk_subpkt_t
cdk_subpkt_find_nth (cdk_subpkt_t ctx, size_t type, size_t idx)
{
  cdk_subpkt_t s;
  size_t pos;
  
  pos = 0;
  for (s = ctx; s; s = s->next)
    {
      if (s->type == type && pos++ == idx)
	return s;
    }
  
  return NULL;
}


/**
 * cdk_subpkt_new:
 * @size: the size of the new context
 *
 * Create a new sub packet node with the size of @size.
 **/
cdk_subpkt_t
cdk_subpkt_new (size_t size)
{
  cdk_subpkt_t s;
  
  if (!size)
    return NULL;
  s = cdk_calloc (1, sizeof *s + size + 1);
  if (!s)
    return NULL;
  return s;
}


/**
 * cdk_subpkt_get_data:
 * @ctx: the sub packet node
 * @r_type: pointer store the packet type
 * @r_nbytes: pointer to store the packet size
 *
 * Extract the data from the given sub packet. The type is returned
 * in @r_type and the size in @r_nbytes.
 **/
const byte*
cdk_subpkt_get_data (cdk_subpkt_t ctx, size_t *r_type, size_t *r_nbytes)
{
  if (!ctx || !r_nbytes)
    return NULL;
  if (r_type)
    *r_type = ctx->type;
  *r_nbytes = ctx->size;
  return ctx->d;
}


/**
 * cdk_subpkt_add:
 * @root: the root node
 * @node: the node to add
 *
 * Add the node in @node to the root node @root.
 **/
cdk_error_t
cdk_subpkt_add (cdk_subpkt_t root, cdk_subpkt_t node)
{
  cdk_subpkt_t n1;

  if (!root)
    return CDK_Inv_Value;
  for (n1 = root; n1->next; n1 = n1->next)
    ;
  n1->next = node;
  return 0;
}


byte*
_cdk_subpkt_get_array (cdk_subpkt_t s, int count, size_t *r_nbytes)
{
  cdk_subpkt_t list;
  byte *buf;
  size_t n, nbytes;
  
  if (!s) 
    {
      if (r_nbytes)
	*r_nbytes = 0;
      return NULL;
    }
  
  for (n = 0, list = s; list; list = list->next)
    {
      n++; /* type */
      n += list->size;
      if (list->size < 192)
	n++;
      else if (list->size < 8384)
	n += 2;
      else 
	n += 5;
    }
  buf = cdk_calloc (1, n+1);
  if (!buf)
    return NULL;

  n = 0;
  for (list = s; list; list = list->next)
    {
      nbytes = 1 + list->size; /* type */
      if (nbytes < 192)
	buf[n++] = nbytes;
      else if (nbytes < 8384)
	{
	  buf[n++] = nbytes / 256 + 192;
	  buf[n++] = nbytes % 256;
        }
      else
	{
	  buf[n++] = 0xFF;
	  buf[n++] = nbytes >> 24;
	  buf[n++] = nbytes >> 16;
	  buf[n++] = nbytes >>  8;
	  buf[n++] = nbytes;
        }
      buf[n++] = list->type;
      memcpy (buf + n, list->d, list->size);
      n += list->size;
    }
  
  if (count)
    {
      cdk_free (buf);
      buf = NULL;
    }
  if (r_nbytes)
    *r_nbytes = n;
  return buf;
}


cdk_error_t
_cdk_subpkt_copy (cdk_subpkt_t *r_dst, cdk_subpkt_t src)
{
  cdk_subpkt_t root, p, node;
  
  if (!src || !r_dst)
    return CDK_Inv_Value;
  
  root = NULL;
  for (p = src; p; p = p->next) 
    {
      node = cdk_subpkt_new (p->size);
      if (node) 
	{
	  memcpy (node->d, p->d, p->size);
	  node->type = p->type;
	  node->size = p->size;
        }
      if (!root)
	root = node;
      else
	cdk_subpkt_add (root, node);
    }
  *r_dst = root;
  return 0;
}


/**
 * cdk_subpkt_init:
 * @node: the sub packet node
 * @type: type of the packet which data should be initialized
 * @buf: the buffer with the actual data
 * @buflen: the size of the data
 *
 * Set the packet data of the given root and set the type of it.
 **/
void
cdk_subpkt_init (cdk_subpkt_t node, size_t type, 
		 const void *buf, size_t buflen)
{
  if (!node)
    return;  
  node->type = type;
  node->size = buflen;
  memcpy (node->d, buf, buflen);
}


/* FIXME: We need to think of a public interface for it. */
const byte*
cdk_key_desig_revoker_walk (cdk_desig_revoker_t root,
                            cdk_desig_revoker_t * ctx,
                            int *r_class, int *r_algid)
{
  cdk_desig_revoker_t n;
  
  if (! *ctx)
    {
      *ctx = root;
      n = root;
    }
  else 
    {
      n = (*ctx)->next;
      *ctx = n;
    }
  
  if (n && r_class && r_algid)
    {
      *r_class = n->r_class;
      *r_algid = n->algid;
    }
  
  return n? n->fpr : NULL;
}


/**
 * cdk_subpkt_find_next:
 * @root: the base where to begin the iteration
 * @type: the type to find or 0 for the next node.
 * 
 * Try to find the next node after @root with type.
 * If type is 0, the next node will be returned.
 **/
cdk_subpkt_t
cdk_subpkt_find_next (cdk_subpkt_t root, size_t type)
{
  cdk_subpkt_t node;
  
  for (node = root->next; node; node = node->next)
    {
      if (!type)
	return node;
      else if (node->type == type)
	return node;
    }
  
  return NULL;
}