summaryrefslogtreecommitdiffstats
path: root/g10/parse-packet.c
blob: 7c6b85782640f7213ce0858b80405b2c9677b572 (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
/* parse-packet.c  - read packets
 *	Copyright (c) 1997 by Werner Koch (dd9jn)
 *
 * This file is part of G10.
 *
 * G10 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.
 *
 * G10 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 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 <assert.h>

#include "packet.h"
#include "iobuf.h"
#include "mpi.h"
#include "util.h"
#include "cipher.h"
#include "memory.h"
#include "filter.h"
#include "options.h"

static mpi_print_mode = 0;
static list_mode = 0;

static void skip_packet( IOBUF inp, int pkttype, unsigned long pktlen );
static void skip_rest( IOBUF inp, unsigned long pktlen );
static int  parse_publickey( IOBUF inp, int pkttype, unsigned long pktlen,
							     PACKET *packet );
static int  parse_signature( IOBUF inp, int pkttype, unsigned long pktlen,
							 PKT_signature *sig );
static int  parse_certificate( IOBUF inp, int pkttype, unsigned long pktlen,
				      byte *hdr, int hdrlen, PACKET *packet );
static int  parse_user_id( IOBUF inp, int pkttype, unsigned long pktlen,
							   PACKET *packet );
static void parse_comment( IOBUF inp, int pkttype, unsigned long pktlen );
static void parse_trust( IOBUF inp, int pkttype, unsigned long pktlen );
static int  parse_plaintext( IOBUF inp, int pkttype, unsigned long pktlen,
								PACKET *pkt );
static int  parse_compressed( IOBUF inp, int pkttype, unsigned long pktlen,
							   PACKET *packet );
static int  parse_encrypted( IOBUF inp, int pkttype, unsigned long pktlen,
							   PACKET *packet );

static u16
checksum( byte *p )
{
    u16 n, a;

    n = *p++ << 8;
    n |= *p++;
    for(a=0; n; n-- )
	a += *p++;
    return a;
}

static unsigned short
read_16(IOBUF inp)
{
    unsigned short a;
    a = iobuf_get_noeof(inp) << 8;
    a |= iobuf_get_noeof(inp);
    return a;
}

static unsigned long
read_32(IOBUF inp)
{
    unsigned long a;
    a =  iobuf_get_noeof(inp) << 24;
    a |= iobuf_get_noeof(inp) << 16;
    a |= iobuf_get_noeof(inp) << 8;
    a |= iobuf_get_noeof(inp);
    return a;
}

int
set_packet_list_mode( int mode )
{
    int old = list_mode;
    list_mode = mode;
    mpi_print_mode = DBG_MPI;
    return old;
}

/****************
 * Parse a Packet and return it in packet
 * Returns: 0 := valid packet in pkt
 *	   -1 := no more packets
 *	   >0 := error
 * Note: The function may return an error and a partly valid packet;
 * caller must free this packet.
 */
int
parse_packet( IOBUF inp, PACKET *pkt )
{
    int rc, ctb, pkttype, lenbytes;
    unsigned long pktlen;
    byte hdr[5];
    int hdrlen;

    assert( !pkt->pkt.generic );
    if( (ctb = iobuf_get(inp)) == -1 )
	return -1;
    hdrlen=0;
    hdr[hdrlen++] = ctb;
    if( !(ctb & 0x80) ) {
	log_error("invalid packet at '%s'\n", iobuf_where(inp) );
	return G10ERR_INVALID_PACKET;
    }
    /* we handle the pgp 3 extensions here, so that we can skip such packets*/
    pkttype =  ctb & 0x40 ? (ctb & 0x3f) : ((ctb>>2)&0xf);
    lenbytes = (ctb & 0x40) || ((ctb&3)==3)? 0 : (1<<(ctb & 3));
    pktlen = 0;
    if( !lenbytes ) {
	pktlen = 0; /* don't know the value */
	iobuf_set_block_mode(inp, 1);
    }
    else {
	for( ; lenbytes; lenbytes-- ) {
	    pktlen <<= 8;
	    pktlen |= hdr[hdrlen++] = iobuf_get_noeof(inp);
	}
    }

    if( DBG_PACKET )
	log_debug("parse_packet(iob=%d): type=%d length=%lu\n",
					    iobuf_id(inp), pkttype, pktlen );
    pkt->pkttype = pkttype;
    rc = G10ERR_UNKNOWN_PACKET; /* default to no error */
    switch( pkttype ) {
      case PKT_PUBKEY_CERT:
	pkt->pkt.pubkey_cert = m_alloc_clear(sizeof *pkt->pkt.pubkey_cert );
	rc = parse_certificate(inp, pkttype, pktlen, hdr, hdrlen, pkt );
	break;
      case PKT_SECKEY_CERT:
	pkt->pkt.seckey_cert = m_alloc_clear(sizeof *pkt->pkt.seckey_cert );
	rc = parse_certificate(inp, pkttype, pktlen, hdr, hdrlen, pkt );
	break;
      case PKT_PUBKEY_ENC:
	rc = parse_publickey(inp, pkttype, pktlen, pkt );
	break;
      case PKT_SIGNATURE:
	pkt->pkt.signature = m_alloc_clear(sizeof *pkt->pkt.signature );
	rc = parse_signature(inp, pkttype, pktlen, pkt->pkt.signature );
	m_check(pkt->pkt.signature);
	break;
      case PKT_USER_ID:
	rc = parse_user_id(inp, pkttype, pktlen, pkt );
	break;
      case PKT_COMMENT:
	parse_comment(inp, pkttype, pktlen);
	break;
      case PKT_RING_TRUST:
	parse_trust(inp, pkttype, pktlen);
	break;
      case PKT_PLAINTEXT:
	rc = parse_plaintext(inp, pkttype, pktlen, pkt );
	break;
      case PKT_COMPR_DATA:
	rc = parse_compressed(inp, pkttype, pktlen, pkt );
	break;
      case PKT_ENCR_DATA:
	rc = parse_encrypted(inp, pkttype, pktlen, pkt );
	break;
      default:
	skip_packet(inp, pkttype, pktlen);
	break;
    }

    return rc;
}


static void
skip_packet( IOBUF inp, int pkttype, unsigned long pktlen )
{
    if( list_mode )
	printf(":unknown packet: type %2d, length %lu\n", pkttype, pktlen );
    skip_rest(inp,pktlen);
}

static void
skip_rest( IOBUF inp, unsigned long pktlen )
{
    if( iobuf_in_block_mode(inp) ) {
	while( iobuf_get(inp) != -1 )
		;
    }
    else {
	for( ; pktlen; pktlen-- )
	    iobuf_get(inp);
    }
}


static int
parse_publickey( IOBUF inp, int pkttype, unsigned long pktlen, PACKET *packet )
{
    int version;
    unsigned n;
    PKT_pubkey_enc *k;

    k = packet->pkt.pubkey_enc = m_alloc(sizeof *packet->pkt.pubkey_enc );
    if( pktlen < 12 ) {
	log_error("packet(%d) too short\n", pkttype);
	goto leave;
    }
    version = iobuf_get_noeof(inp); pktlen--;
    if( version != 2 && version != 3 ) {
	log_error("packet(%d) with unknown version %d\n", pkttype, version);
	goto leave;
    }
    k->keyid[0] = read_32(inp); pktlen -= 4;
    k->keyid[1] = read_32(inp); pktlen -= 4;
    k->pubkey_algo = iobuf_get_noeof(inp); pktlen--;
    if( list_mode )
	printf(":public key packet: keyid %08lX%08lX\n",
					k->keyid[0], k->keyid[1]);
    if( k->pubkey_algo == PUBKEY_ALGO_RSA ) {
	n = pktlen;
	k->d.rsa.rsa_integer = mpi_decode(inp, &n ); pktlen -=n;
	if( list_mode ) {
	    printf("\trsa integer: ");
	    mpi_print(stdout, k->d.rsa.rsa_integer, mpi_print_mode );
	    putchar('\n');
	}
    }
    else if( list_mode )
	printf("\tunknown algorithm %d\n", k->pubkey_algo );


  leave:
    skip_rest(inp, pktlen);
    return 0;
}


static int
parse_signature( IOBUF inp, int pkttype, unsigned long pktlen,
					  PKT_signature *sig )
{
    int version, md5_len;
    unsigned n;

    if( pktlen < 16 ) {
	log_error("packet(%d) too short\n", pkttype);
	goto leave;
    }
    version = iobuf_get_noeof(inp); pktlen--;
    if( version != 2 && version != 3 ) {
	log_error("packet(%d) with unknown version %d\n", pkttype, version);
	goto leave;
    }
    m_check(sig);
    md5_len = iobuf_get_noeof(inp); pktlen--;
    sig->sig_class = iobuf_get_noeof(inp); pktlen--;
    sig->timestamp = read_32(inp); pktlen -= 4;
    sig->keyid[0] = read_32(inp); pktlen -= 4;
    sig->keyid[1] = read_32(inp); pktlen -= 4;
    sig->pubkey_algo = iobuf_get_noeof(inp); pktlen--;
    m_check(sig);
    if( list_mode )
	printf(":signature packet: keyid %08lX%08lX\n"
	       "\tversion %d, created %lu, md5len %d, sigclass %02x\n",
		sig->keyid[0], sig->keyid[1],
		version, sig->timestamp, md5_len, sig->sig_class );
    if( sig->pubkey_algo == PUBKEY_ALGO_RSA ) {
	if( pktlen < 5 ) {
	    log_error("packet(%d) too short\n", pkttype);
	    goto leave;
	}
    m_check(sig);
	sig->d.rsa.digest_algo = iobuf_get_noeof(inp); pktlen--;
	sig->d.rsa.digest_start[0] = iobuf_get_noeof(inp); pktlen--;
	sig->d.rsa.digest_start[1] = iobuf_get_noeof(inp); pktlen--;
    m_check(sig);
	n = pktlen;
	sig->d.rsa.rsa_integer = mpi_decode(inp, &n ); pktlen -=n;
	if( list_mode ) {
	    printf("\tdigest algo %d, begin of digest %02x %02x\n",
		    sig->d.rsa.digest_algo,
		    sig->d.rsa.digest_start[0], sig->d.rsa.digest_start[1] );
	    printf("\trsa integer: ");
	    mpi_print(stdout, sig->d.rsa.rsa_integer, mpi_print_mode );
	    putchar('\n');
	}
    }
    else if( list_mode )
	printf("\tunknown algorithm %d\n", sig->pubkey_algo );
    m_check(sig);


  leave:
    skip_rest(inp, pktlen);
    return 0;
}




static int
parse_certificate( IOBUF inp, int pkttype, unsigned long pktlen,
			      byte *hdr, int hdrlen, PACKET *pkt )
{
    int i, version, algorithm;
    unsigned n;
    unsigned long timestamp;
    unsigned short valid_period;
    MPI rsa_pub_mod, rsa_pub_exp;

    if( pkttype == PKT_PUBKEY_CERT ) {
	pkt->pkt.pubkey_cert->mfx.md5 = md5_open(0);
	pkt->pkt.pubkey_cert->mfx.rmd160 = rmd160_open(0);
	pkt->pkt.pubkey_cert->mfx.maxbuf_size = 1;
	md5_write(pkt->pkt.pubkey_cert->mfx.md5, hdr, hdrlen);
	rmd160_write(pkt->pkt.pubkey_cert->mfx.rmd160, hdr, hdrlen);
	iobuf_push_filter( inp, md_filter, &pkt->pkt.pubkey_cert->mfx );
    }

    if( pktlen < 12 ) {
	log_error("packet(%d) too short\n", pkttype);
	goto leave;
    }
    version = iobuf_get_noeof(inp); pktlen--;
    if( version != 2 && version != 3 ) {
	log_error("packet(%d) with unknown version %d\n", pkttype, version);
	goto leave;
    }

    timestamp = read_32(inp); pktlen -= 4;
    valid_period = read_16(inp); pktlen -= 2;
    algorithm = iobuf_get_noeof(inp); pktlen--;
    if( list_mode )
	printf(":%s key certification packet:\n"
	       "\tversion %d, created %lu, valid for %hu days\n",
		pkttype == PKT_PUBKEY_CERT? "public": "secret",
		version, timestamp, valid_period );
    if( pkttype == PKT_SECKEY_CERT )  {
	pkt->pkt.seckey_cert->timestamp = timestamp;
	pkt->pkt.seckey_cert->valid_days = valid_period;
	pkt->pkt.seckey_cert->pubkey_algo = algorithm;
    }
    else {
	pkt->pkt.pubkey_cert->timestamp = timestamp;
	pkt->pkt.pubkey_cert->valid_days = valid_period;
	pkt->pkt.pubkey_cert->pubkey_algo = algorithm;
    }

    if( algorithm == PUBKEY_ALGO_RSA ) {
	n = pktlen; rsa_pub_mod = mpi_decode(inp, &n ); pktlen -=n;
	n = pktlen; rsa_pub_exp = mpi_decode(inp, &n ); pktlen -=n;
	if( list_mode ) {
	    printf(  "\tpublic modulus  n:  ");
	    mpi_print(stdout, rsa_pub_mod, mpi_print_mode  );
	    printf("\n\tpublic exponent e: ");
	    mpi_print(stdout, rsa_pub_exp, mpi_print_mode  );
	    putchar('\n');
	}
	if( pkttype == PKT_PUBKEY_CERT ) {
	    pkt->pkt.pubkey_cert->d.rsa.rsa_n = rsa_pub_mod;
	    pkt->pkt.pubkey_cert->d.rsa.rsa_e = rsa_pub_exp;
	}
	else {
	    PKT_seckey_cert *cert = pkt->pkt.seckey_cert;
	    byte temp[8];
	    byte *mpibuf;

	    pkt->pkt.seckey_cert->d.rsa.rsa_n = rsa_pub_mod;
	    pkt->pkt.seckey_cert->d.rsa.rsa_e = rsa_pub_exp;
	    cert->d.rsa.protect_algo = iobuf_get_noeof(inp); pktlen--;
	    if( list_mode )
		printf(  "\tprotect algo: %d\n", cert->d.rsa.protect_algo);
	    if( cert->d.rsa.protect_algo ) {
		cert->d.rsa.is_protected = 1;
		for(i=0; i < 8 && pktlen; i++, pktlen-- )
		    temp[i] = iobuf_get_noeof(inp);
		if( list_mode ) {
		    printf(  "\tprotect IV: ");
		    for(i=0; i < 8; i++ )
			printf(" %02x", temp[i] );
		    putchar('\n');
		}
		if( cert->d.rsa.protect_algo == CIPHER_ALGO_IDEA )
		    memcpy(cert->d.rsa.protect.idea.iv, temp, 8 );
		else if( cert->d.rsa.protect_algo == CIPHER_ALGO_BLOWFISH )
		    memcpy(cert->d.rsa.protect.blowfish.iv, temp, 8 );
	    }
	    else
		cert->d.rsa.is_protected = 0;

	    n = pktlen; mpibuf = mpi_read(inp, &n ); pktlen -=n; assert(n>=2);
	    cert->d.rsa.rsa_d = (MPI)mpibuf;

	    n = pktlen; mpibuf = mpi_read(inp, &n ); pktlen -=n; assert(n>=2);
	    cert->d.rsa.rsa_p = (MPI)mpibuf;

	    n = pktlen; mpibuf = mpi_read(inp, &n ); pktlen -=n; assert(n>=2);
	    cert->d.rsa.rsa_q = (MPI)mpibuf;

	    n = pktlen; mpibuf = mpi_read(inp, &n ); pktlen -=n; assert(n>=2);
	    cert->d.rsa.rsa_u = (MPI)mpibuf;

	    cert->d.rsa.csum = read_16(inp); pktlen -= 2;
	    cert->d.rsa.calc_csum = 0;
	    if( list_mode ) {
		printf("\t[secret values d,p,q,u are not shown]\n"
		       "\tchecksum: %04hx\n", cert->d.rsa.csum);
	    }
	    if( !cert->d.rsa.is_protected ) { /* convert buffer to MPIs */
	      #define X(a) do { 					\
		    mpibuf = (byte*)cert->d.rsa.rsa_##a;		\
		    cert->d.rsa.calc_csum += checksum( mpibuf );	\
		    cert->d.rsa.rsa_##a = mpi_decode_buffer( mpibuf );	\
		    m_free( mpibuf );					\
		} while(0)
		X(d);
		X(p);
		X(q);
		X(u);
	      #undef X
		log_mpidump("rsa n=", cert->d.rsa.rsa_n );
		log_mpidump("rsa e=", cert->d.rsa.rsa_e );
		log_mpidump("rsa d=", cert->d.rsa.rsa_d );
		log_mpidump("rsa p=", cert->d.rsa.rsa_p );
		log_mpidump("rsa q=", cert->d.rsa.rsa_q );
		log_mpidump("rsa u=", cert->d.rsa.rsa_u );
	    }
	}
    }
    else if( list_mode )
	printf("\tunknown algorithm %d\n", algorithm );


  leave:
    if( pkttype == PKT_PUBKEY_CERT )
	iobuf_pop_filter( inp, md_filter, &pkt->pkt.pubkey_cert->mfx );
    skip_rest(inp, pktlen);
    return 0;
}


static int
parse_user_id( IOBUF inp, int pkttype, unsigned long pktlen, PACKET *packet )
{
    byte *p;

    packet->pkt.user_id = m_alloc(sizeof *packet->pkt.user_id  + pktlen - 1);
    packet->pkt.user_id->len = pktlen;
    p = packet->pkt.user_id->name;
    for( ; pktlen; pktlen--, p++ )
	*p = iobuf_get_noeof(inp);

    if( list_mode ) {
	int n = packet->pkt.user_id->len;
	printf(":user id packet: \"");
	for(p=packet->pkt.user_id->name; n; p++, n-- ) {
	    if( *p >= ' ' && *p <= 'z' )
		putchar(*p);
	    else
		printf("\\x%02x", *p );
	}
	printf("\"\n");
    }
    return 0;
}

static void
parse_comment( IOBUF inp, int pkttype, unsigned long pktlen )
{
    if( list_mode ) {
	printf(":comment packet: \"" );
	for( ; pktlen; pktlen-- ) {
	    int c;
	    c = iobuf_get_noeof(inp);
	    if( c >= ' ' && c <= 'z' )
		putchar(c);
	    else
		printf("\\x%02x", c );
	}
	printf("\"\n");
    }
    skip_rest(inp, pktlen);
}


static void
parse_trust( IOBUF inp, int pkttype, unsigned long pktlen )
{
    int c;

    c = iobuf_get_noeof(inp);
    if( list_mode )
	printf(":trust packet: flag=%02x\n", c );
  #if 0 /* fixme: depending on the context we have different interpretations*/
    if( prev_packet_is_a_key_packet ) {
	int ot = c & 7;   /* ownertrust bits (for the key owner) */

	    !ot ? "undefined" :
	ot == 1 ? "unknown"   : /* we don't know the owner of this key */
	ot == 2 ? "no"        : /* usually we do not trust this key owner */
				/* to sign other keys */
	ot == 5 ? "usually"   : /* usually we trust this key owner to sign */
	ot == 6 ? "always"    : /* always trust this key owner to sign */
	ot == 7 ? "ultimate"  : /* also present in the secret keyring */
	      ""                /* reserved value */
	if( c & (1<<5) )
	    "key is disabled"
	if( c & (1<<7) )
	    "buckstop"
    else if( prev_packet_is_user_is_packet ) {
	    int kl = c & 3; /* keylegit bits */
	0 = "unknown, undefined, or uninitialized trust"
	1 = "we do not trust this key's ownership"
	2 = "we have marginal confidence of this key's ownership"
	3 = "we completely trust this key's ownership."
	 /* This one (3) requires either:
	  * - 1 ultimately trusted signature (SIGTRUST=7)
	  * - COMPLETES_NEEDED completely trusted signatures (SIGTRUST=6)
	  * - MARGINALS_NEEDED marginally trusted signatures (SIGTRUST=5)
	  */
	if( c & 0x80 )
	    "warnonly"
    else if( prev_packet_is_a_signature ) {
	 Bits 0-2 - SIGTRUST bits - Trust bits for this signature.  Value is
		    copied directly from OWNERTRUST bits of signer:
	      000 - undefined, or uninitialized trust.
	      001 - unknown
	      010 - We do not trust this signature.
	      011 - reserved
	      100 - reserved
	      101 - We reasonably trust this signature.
	      110 - We completely trust this signature.
	      111 - ultimately trusted signature (from the owner of the ring)
	  Bit 6 - CHECKED bit - This means that the key checking pass (pgp -kc,
		  also invoked automatically whenever keys are added to the
		  keyring) has tested this signature and found it good.  If
		  this bit is not set, the maintenance pass considers this
		  signature untrustworthy.
	  Bit 7 - CONTIG bit - Means this signature leads up a contiguous trusted
		  certification path all the way back to the ultimately-
		  trusted keyring owner, where the buck stops.	This bit is derived
		  from other trust packets.  It is currently not used for anything
		  in PGP.
    }
  #endif
}


static int
parse_plaintext( IOBUF inp, int pkttype, unsigned long pktlen, PACKET *pkt )
{
    int mode, namelen;
    PKT_plaintext *pt;
    byte *p;
    int c, i;

    if( pktlen && pktlen < 6 ) {
	log_error("packet(%d) too short (%lu)\n", pkttype, (ulong)pktlen);
	goto leave;
    }
    mode = iobuf_get_noeof(inp); if( pktlen ) pktlen--;
    namelen = iobuf_get_noeof(inp); if( pktlen ) pktlen--;
    pt = pkt->pkt.plaintext = m_alloc(sizeof *pkt->pkt.plaintext + namelen -1);
    pt->mode = mode;
    pt->namelen = namelen;
    if( pktlen ) {
	for( i=0; pktlen > 4 && i < namelen; pktlen--, i++ )
	    pt->name[i] = iobuf_get_noeof(inp);
    }
    else {
	for( i=0; i < namelen; i++ )
	    if( (c=iobuf_get(inp)) == -1 )
		break;
	    else
		pt->name[i] = c;
    }
    pt->timestamp = read_32(inp); if( pktlen) pktlen -= 4;
    pt->len = pktlen;
    pt->buf = inp;
    pktlen = 0;

    if( list_mode ) {
	printf(":literal data packet:\n"
	       "\tmode %c, created %lu, name=\"",
		    mode >= ' ' && mode <'z'? mode : '?',
		    pt->timestamp );
	for(p=pt->name,i=0; i < namelen; p++, i++ ) {
	    if( *p >= ' ' && *p <= 'z' )
		putchar(*p);
	    else
		printf("\\x%02x", *p );
	}
	printf("\",\n\traw data: %lu bytes\n", pt->len );
    }

  leave:
    return 0;
}


static int
parse_compressed( IOBUF inp, int pkttype, unsigned long pktlen, PACKET *pkt )
{
    PKT_compressed *zd;
    int algorithm;

    /* pktlen is here 0, but data follows
     * (this should be the last object in a file or
     *	the compress algorithm should know the length)
     */
    zd = pkt->pkt.compressed =	m_alloc(sizeof *pkt->pkt.compressed );
    zd->len = 0; /* not yet used */
    zd->algorithm = iobuf_get_noeof(inp);
    zd->buf = inp;
    algorithm = iobuf_get_noeof(inp);
    if( list_mode )
	printf(":compressed packet: algo=%d\n", zd->algorithm);
    return 0;
}


static int
parse_encrypted( IOBUF inp, int pkttype, unsigned long pktlen, PACKET *pkt )
{
    PKT_encr_data *ed;

    ed = pkt->pkt.encr_data =  m_alloc(sizeof *pkt->pkt.encr_data );
    ed->len = pktlen;
    ed->buf = NULL;
    if( pktlen && pktlen < 10 ) {
	log_error("packet(%d) too short\n", pkttype);
	skip_rest(inp, pktlen);
	goto leave;
    }
    if( list_mode )
	if( pktlen )
	    printf(":encrypted data packet:\n\tlength: %lu\n", pktlen-10);
	else
	    printf(":encrypted data packet:\n\tlength: unknown\n");

    ed->buf = inp;
    pktlen = 0;

  leave:
    return 0;
}