~ubuntu-branches/ubuntu/karmic/gnupg2/karmic-updates

« back to all changes in this revision

Viewing changes to g10/parse-packet.c

  • Committer: Bazaar Package Importer
  • Author(s): Thomas Viehmann
  • Date: 2008-10-04 10:25:53 UTC
  • mfrom: (5.1.15 intrepid)
  • Revision ID: james.westby@ubuntu.com-20081004102553-fv62pp8dsitxli47
Tags: 2.0.9-3.1
* Non-maintainer upload.
* agent/gpg-agent.c: Deinit the threading library before exec'ing
  the command to run in --daemon mode. And because that still doesn't
  restore the sigprocmask, do that manually. Closes: #499569

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* parse-packet.c  - read packets
2
 
 * Copyright (C) 1998, 1999, 2000, 2001, 2002,
3
 
 *               2003 Free Software Foundation, Inc.
 
2
 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
 
3
 *               2007 Free Software Foundation, Inc.
4
4
 *
5
5
 * This file is part of GnuPG.
6
6
 *
7
7
 * GnuPG is free software; you can redistribute it and/or modify
8
8
 * it under the terms of the GNU General Public License as published by
9
 
 * the Free Software Foundation; either version 2 of the License, or
 
9
 * the Free Software Foundation; either version 3 of the License, or
10
10
 * (at your option) any later version.
11
11
 *
12
12
 * GnuPG is distributed in the hope that it will be useful,
15
15
 * GNU General Public License for more details.
16
16
 *
17
17
 * You should have received a copy of the GNU General Public License
18
 
 * along with this program; if not, write to the Free Software
19
 
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 
18
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20
19
 */
21
20
 
22
21
#include <config.h>
25
24
#include <string.h>
26
25
#include <assert.h>
27
26
 
 
27
#include "gpg.h"
28
28
#include "packet.h"
29
29
#include "iobuf.h"
30
 
#include "mpi.h"
31
30
#include "util.h"
32
31
#include "cipher.h"
33
 
#include "memory.h"
34
32
#include "filter.h"
35
33
#include "photoid.h"
36
34
#include "options.h"
37
35
#include "main.h"
38
36
#include "i18n.h"
39
37
 
40
 
static int mpi_print_mode = 0;
41
 
static int list_mode = 0;
 
38
static int mpi_print_mode;
 
39
static int list_mode;
 
40
static FILE *listfp;
42
41
 
43
 
static int  parse( iobuf_t inp, PACKET *pkt, int onlykeypkts,
44
 
                  off_t *retpos, int *skip, iobuf_t out, int do_skip
 
42
static int  parse( IOBUF inp, PACKET *pkt, int onlykeypkts,
 
43
                  off_t *retpos, int *skip, IOBUF out, int do_skip
45
44
#ifdef DEBUG_PARSE_PACKET
46
45
                   ,const char *dbg_w, const char *dbg_f, int dbg_l
47
46
#endif
48
47
                 );
49
 
static int  copy_packet( iobuf_t inp, iobuf_t out, int pkttype,
50
 
                                               unsigned long pktlen );
51
 
static void skip_packet( iobuf_t inp, int pkttype, unsigned long pktlen );
52
 
static void skip_rest( iobuf_t inp, unsigned long pktlen );
53
 
static void *read_rest( iobuf_t inp, size_t pktlen );
54
 
static int  parse_symkeyenc( iobuf_t inp, int pkttype, unsigned long pktlen,
55
 
                                                             PACKET *packet );
56
 
static int  parse_pubkeyenc( iobuf_t inp, int pkttype, unsigned long pktlen,
57
 
                                                             PACKET *packet );
58
 
static int  parse_signature( iobuf_t inp, int pkttype, unsigned long pktlen,
59
 
                                                         PKT_signature *sig );
60
 
static int  parse_onepass_sig( iobuf_t inp, int pkttype, unsigned long pktlen,
 
48
static int  copy_packet( IOBUF inp, IOBUF out, int pkttype,
 
49
                         unsigned long pktlen, int partial );
 
50
static void skip_packet( IOBUF inp, int pkttype,
 
51
                         unsigned long pktlen, int partial );
 
52
static void *read_rest( IOBUF inp, size_t pktlen, int partial );
 
53
static int  parse_marker( IOBUF inp, int pkttype, unsigned long pktlen );
 
54
static int  parse_symkeyenc( IOBUF inp, int pkttype, unsigned long pktlen,
 
55
                                                             PACKET *packet );
 
56
static int  parse_pubkeyenc( IOBUF inp, int pkttype, unsigned long pktlen,
 
57
                                                             PACKET *packet );
 
58
static int  parse_onepass_sig( IOBUF inp, int pkttype, unsigned long pktlen,
61
59
                                                        PKT_onepass_sig *ops );
62
 
static int  parse_key( iobuf_t inp, int pkttype, unsigned long pktlen,
 
60
static int  parse_key( IOBUF inp, int pkttype, unsigned long pktlen,
63
61
                                      byte *hdr, int hdrlen, PACKET *packet );
64
 
static int  parse_user_id( iobuf_t inp, int pkttype, unsigned long pktlen,
65
 
                                                           PACKET *packet );
66
 
static int  parse_attribute( iobuf_t inp, int pkttype, unsigned long pktlen,
67
 
                                                           PACKET *packet );
68
 
static int  parse_comment( iobuf_t inp, int pkttype, unsigned long pktlen,
69
 
                                                           PACKET *packet );
70
 
static void parse_trust( iobuf_t inp, int pkttype, unsigned long pktlen,
71
 
                                                           PACKET *packet );
72
 
static int  parse_plaintext( iobuf_t inp, int pkttype, unsigned long pktlen,
73
 
                                               PACKET *packet, int new_ctb);
74
 
static int  parse_compressed( iobuf_t inp, int pkttype, unsigned long pktlen,
 
62
static int  parse_user_id( IOBUF inp, int pkttype, unsigned long pktlen,
 
63
                                                           PACKET *packet );
 
64
static int  parse_attribute( IOBUF inp, int pkttype, unsigned long pktlen,
 
65
                                                           PACKET *packet );
 
66
static int  parse_comment( IOBUF inp, int pkttype, unsigned long pktlen,
 
67
                                                           PACKET *packet );
 
68
static void parse_trust( IOBUF inp, int pkttype, unsigned long pktlen,
 
69
                                                           PACKET *packet );
 
70
static int  parse_plaintext( IOBUF inp, int pkttype, unsigned long pktlen,
 
71
                             PACKET *packet, int new_ctb, int partial);
 
72
static int  parse_compressed( IOBUF inp, int pkttype, unsigned long pktlen,
75
73
                                               PACKET *packet, int new_ctb );
76
 
static int  parse_encrypted( iobuf_t inp, int pkttype, unsigned long pktlen,
77
 
                                               PACKET *packet, int new_ctb);
78
 
static int  parse_mdc( iobuf_t inp, int pkttype, unsigned long pktlen,
79
 
                                               PACKET *packet, int new_ctb);
80
 
static int  parse_gpg_control( iobuf_t inp, int pkttype, unsigned long pktlen,
81
 
                               PACKET *packet );
 
74
static int  parse_encrypted( IOBUF inp, int pkttype, unsigned long pktlen,
 
75
                             PACKET *packet, int new_ctb, int partial);
 
76
static int  parse_mdc( IOBUF inp, int pkttype, unsigned long pktlen,
 
77
                                               PACKET *packet, int new_ctb);
 
78
static int  parse_gpg_control( IOBUF inp, int pkttype, unsigned long pktlen,
 
79
                               PACKET *packet, int partial );
82
80
 
83
81
static unsigned short
84
 
read_16(iobuf_t inp)
 
82
read_16(IOBUF inp)
85
83
{
86
84
    unsigned short a;
87
85
    a = iobuf_get_noeof(inp) << 8;
90
88
}
91
89
 
92
90
static unsigned long
93
 
read_32(iobuf_t inp)
 
91
read_32(IOBUF inp)
94
92
{
95
93
    unsigned long a;
96
94
    a =  iobuf_get_noeof(inp) << 24;
101
99
}
102
100
 
103
101
 
 
102
/* Read an external representation of an mpi and return the MPI.  The
 
103
 * external format is a 16 bit unsigned value stored in network byte
 
104
 * order, giving the number of bits for the following integer. The
 
105
 * integer is stored with MSB first (left padded with zeroes to align
 
106
 * on a byte boundary).
 
107
 */
 
108
static gcry_mpi_t
 
109
mpi_read (iobuf_t inp, unsigned int *ret_nread, int secure)
 
110
{
 
111
  /*FIXME: Needs to be synced with gnupg14/mpi/mpicoder.c*/
 
112
 
 
113
  int c, c1, c2, i;
 
114
  unsigned int nbits, nbytes;
 
115
  size_t nread;
 
116
  gcry_mpi_t a = NULL;
 
117
  byte *buf = NULL;
 
118
  byte *p;
 
119
  
 
120
  if ( (c = c1 = iobuf_get (inp)) == -1 )
 
121
    goto leave;
 
122
  nbits = c << 8;
 
123
  if ( (c = c2 = iobuf_get (inp)) == -1 )
 
124
    goto leave;
 
125
  nbits |= c;
 
126
  if ( nbits > MAX_EXTERN_MPI_BITS ) 
 
127
    {
 
128
      log_error("mpi too large (%u bits)\n", nbits);
 
129
      goto leave;
 
130
    }
 
131
  nread = 2;
 
132
  nbytes = (nbits+7) / 8;
 
133
  buf = secure ? gcry_xmalloc_secure (nbytes + 2) : gcry_xmalloc (nbytes + 2);
 
134
  p = buf;
 
135
  p[0] = c1;
 
136
  p[1] = c2;
 
137
  for ( i=0 ; i < nbytes; i++ ) 
 
138
    {
 
139
      p[i+2] = iobuf_get(inp) & 0xff;
 
140
      nread++;
 
141
    }
 
142
  if ( gcry_mpi_scan( &a, GCRYMPI_FMT_PGP, buf, nread, &nread ) )
 
143
    a = NULL;
 
144
    
 
145
 leave:
 
146
  gcry_free(buf);
 
147
  if ( nread > *ret_nread )
 
148
    log_bug ("mpi larger than packet");
 
149
  else
 
150
    *ret_nread = nread;
 
151
  return a;
 
152
}
 
153
 
 
154
 
 
155
 
 
156
 
104
157
int
105
158
set_packet_list_mode( int mode )
106
159
{
107
160
    int old = list_mode;
108
161
    list_mode = mode;
109
 
    /* FIXME(gcrypt) mpi_print_mode = DBG_MPI; */
 
162
   /* FIXME(gcrypt) mpi_print_mode = DBG_MPI; */
 
163
    /* We use stdout print only if invoked by the --list-packets
 
164
       command but switch to stderr in all otehr cases.  This breaks
 
165
       the previous behaviour but that seems to be more of a bug than
 
166
       intentional.  I don't believe that any application makes use of
 
167
       this long standing annoying way of printing to stdout except
 
168
       when doing a --list-packets. If this assumption fails, it will
 
169
       be easy to add an option for the listing stream.  Note that we
 
170
       initialize it only once; mainly because some code may switch
 
171
       the option value later back to 1 and we want to have all output
 
172
       to the same stream.  
 
173
 
 
174
       Using stderr is not actually very clean because it bypasses the
 
175
       logging code but it is a special thing anyay.  I am not sure
 
176
       whether using log_stream() would be better.  Perhaps we should
 
177
       enable the list mdoe only with a special option. */
 
178
    if (!listfp)
 
179
        listfp = opt.list_packets == 2 ? stdout : stderr;
110
180
    return old;
111
181
}
112
182
 
133
203
 */
134
204
#ifdef DEBUG_PARSE_PACKET
135
205
int
136
 
dbg_parse_packet( iobuf_t inp, PACKET *pkt, const char *dbg_f, int dbg_l )
 
206
dbg_parse_packet( IOBUF inp, PACKET *pkt, const char *dbg_f, int dbg_l )
137
207
{
138
208
    int skip, rc;
139
209
 
144
214
}
145
215
#else
146
216
int
147
 
parse_packet( iobuf_t inp, PACKET *pkt )
 
217
parse_packet( IOBUF inp, PACKET *pkt )
148
218
{
149
219
    int skip, rc;
150
220
 
160
230
 */
161
231
#ifdef DEBUG_PARSE_PACKET
162
232
int
163
 
dbg_search_packet( iobuf_t inp, PACKET *pkt, off_t *retpos, int with_uid,
 
233
dbg_search_packet( IOBUF inp, PACKET *pkt, off_t *retpos, int with_uid,
164
234
                   const char *dbg_f, int dbg_l )
165
235
{
166
236
    int skip, rc;
172
242
}
173
243
#else
174
244
int
175
 
search_packet( iobuf_t inp, PACKET *pkt, off_t *retpos, int with_uid )
 
245
search_packet( IOBUF inp, PACKET *pkt, off_t *retpos, int with_uid )
176
246
{
177
247
    int skip, rc;
178
248
 
188
258
 */
189
259
#ifdef DEBUG_PARSE_PACKET
190
260
int
191
 
dbg_copy_all_packets( iobuf_t inp, iobuf_t out,
 
261
dbg_copy_all_packets( IOBUF inp, IOBUF out,
192
262
                   const char *dbg_f, int dbg_l )
193
263
{
194
264
    PACKET pkt;
200
270
}
201
271
#else
202
272
int
203
 
copy_all_packets( iobuf_t inp, iobuf_t out )
 
273
copy_all_packets( IOBUF inp, IOBUF out )
204
274
{
205
275
    PACKET pkt;
206
276
    int skip, rc=0;
217
287
 */
218
288
#ifdef DEBUG_PARSE_PACKET
219
289
int
220
 
dbg_copy_some_packets( iobuf_t inp, iobuf_t out, off_t stopoff,
 
290
dbg_copy_some_packets( IOBUF inp, IOBUF out, off_t stopoff,
221
291
                   const char *dbg_f, int dbg_l )
222
292
{
223
293
    PACKET pkt;
232
302
}
233
303
#else
234
304
int
235
 
copy_some_packets( iobuf_t inp, iobuf_t out, off_t stopoff )
 
305
copy_some_packets( IOBUF inp, IOBUF out, off_t stopoff )
236
306
{
237
307
    PACKET pkt;
238
308
    int skip, rc=0;
250
320
 */
251
321
#ifdef DEBUG_PARSE_PACKET
252
322
int
253
 
dbg_skip_some_packets( iobuf_t inp, unsigned n,
 
323
dbg_skip_some_packets( IOBUF inp, unsigned n,
254
324
                   const char *dbg_f, int dbg_l )
255
325
{
256
326
    int skip, rc=0;
264
334
}
265
335
#else
266
336
int
267
 
skip_some_packets( iobuf_t inp, unsigned n )
 
337
skip_some_packets( IOBUF inp, unsigned n )
268
338
{
269
339
    int skip, rc=0;
270
340
    PACKET pkt;
286
356
 * if OUT is not NULL, a special copymode is used.
287
357
 */
288
358
static int
289
 
parse( iobuf_t inp, PACKET *pkt, int onlykeypkts, off_t *retpos,
290
 
       int *skip, iobuf_t out, int do_skip
 
359
parse( IOBUF inp, PACKET *pkt, int onlykeypkts, off_t *retpos,
 
360
       int *skip, IOBUF out, int do_skip
291
361
#ifdef DEBUG_PARSE_PACKET
292
362
       ,const char *dbg_w, const char *dbg_f, int dbg_l
293
363
#endif
297
367
    unsigned long pktlen;
298
368
    byte hdr[8];
299
369
    int hdrlen;
300
 
    int new_ctb = 0;
 
370
    int new_ctb = 0, partial=0;
301
371
    int with_uid = (onlykeypkts == 2);
302
372
 
303
373
    *skip = 0;
313
383
    hdr[hdrlen++] = ctb;
314
384
    if( !(ctb & 0x80) ) {
315
385
        log_error("%s: invalid packet (ctb=%02x)\n", iobuf_where(inp), ctb );
316
 
        rc = GPG_ERR_INV_PACKET;
 
386
        rc = gpg_error (GPG_ERR_INV_PACKET);
317
387
        goto leave;
318
388
    }
319
389
    pktlen = 0;
322
392
        pkttype = ctb & 0x3f;
323
393
        if( (c = iobuf_get(inp)) == -1 ) {
324
394
            log_error("%s: 1st length byte missing\n", iobuf_where(inp) );
325
 
            rc = GPG_ERR_INV_PACKET;
 
395
            rc = gpg_error (GPG_ERR_INV_PACKET);
326
396
            goto leave;
327
397
        }
328
398
        if (pkttype == PKT_COMPRESSED) {
329
399
             iobuf_set_partial_block_mode(inp, c & 0xff);
330
400
             pktlen = 0;/* to indicate partial length */
 
401
             partial=1;
331
402
        }
332
403
        else {
333
404
             hdr[hdrlen++] = c;
334
405
             if( c < 192 )
335
 
                  pktlen = c;
336
 
             else if( c < 224 ) {
337
 
                  pktlen = (c - 192) * 256;
338
 
                  if( (c = iobuf_get(inp)) == -1 ) {
339
 
                       log_error("%s: 2nd length byte missing\n",
340
 
                                 iobuf_where(inp) );
341
 
                       rc = GPG_ERR_INV_PACKET;
342
 
                       goto leave;
343
 
                  }
344
 
                  hdr[hdrlen++] = c;
345
 
                  pktlen += c + 192;
346
 
             }
347
 
             else if( c == 255 ) {
348
 
                  pktlen  = (hdr[hdrlen++] = iobuf_get_noeof(inp)) << 24;
349
 
                  pktlen |= (hdr[hdrlen++] = iobuf_get_noeof(inp)) << 16;
350
 
                  pktlen |= (hdr[hdrlen++] = iobuf_get_noeof(inp)) << 8;
351
 
                  if( (c = iobuf_get(inp)) == -1 ) {
352
 
                       log_error("%s: 4 byte length invalid\n",
353
 
                                 iobuf_where(inp) );
354
 
                       rc = GPG_ERR_INV_PACKET;
355
 
                       goto leave;
356
 
                  }
357
 
                  pktlen |= (hdr[hdrlen++] = c );
358
 
             }
359
 
             else { /* partial body length */
360
 
                  iobuf_set_partial_block_mode(inp, c & 0xff);
361
 
                  pktlen = 0;/* to indicate partial length */
362
 
             }
 
406
               pktlen = c;
 
407
             else if( c < 224 )
 
408
               {
 
409
                 pktlen = (c - 192) * 256;
 
410
                 if( (c = iobuf_get(inp)) == -1 )
 
411
                   {
 
412
                     log_error("%s: 2nd length byte missing\n",
 
413
                               iobuf_where(inp) );
 
414
                     rc = gpg_error (GPG_ERR_INV_PACKET);
 
415
                     goto leave;
 
416
                   }
 
417
                 hdr[hdrlen++] = c;
 
418
                 pktlen += c + 192;
 
419
               }
 
420
             else if( c == 255 )
 
421
               {
 
422
                 pktlen  = (hdr[hdrlen++] = iobuf_get_noeof(inp)) << 24;
 
423
                 pktlen |= (hdr[hdrlen++] = iobuf_get_noeof(inp)) << 16;
 
424
                 pktlen |= (hdr[hdrlen++] = iobuf_get_noeof(inp)) << 8;
 
425
                 if( (c = iobuf_get(inp)) == -1 )
 
426
                   {
 
427
                     log_error("%s: 4 byte length invalid\n",
 
428
                               iobuf_where(inp) );
 
429
                     rc = gpg_error (GPG_ERR_INV_PACKET);
 
430
                     goto leave;
 
431
                   }
 
432
                 pktlen |= (hdr[hdrlen++] = c );
 
433
               }
 
434
             else
 
435
               {
 
436
                 /* Partial body length.  Note that we handled
 
437
                    PKT_COMPRESSED earlier. */
 
438
                 if(pkttype==PKT_PLAINTEXT || pkttype==PKT_ENCRYPTED
 
439
                    || pkttype==PKT_ENCRYPTED_MDC)
 
440
                   {
 
441
                     iobuf_set_partial_block_mode(inp, c & 0xff);
 
442
                     pktlen = 0;/* to indicate partial length */
 
443
                     partial=1;
 
444
                   }
 
445
                 else
 
446
                   {
 
447
                     log_error("%s: partial length for invalid"
 
448
                               " packet type %d\n",iobuf_where(inp),pkttype);
 
449
                     rc = gpg_error (GPG_ERR_INV_PACKET);
 
450
                     goto leave;
 
451
                   }
 
452
               }
363
453
        }
364
454
    }
365
 
    else {
 
455
    else
 
456
      {
366
457
        pkttype = (ctb>>2)&0xf;
367
458
        lenbytes = ((ctb&3)==3)? 0 : (1<<(ctb & 3));
368
 
        if( !lenbytes ) {
 
459
        if( !lenbytes )
 
460
          {
369
461
            pktlen = 0; /* don't know the value */
370
 
            switch (pkttype) {
371
 
              case PKT_ENCRYPTED:
372
 
              case PKT_PLAINTEXT:
373
 
                /* These partial length encodings are from an very
374
 
                   early GnuPG release and deprecated.  However we
375
 
                   still support them read-wise.  Note, that we should
376
 
                   not allow them for any key related packets, because
377
 
                   this might render a keyring unusable if an errenous
378
 
                   packet indicated this mode but not complying to it
379
 
                   gets imported. */
380
 
                iobuf_set_block_mode(inp, 1);
381
 
                break;
382
 
 
383
 
              case PKT_COMPRESSED:
384
 
                break; /* the orginal pgp 2 way. */
385
 
 
386
 
              default:
387
 
                log_error ("%s: old style partial length "
388
 
                           "for invalid packet type\n", iobuf_where(inp) );
 
462
            /* This isn't really partial, but we can treat it the same
 
463
               in a "read until the end" sort of way. */
 
464
            partial=1;
 
465
            if(pkttype!=PKT_ENCRYPTED && pkttype!=PKT_PLAINTEXT
 
466
               && pkttype!=PKT_COMPRESSED)
 
467
              {
 
468
                log_error ("%s: indeterminate length for invalid"
 
469
                           " packet type %d\n", iobuf_where(inp), pkttype );
389
470
                rc = gpg_error (GPG_ERR_INV_PACKET);
390
 
                goto leave;
391
 
            }
392
 
       }
393
 
        else {
394
 
            for( ; lenbytes; lenbytes-- ) {
 
471
                goto leave;
 
472
              }
 
473
          }
 
474
        else
 
475
          {
 
476
            for( ; lenbytes; lenbytes-- )
 
477
              {
395
478
                pktlen <<= 8;
396
479
                pktlen |= hdr[hdrlen++] = iobuf_get_noeof(inp);
397
 
            }
398
 
        }
399
 
    }
 
480
              }
 
481
          }
 
482
      }
400
483
 
401
484
    if (pktlen == 0xffffffff) {
402
485
        /* with a some probability this is caused by a problem in the
407
490
    }
408
491
 
409
492
    if( out && pkttype  ) {
410
 
        rc = iobuf_write( out, hdr, hdrlen );
411
 
        if (!rc)
412
 
            rc = copy_packet(inp, out, pkttype, pktlen );
413
 
        goto leave;
 
493
      rc = iobuf_write (out, hdr, hdrlen);
 
494
      if (!rc)
 
495
            rc = copy_packet(inp, out, pkttype, pktlen, partial );
 
496
      goto leave;
414
497
    }
415
498
 
416
499
    if (with_uid && pkttype == PKT_USER_ID)
421
504
                        && pkttype != PKT_PUBLIC_KEY
422
505
                        && pkttype != PKT_SECRET_SUBKEY
423
506
                        && pkttype != PKT_SECRET_KEY  ) ) {
424
 
        skip_rest(inp, pktlen);
 
507
        iobuf_skip_rest(inp, pktlen, partial);
425
508
        *skip = 1;
426
509
        rc = 0;
427
510
        goto leave;
438
521
#endif
439
522
    }
440
523
    pkt->pkttype = pkttype;
441
 
    rc = GPG_ERR_UNKNOWN_PACKET; /* default error */
 
524
    rc = G10ERR_UNKNOWN_PACKET; /* default error */
442
525
    switch( pkttype ) {
443
526
      case PKT_PUBLIC_KEY:
444
527
      case PKT_PUBLIC_SUBKEY:
445
 
        pkt->pkt.public_key = xcalloc (1,sizeof *pkt->pkt.public_key );
 
528
        pkt->pkt.public_key = xmalloc_clear(sizeof *pkt->pkt.public_key );
446
529
        rc = parse_key(inp, pkttype, pktlen, hdr, hdrlen, pkt );
447
530
        break;
448
531
      case PKT_SECRET_KEY:
449
532
      case PKT_SECRET_SUBKEY:
450
 
        pkt->pkt.secret_key = xcalloc (1,sizeof *pkt->pkt.secret_key );
 
533
        pkt->pkt.secret_key = xmalloc_clear(sizeof *pkt->pkt.secret_key );
451
534
        rc = parse_key(inp, pkttype, pktlen, hdr, hdrlen, pkt );
452
535
        break;
453
536
      case PKT_SYMKEY_ENC:
457
540
        rc = parse_pubkeyenc(inp, pkttype, pktlen, pkt );
458
541
        break;
459
542
      case PKT_SIGNATURE:
460
 
        pkt->pkt.signature = xcalloc (1,sizeof *pkt->pkt.signature );
 
543
        pkt->pkt.signature = xmalloc_clear(sizeof *pkt->pkt.signature );
461
544
        rc = parse_signature(inp, pkttype, pktlen, pkt->pkt.signature );
462
545
        break;
463
546
      case PKT_ONEPASS_SIG:
464
 
        pkt->pkt.onepass_sig = xcalloc (1,sizeof *pkt->pkt.onepass_sig );
 
547
        pkt->pkt.onepass_sig = xmalloc_clear(sizeof *pkt->pkt.onepass_sig );
465
548
        rc = parse_onepass_sig(inp, pkttype, pktlen, pkt->pkt.onepass_sig );
466
549
        break;
467
550
      case PKT_USER_ID:
480
563
        rc = 0;
481
564
        break;
482
565
      case PKT_PLAINTEXT:
483
 
        rc = parse_plaintext(inp, pkttype, pktlen, pkt, new_ctb );
 
566
        rc = parse_plaintext(inp, pkttype, pktlen, pkt, new_ctb, partial );
484
567
        break;
485
568
      case PKT_COMPRESSED:
486
569
        rc = parse_compressed(inp, pkttype, pktlen, pkt, new_ctb );
487
570
        break;
488
571
      case PKT_ENCRYPTED:
489
572
      case PKT_ENCRYPTED_MDC:
490
 
        rc = parse_encrypted(inp, pkttype, pktlen, pkt, new_ctb );
 
573
        rc = parse_encrypted(inp, pkttype, pktlen, pkt, new_ctb, partial );
491
574
        break;
492
575
      case PKT_MDC:
493
576
        rc = parse_mdc(inp, pkttype, pktlen, pkt, new_ctb );
494
577
        break;
495
578
      case PKT_GPG_CONTROL:
496
 
        rc = parse_gpg_control(inp, pkttype, pktlen, pkt );
 
579
        rc = parse_gpg_control(inp, pkttype, pktlen, pkt, partial );
497
580
        break;
 
581
    case PKT_MARKER:
 
582
        rc = parse_marker(inp,pkttype,pktlen);
 
583
        break;
498
584
      default:
499
 
        skip_packet(inp, pkttype, pktlen);
 
585
        skip_packet(inp, pkttype, pktlen, partial);
500
586
        break;
501
587
    }
502
588
 
503
589
  leave:
504
590
    if( !rc && iobuf_error(inp) )
505
 
        rc = GPG_ERR_INV_KEYRING;
 
591
        rc = G10ERR_INV_KEYRING;
506
592
    return rc;
507
593
}
508
594
 
511
597
{
512
598
    if( *i && !(*i%8) ) {
513
599
        if( *i && !(*i%24) )
514
 
            printf("\n%4d:", *i );
 
600
            fprintf (listfp, "\n%4d:", *i );
515
601
        else
516
 
            putchar(' ');
 
602
            putc (' ', listfp);
517
603
    }
518
604
    if( c == -1 )
519
 
        printf(" EOF" );
 
605
        fprintf (listfp, " EOF" );
520
606
    else
521
 
        printf(" %02x", c );
 
607
        fprintf (listfp, " %02x", c );
522
608
    ++*i;
523
609
}
524
610
 
525
611
 
526
612
static int
527
 
copy_packet( iobuf_t inp, iobuf_t out, int pkttype, unsigned long pktlen )
 
613
copy_packet( IOBUF inp, IOBUF out, int pkttype,
 
614
             unsigned long pktlen, int partial )
528
615
{
529
 
    int rc, n;
 
616
    int rc;
 
617
    int n;
530
618
    char buf[100];
531
619
 
532
 
    if( iobuf_in_block_mode(inp) ) {
 
620
    if( partial ) {
533
621
        while( (n = iobuf_read( inp, buf, 100 )) != -1 )
534
 
            if( (rc = iobuf_write(out, buf, n )) )
 
622
            if( (rc=iobuf_write(out, buf, n )) )
535
623
                return rc; /* write error */
536
624
    }
537
625
    else if( !pktlen && pkttype == PKT_COMPRESSED ) {
538
626
        log_debug("copy_packet: compressed!\n");
539
627
        /* compressed packet, copy till EOF */
540
628
        while( (n = iobuf_read( inp, buf, 100 )) != -1 )
541
 
            if( (rc = iobuf_write(out, buf, n )) )
 
629
            if( (rc=iobuf_write(out, buf, n )) )
542
630
                return rc; /* write error */
543
631
    }
544
632
    else {
546
634
            n = pktlen > 100 ? 100 : pktlen;
547
635
            n = iobuf_read( inp, buf, n );
548
636
            if( n == -1 )
549
 
              return GPG_ERR_GENERAL; /* FIXME(gcrypt): read error*/;
550
 
            if( (rc = iobuf_write(out, buf, n )) )
 
637
                return gpg_error (GPG_ERR_EOF);
 
638
            if( (rc=iobuf_write(out, buf, n )) )
551
639
                return rc; /* write error */
552
640
        }
553
641
    }
556
644
 
557
645
 
558
646
static void
559
 
skip_packet( iobuf_t inp, int pkttype, unsigned long pktlen )
 
647
skip_packet( IOBUF inp, int pkttype, unsigned long pktlen, int partial )
560
648
{
561
 
    if( list_mode ) {
562
 
        if( pkttype == PKT_MARKER )
563
 
            fputs(":marker packet:\n", stdout );
564
 
        else
565
 
            printf(":unknown packet: type %2d, length %lu\n", pkttype, pktlen);
566
 
        if( pkttype ) {
567
 
            int c, i=0 ;
568
 
            if( pkttype != PKT_MARKER )
569
 
                fputs("dump:", stdout );
570
 
            if( iobuf_in_block_mode(inp) ) {
571
 
                while( (c=iobuf_get(inp)) != -1 )
572
 
                    dump_hex_line(c, &i);
573
 
            }
574
 
            else {
575
 
                for( ; pktlen; pktlen-- )
576
 
                    dump_hex_line(iobuf_get(inp), &i);
577
 
            }
578
 
            putchar('\n');
579
 
            return;
 
649
  if( list_mode )
 
650
    {
 
651
      fprintf (listfp, ":unknown packet: type %2d, length %lu\n",
 
652
               pkttype, pktlen);
 
653
      if( pkttype )
 
654
        {
 
655
          int c, i=0 ;
 
656
          fputs("dump:", listfp );
 
657
          if( partial )
 
658
            {
 
659
              while( (c=iobuf_get(inp)) != -1 )
 
660
                dump_hex_line(c, &i);
 
661
            }
 
662
          else
 
663
            {
 
664
              for( ; pktlen; pktlen-- )
 
665
                dump_hex_line(iobuf_get(inp), &i);
 
666
            }
 
667
          putc ('\n', listfp);
 
668
          return;
580
669
        }
581
670
    }
582
 
    skip_rest(inp,pktlen);
583
 
}
584
 
 
585
 
static void
586
 
skip_rest( iobuf_t inp, unsigned long pktlen )
587
 
{
588
 
    if( iobuf_in_block_mode(inp) ) {
589
 
        while( iobuf_get(inp) != -1 )
590
 
                ;
591
 
    }
592
 
    else {
593
 
        for( ; pktlen; pktlen-- )
594
 
            if( iobuf_get(inp) == -1 )
595
 
                break;
596
 
    }
597
 
}
598
 
 
 
671
  iobuf_skip_rest(inp,pktlen,partial);
 
672
}
599
673
 
600
674
static void *
601
 
read_rest( iobuf_t inp, size_t pktlen )
 
675
read_rest( IOBUF inp, size_t pktlen, int partial )
602
676
{
603
677
    byte *p;
604
678
    int i;
605
679
 
606
 
    if( iobuf_in_block_mode(inp) ) {
 
680
    if( partial ) {
607
681
        log_error("read_rest: can't store stream data\n");
608
682
        p = NULL;
609
683
    }
610
684
    else {
611
 
        p = xmalloc ( pktlen );
 
685
        p = xmalloc( pktlen );
612
686
        for(i=0; pktlen; pktlen--, i++ )
613
687
            p[i] = iobuf_get(inp);
614
688
    }
615
689
    return p;
616
690
}
617
691
 
618
 
 
619
 
 
620
 
static int
621
 
parse_symkeyenc( iobuf_t inp, int pkttype, unsigned long pktlen, PACKET *packet )
 
692
static int
 
693
parse_marker( IOBUF inp, int pkttype, unsigned long pktlen )
 
694
{
 
695
  if(pktlen!=3)
 
696
    goto fail;
 
697
 
 
698
  if(iobuf_get(inp)!='P')
 
699
    {
 
700
      pktlen--;
 
701
      goto fail;
 
702
    }
 
703
 
 
704
  if(iobuf_get(inp)!='G')
 
705
    {
 
706
      pktlen--;
 
707
      goto fail;
 
708
    }
 
709
 
 
710
  if(iobuf_get(inp)!='P')
 
711
    {
 
712
      pktlen--;
 
713
      goto fail;
 
714
    }
 
715
 
 
716
  if(list_mode)
 
717
    fputs(":marker packet: PGP\n", listfp );
 
718
 
 
719
  return 0;
 
720
 
 
721
 fail:
 
722
  log_error("invalid marker packet\n");
 
723
  iobuf_skip_rest(inp,pktlen,0);
 
724
  return G10ERR_INVALID_PACKET;
 
725
}
 
726
 
 
727
static int
 
728
parse_symkeyenc( IOBUF inp, int pkttype, unsigned long pktlen, PACKET *packet )
622
729
{
623
730
    PKT_symkey_enc *k;
624
731
    int rc = 0;
626
733
 
627
734
    if( pktlen < 4 ) {
628
735
        log_error("packet(%d) too short\n", pkttype);
629
 
        rc = GPG_ERR_INV_PACKET;
 
736
        rc = gpg_error (GPG_ERR_INV_PACKET);
630
737
        goto leave;
631
738
    }
632
739
    version = iobuf_get_noeof(inp); pktlen--;
633
740
    if( version != 4 ) {
634
741
        log_error("packet(%d) with unknown version %d\n", pkttype, version);
635
 
        rc = GPG_ERR_INV_PACKET;
 
742
        rc = gpg_error (GPG_ERR_INV_PACKET);
636
743
        goto leave;
637
744
    }
638
745
    if( pktlen > 200 ) { /* (we encode the seskeylen in a byte) */
639
746
        log_error("packet(%d) too large\n", pkttype);
640
 
        rc = GPG_ERR_INV_PACKET;
 
747
        rc = gpg_error (GPG_ERR_INV_PACKET);
641
748
        goto leave;
642
749
    }
643
750
    cipher_algo = iobuf_get_noeof(inp); pktlen--;
659
766
    }
660
767
    if( minlen > pktlen ) {
661
768
        log_error("packet with S2K %d too short\n", s2kmode );
662
 
        rc = GPG_ERR_INV_PACKET;
 
769
        rc = gpg_error (GPG_ERR_INV_PACKET);
663
770
        goto leave;
664
771
    }
665
772
    seskeylen = pktlen - minlen;
666
 
    k = packet->pkt.symkey_enc = xcalloc (1, sizeof *packet->pkt.symkey_enc
 
773
    k = packet->pkt.symkey_enc = xmalloc_clear( sizeof *packet->pkt.symkey_enc
667
774
                                                + seskeylen - 1 );
668
775
    k->version = version;
669
776
    k->cipher_algo = cipher_algo;
677
784
        k->s2k.count = iobuf_get(inp); pktlen--;
678
785
    }
679
786
    k->seskeylen = seskeylen;
680
 
    for(i=0; i < seskeylen && pktlen; i++, pktlen-- )
681
 
        k->seskey[i] = iobuf_get_noeof(inp);
 
787
    if(k->seskeylen)
 
788
      {
 
789
        for(i=0; i < seskeylen && pktlen; i++, pktlen-- )
 
790
          k->seskey[i] = iobuf_get_noeof(inp);
 
791
 
 
792
        /* What we're watching out for here is a session key decryptor
 
793
           with no salt.  The RFC says that using salt for this is a
 
794
           MUST. */
 
795
        if(s2kmode!=1 && s2kmode!=3)
 
796
          log_info(_("WARNING: potentially insecure symmetrically"
 
797
                     " encrypted session key\n"));
 
798
      }
682
799
    assert( !pktlen );
683
800
 
684
801
    if( list_mode ) {
685
 
        printf(":symkey enc packet: version %d, cipher %d, s2k %d, hash %d\n",
686
 
                            version, cipher_algo, s2kmode, hash_algo);
 
802
        fprintf (listfp, ":symkey enc packet: version %d, cipher %d, s2k %d, hash %d",
 
803
               version, cipher_algo, s2kmode, hash_algo);
 
804
        if(seskeylen)
 
805
          fprintf (listfp, ", seskey %d bits",(seskeylen-1)*8);
 
806
        fprintf (listfp, "\n");
687
807
        if( s2kmode == 1 || s2kmode == 3 ) {
688
 
            printf("\tsalt ");
 
808
            fprintf (listfp, "\tsalt ");
689
809
            for(i=0; i < 8; i++ )
690
 
                printf("%02x", k->s2k.salt[i]);
 
810
                fprintf (listfp, "%02x", k->s2k.salt[i]);
691
811
            if( s2kmode == 3 )
692
 
                printf(", count %lu\n", (ulong)k->s2k.count );
693
 
            printf("\n");
 
812
                fprintf (listfp, ", count %lu (%lu)",
 
813
                         S2K_DECODE_COUNT((ulong)k->s2k.count),
 
814
                         (ulong)k->s2k.count );
 
815
            fprintf (listfp, "\n");
694
816
        }
695
817
    }
696
818
 
697
819
  leave:
698
 
    skip_rest(inp, pktlen);
 
820
    iobuf_skip_rest(inp, pktlen, 0);
699
821
    return rc;
700
822
}
701
823
 
702
824
static int
703
 
parse_pubkeyenc( iobuf_t inp, int pkttype, unsigned long pktlen, PACKET *packet )
 
825
parse_pubkeyenc( IOBUF inp, int pkttype, unsigned long pktlen, PACKET *packet )
704
826
{
705
827
    unsigned int n;
706
828
    int rc = 0;
707
829
    int i, ndata;
708
830
    PKT_pubkey_enc *k;
709
831
 
710
 
    k = packet->pkt.pubkey_enc = xcalloc (1,sizeof *packet->pkt.pubkey_enc);
 
832
    k = packet->pkt.pubkey_enc = xmalloc_clear(sizeof *packet->pkt.pubkey_enc);
711
833
    if( pktlen < 12 ) {
712
834
        log_error("packet(%d) too short\n", pkttype);
713
 
        rc = GPG_ERR_INV_PACKET;
 
835
        rc = gpg_error (GPG_ERR_INV_PACKET);
714
836
        goto leave;
715
837
    }
716
838
    k->version = iobuf_get_noeof(inp); pktlen--;
717
839
    if( k->version != 2 && k->version != 3 ) {
718
840
        log_error("packet(%d) with unknown version %d\n", pkttype, k->version);
719
 
        rc = GPG_ERR_INV_PACKET;
 
841
        rc = gpg_error (GPG_ERR_INV_PACKET);
720
842
        goto leave;
721
843
    }
722
844
    k->keyid[0] = read_32(inp); pktlen -= 4;
724
846
    k->pubkey_algo = iobuf_get_noeof(inp); pktlen--;
725
847
    k->throw_keyid = 0; /* only used as flag for build_packet */
726
848
    if( list_mode )
727
 
        printf(":pubkey enc packet: version %d, algo %d, keyid %08lX%08lX\n",
 
849
        fprintf (listfp, ":pubkey enc packet: version %d, algo %d, keyid %08lX%08lX\n",
728
850
          k->version, k->pubkey_algo, (ulong)k->keyid[0], (ulong)k->keyid[1]);
729
851
 
730
852
    ndata = pubkey_get_nenc(k->pubkey_algo);
731
853
    if( !ndata ) {
732
854
        if( list_mode )
733
 
            printf("\tunsupported algorithm %d\n", k->pubkey_algo );
 
855
            fprintf (listfp, "\tunsupported algorithm %d\n", k->pubkey_algo );
734
856
        unknown_pubkey_warning( k->pubkey_algo );
735
857
        k->data[0] = NULL;  /* no need to store the encrypted data */
736
858
    }
739
861
            n = pktlen;
740
862
            k->data[i] = mpi_read(inp, &n, 0); pktlen -=n;
741
863
            if( list_mode ) {
742
 
                printf("\tdata: ");
743
 
                mpi_print(stdout, k->data[i], mpi_print_mode );
744
 
                putchar('\n');
 
864
                fprintf (listfp, "\tdata: ");
 
865
                mpi_print(listfp, k->data[i], mpi_print_mode );
 
866
                putc ('\n', listfp);
745
867
            }
746
868
            if (!k->data[i])
747
 
                rc = GPG_ERR_INV_PACKET;
 
869
                rc = gpg_error (GPG_ERR_INV_PACKET);
748
870
        }
749
871
    }
750
872
 
751
873
  leave:
752
 
    skip_rest(inp, pktlen);
 
874
    iobuf_skip_rest(inp, pktlen, 0);
753
875
    return rc;
754
876
}
755
877
 
765
887
     * detect the ARRs - we print our old message here when it is a faked
766
888
     * ARR and add an additional notice */
767
889
    if ( type == SIGSUBPKT_ARR && !hashed ) {
768
 
        printf("\tsubpkt %d len %u (additional recipient request)\n"
769
 
               "WARNING: PGP versions > 5.0 and < 6.5.8 will automagically "
770
 
               "encrypt to this key and thereby reveal the plaintext to "
771
 
               "the owner of this ARR key. Detailed info follows:\n",
772
 
               type, (unsigned)length );
 
890
        fprintf (listfp,
 
891
                 "\tsubpkt %d len %u (additional recipient request)\n"
 
892
                 "WARNING: PGP versions > 5.0 and < 6.5.8 will automagically "
 
893
                 "encrypt to this key and thereby reveal the plaintext to "
 
894
                 "the owner of this ARR key. Detailed info follows:\n",
 
895
                 type, (unsigned)length );
773
896
    }
774
897
    
775
898
    buffer++;
776
899
    length--;
777
900
   
778
 
    printf("\t%s%ssubpkt %d len %u (", /*)*/
 
901
    fprintf (listfp, "\t%s%ssubpkt %d len %u (", /*)*/
779
902
              critical ? "critical ":"",
780
903
              hashed ? "hashed ":"", type, (unsigned)length );
781
904
    if( length > buflen ) {
782
 
        printf("too short: buffer is only %u)\n", (unsigned)buflen );
 
905
        fprintf (listfp, "too short: buffer is only %u)\n", (unsigned)buflen );
783
906
        return;
784
907
    }
785
908
    switch( type ) {
786
909
      case SIGSUBPKT_SIG_CREATED:
787
910
        if( length >= 4 )
788
 
            printf("sig created %s", strtimestamp( buffer_to_u32(buffer) ) );
 
911
            fprintf (listfp, "sig created %s", strtimestamp( buffer_to_u32(buffer) ) );
789
912
        break;
790
913
      case SIGSUBPKT_SIG_EXPIRE:
791
914
        if( length >= 4 )
792
 
            printf("sig expires after %s",
793
 
                                     strtimevalue( buffer_to_u32(buffer) ) );
 
915
          {
 
916
            if(buffer_to_u32(buffer))
 
917
              fprintf (listfp, "sig expires after %s",
 
918
                       strtimevalue( buffer_to_u32(buffer) ) );
 
919
            else
 
920
              fprintf (listfp, "sig does not expire");
 
921
          }
794
922
        break;
795
923
      case SIGSUBPKT_EXPORTABLE:
796
924
        if( length )
797
 
            printf("%sexportable", *buffer? "":"not ");
 
925
            fprintf (listfp, "%sexportable", *buffer? "":"not ");
798
926
        break;
799
927
      case SIGSUBPKT_TRUST:
800
928
        if(length!=2)
801
929
          p="[invalid trust subpacket]";
802
930
        else
803
 
          printf("trust signature of depth %d, value %d",buffer[0],buffer[1]);
 
931
          fprintf (listfp, "trust signature of depth %d, value %d",buffer[0],buffer[1]);
804
932
        break;
805
933
      case SIGSUBPKT_REGEXP:
806
934
        if(!length)
807
935
          p="[invalid regexp subpacket]";
808
936
        else
809
 
          printf("regular expression: \"%s\"",buffer);
 
937
          fprintf (listfp, "regular expression: \"%s\"",buffer);
810
938
        break;
811
939
      case SIGSUBPKT_REVOCABLE:
812
940
        if( length )
813
 
            printf("%srevocable", *buffer? "":"not ");
 
941
            fprintf (listfp, "%srevocable", *buffer? "":"not ");
814
942
        break;
815
943
      case SIGSUBPKT_KEY_EXPIRE:
816
944
        if( length >= 4 )
817
 
            printf("key expires after %s",
818
 
                                    strtimevalue( buffer_to_u32(buffer) ) );
 
945
          {
 
946
            if(buffer_to_u32(buffer))
 
947
              fprintf (listfp, "key expires after %s",
 
948
                       strtimevalue( buffer_to_u32(buffer) ) );
 
949
            else
 
950
              fprintf (listfp, "key does not expire");
 
951
          }
819
952
        break;
820
953
      case SIGSUBPKT_PREF_SYM:
821
 
        fputs("pref-sym-algos:", stdout );
 
954
        fputs("pref-sym-algos:", listfp );
822
955
        for( i=0; i < length; i++ )
823
 
            printf(" %d", buffer[i] );
 
956
            fprintf (listfp, " %d", buffer[i] );
824
957
        break;
825
958
      case SIGSUBPKT_REV_KEY:
826
 
        fputs("revocation key: ", stdout );
 
959
        fputs("revocation key: ", listfp );
827
960
        if( length < 22 )
828
961
            p = "[too short]";
829
962
        else {
830
 
            printf("c=%02x a=%d f=", buffer[0], buffer[1] );
 
963
            fprintf (listfp, "c=%02x a=%d f=", buffer[0], buffer[1] );
831
964
            for( i=2; i < length; i++ )
832
 
                printf("%02X", buffer[i] );
 
965
                fprintf (listfp, "%02X", buffer[i] );
833
966
        }
834
967
        break;
835
968
      case SIGSUBPKT_ISSUER:
836
969
        if( length >= 8 )
837
 
            printf("issuer key ID %08lX%08lX",
 
970
            fprintf (listfp, "issuer key ID %08lX%08lX",
838
971
                      (ulong)buffer_to_u32(buffer),
839
972
                      (ulong)buffer_to_u32(buffer+4) );
840
973
        break;
841
974
      case SIGSUBPKT_NOTATION:
842
975
        {
843
 
            fputs("notation: ", stdout );
 
976
            fputs("notation: ", listfp );
844
977
            if( length < 8 )
845
978
                p = "[too short]";
846
979
            else {
853
986
                if( 8+n1+n2 != length )
854
987
                    p = "[error]";
855
988
                else {
856
 
                    print_string( stdout, s, n1, ')' );
857
 
                    putc( '=', stdout );
 
989
                    print_string( listfp, s, n1, ')' );
 
990
                    putc( '=', listfp );
858
991
 
859
992
                    if( *buffer & 0x80 )
860
 
                      print_string( stdout, s+n1, n2, ')' );
 
993
                      print_string( listfp, s+n1, n2, ')' );
861
994
                    else
862
995
                      p = "[not human readable]";
863
996
                }
865
998
        }
866
999
        break;
867
1000
      case SIGSUBPKT_PREF_HASH:
868
 
        fputs("pref-hash-algos:", stdout );
 
1001
        fputs("pref-hash-algos:", listfp );
869
1002
        for( i=0; i < length; i++ )
870
 
            printf(" %d", buffer[i] );
 
1003
            fprintf (listfp, " %d", buffer[i] );
871
1004
        break;
872
1005
      case SIGSUBPKT_PREF_COMPR:
873
 
        fputs("pref-zip-algos:", stdout );
 
1006
        fputs("pref-zip-algos:", listfp );
874
1007
        for( i=0; i < length; i++ )
875
 
            printf(" %d", buffer[i] );
 
1008
            fprintf (listfp, " %d", buffer[i] );
876
1009
        break;
877
1010
      case SIGSUBPKT_KS_FLAGS:
878
 
        fputs("key server preferences:",stdout);
 
1011
        fputs("key server preferences:",listfp);
879
1012
        for(i=0;i<length;i++)
880
 
          printf(" %02X", buffer[i]);
 
1013
          fprintf (listfp, " %02X", buffer[i]);
881
1014
        break;
882
1015
      case SIGSUBPKT_PREF_KS:
883
 
        fputs("preferred key server: ", stdout );
884
 
        print_string( stdout, buffer, length, ')' );
 
1016
        fputs("preferred key server: ", listfp );
 
1017
        print_string( listfp, buffer, length, ')' );
885
1018
        break;
886
1019
      case SIGSUBPKT_PRIMARY_UID:
887
1020
        p = "primary user ID";
888
1021
        break;
889
1022
      case SIGSUBPKT_POLICY:
890
 
        fputs("policy: ", stdout );
891
 
        print_string( stdout, buffer, length, ')' );
 
1023
        fputs("policy: ", listfp );
 
1024
        print_string( listfp, buffer, length, ')' );
892
1025
        break;
893
1026
      case SIGSUBPKT_KEY_FLAGS:
894
 
        fputs ( "key flags:", stdout );
 
1027
        fputs ( "key flags:", listfp );
895
1028
        for( i=0; i < length; i++ )
896
 
            printf(" %02X", buffer[i] );
 
1029
            fprintf (listfp, " %02X", buffer[i] );
897
1030
        break;
898
1031
      case SIGSUBPKT_SIGNERS_UID:
899
1032
        p = "signer's user ID";
900
1033
        break;
901
1034
      case SIGSUBPKT_REVOC_REASON:
902
1035
        if( length ) {
903
 
            printf("revocation reason 0x%02x (", *buffer );
904
 
            print_string( stdout, buffer+1, length-1, ')' );
 
1036
            fprintf (listfp, "revocation reason 0x%02x (", *buffer );
 
1037
            print_string( listfp, buffer+1, length-1, ')' );
905
1038
            p = ")";
906
1039
        }
907
1040
        break;
908
1041
      case SIGSUBPKT_ARR:
909
 
        fputs("Big Brother's key (ignored): ", stdout );
 
1042
        fputs("Big Brother's key (ignored): ", listfp );
910
1043
        if( length < 22 )
911
1044
            p = "[too short]";
912
1045
        else {
913
 
            printf("c=%02x a=%d f=", buffer[0], buffer[1] );
 
1046
            fprintf (listfp, "c=%02x a=%d f=", buffer[0], buffer[1] );
914
1047
            for( i=2; i < length; i++ )
915
 
                printf("%02X", buffer[i] );
 
1048
                fprintf (listfp, "%02X", buffer[i] );
916
1049
        }
917
1050
        break;
918
1051
      case SIGSUBPKT_FEATURES:
919
 
        fputs ( "features:", stdout );
 
1052
        fputs ( "features:", listfp );
920
1053
        for( i=0; i < length; i++ )
921
 
            printf(" %02x", buffer[i] );
 
1054
            fprintf (listfp, " %02x", buffer[i] );
 
1055
        break;
 
1056
      case SIGSUBPKT_SIGNATURE:
 
1057
        fputs("signature: ",listfp);
 
1058
        if(length<17)
 
1059
          p="[too short]";
 
1060
        else
 
1061
          fprintf (listfp, "v%d, class 0x%02X, algo %d, digest algo %d",
 
1062
                 buffer[0],
 
1063
                 buffer[0]==3?buffer[2]:buffer[1],
 
1064
                 buffer[0]==3?buffer[15]:buffer[2],
 
1065
                 buffer[0]==3?buffer[16]:buffer[3]);
922
1066
        break;
923
1067
      default:
924
1068
        if(type>=100 && type<=110)
928
1072
        break;
929
1073
    }
930
1074
 
931
 
    printf("%s)\n", p? p: "");
 
1075
    fprintf (listfp, "%s)\n", p? p: "");
932
1076
}
933
1077
 
934
1078
/****************
935
 
 * Returns: >= 0 offset into buffer
936
 
 *          -1 unknown type
937
 
 *          -2 unsupported type
938
 
 *          -3 subpacket too short
 
1079
 * Returns: >= 0 use this offset into buffer
 
1080
 *          -1 explicitly reject returning this type
 
1081
 *          -2 subpacket too short
939
1082
 */
940
1083
int
941
1084
parse_one_sig_subpkt( const byte *buffer, size_t n, int type )
942
1085
{
943
 
    switch( type ) {
944
 
      case SIGSUBPKT_REV_KEY:
945
 
        if(n < 22)
946
 
          break;
947
 
        return 0;
948
 
      case SIGSUBPKT_SIG_CREATED:
949
 
      case SIGSUBPKT_SIG_EXPIRE:
950
 
      case SIGSUBPKT_KEY_EXPIRE:
951
 
        if( n < 4 )
952
 
            break;
953
 
        return 0;
954
 
      case SIGSUBPKT_KEY_FLAGS:
955
 
      case SIGSUBPKT_KS_FLAGS:
956
 
      case SIGSUBPKT_PREF_SYM:
957
 
      case SIGSUBPKT_PREF_HASH:
958
 
      case SIGSUBPKT_PREF_COMPR:
959
 
      case SIGSUBPKT_POLICY:
960
 
      case SIGSUBPKT_PREF_KS:
961
 
      case SIGSUBPKT_FEATURES:
962
 
      case SIGSUBPKT_REGEXP:
963
 
        return 0;
964
 
      case SIGSUBPKT_EXPORTABLE:
965
 
      case SIGSUBPKT_REVOCABLE:
966
 
        if( !n )
967
 
            break;
968
 
        return 0;
969
 
      case SIGSUBPKT_ISSUER: /* issuer key ID */
970
 
        if( n < 8 )
971
 
            break;
972
 
        return 0;
973
 
      case SIGSUBPKT_NOTATION:
974
 
        if( n < 8 ) /* minimum length needed */
975
 
            break;
976
 
        return 0;
977
 
      case SIGSUBPKT_REVOC_REASON:
978
 
        if( !n  )
979
 
            break;
980
 
        return 0;
981
 
      case SIGSUBPKT_PRIMARY_UID:
982
 
          if ( n != 1 )
983
 
              break;
984
 
          return 0;   
985
 
      case SIGSUBPKT_TRUST:
986
 
          if ( n != 2 )
987
 
              break;
988
 
          return 0;
989
 
      default: return -1;
 
1086
  switch( type )
 
1087
    {
 
1088
    case SIGSUBPKT_REV_KEY:
 
1089
      if(n < 22)
 
1090
        break;
 
1091
      return 0;
 
1092
    case SIGSUBPKT_SIG_CREATED:
 
1093
    case SIGSUBPKT_SIG_EXPIRE:
 
1094
    case SIGSUBPKT_KEY_EXPIRE:
 
1095
      if( n < 4 )
 
1096
        break;
 
1097
      return 0;
 
1098
    case SIGSUBPKT_KEY_FLAGS:
 
1099
    case SIGSUBPKT_KS_FLAGS:
 
1100
    case SIGSUBPKT_PREF_SYM:
 
1101
    case SIGSUBPKT_PREF_HASH:
 
1102
    case SIGSUBPKT_PREF_COMPR:
 
1103
    case SIGSUBPKT_POLICY:
 
1104
    case SIGSUBPKT_PREF_KS:
 
1105
    case SIGSUBPKT_FEATURES:
 
1106
    case SIGSUBPKT_REGEXP:
 
1107
      return 0;
 
1108
    case SIGSUBPKT_SIGNATURE:
 
1109
    case SIGSUBPKT_EXPORTABLE:
 
1110
    case SIGSUBPKT_REVOCABLE:
 
1111
    case SIGSUBPKT_REVOC_REASON:
 
1112
      if( !n )
 
1113
        break;
 
1114
      return 0;
 
1115
    case SIGSUBPKT_ISSUER: /* issuer key ID */
 
1116
      if( n < 8 )
 
1117
        break;
 
1118
      return 0;
 
1119
    case SIGSUBPKT_NOTATION:
 
1120
      /* minimum length needed, and the subpacket must be well-formed
 
1121
         where the name length and value length all fit inside the
 
1122
         packet. */
 
1123
      if(n<8 || 8+((buffer[4]<<8)|buffer[5])+((buffer[6]<<8)|buffer[7]) != n)
 
1124
        break;
 
1125
      return 0;
 
1126
    case SIGSUBPKT_PRIMARY_UID:
 
1127
      if ( n != 1 )
 
1128
        break;
 
1129
      return 0;
 
1130
    case SIGSUBPKT_TRUST:
 
1131
      if ( n != 2 )
 
1132
        break;
 
1133
      return 0;
 
1134
    default: return 0;
990
1135
    }
991
 
    return -3;
992
 
}
993
 
 
 
1136
  return -2;
 
1137
}
 
1138
 
 
1139
/* Not many critical notations we understand yet... */
 
1140
static int
 
1141
can_handle_critical_notation(const byte *name,size_t len)
 
1142
{
 
1143
  if(len==32 && memcmp(name,"preferred-email-encoding@pgp.com",32)==0)
 
1144
    return 1;
 
1145
  if(len==21 && memcmp(name,"pka-address@gnupg.org",21)==0)
 
1146
    return 1;
 
1147
 
 
1148
  return 0;
 
1149
}
994
1150
 
995
1151
static int
996
1152
can_handle_critical( const byte *buffer, size_t n, int type )
997
1153
{
998
 
    switch( type ) {
999
 
      case SIGSUBPKT_NOTATION:
1000
 
        if( n >= 8 && (*buffer & 0x80) )
1001
 
            return 1; /* human readable is handled */
1002
 
        return 0;
1003
 
 
1004
 
      case SIGSUBPKT_SIG_CREATED:
1005
 
      case SIGSUBPKT_SIG_EXPIRE:
1006
 
      case SIGSUBPKT_KEY_EXPIRE:
1007
 
      case SIGSUBPKT_EXPORTABLE:
1008
 
      case SIGSUBPKT_REVOCABLE:
1009
 
      case SIGSUBPKT_REV_KEY:
1010
 
      case SIGSUBPKT_ISSUER:/* issuer key ID */
1011
 
      case SIGSUBPKT_PREF_SYM:
1012
 
      case SIGSUBPKT_PREF_HASH:
1013
 
      case SIGSUBPKT_PREF_COMPR:
1014
 
      case SIGSUBPKT_KEY_FLAGS:
1015
 
      case SIGSUBPKT_PRIMARY_UID:
1016
 
      case SIGSUBPKT_FEATURES:
1017
 
      case SIGSUBPKT_TRUST:
1018
 
      case SIGSUBPKT_REGEXP:
1019
 
        /* Is it enough to show the policy or keyserver? */
1020
 
      case SIGSUBPKT_POLICY:
1021
 
      case SIGSUBPKT_PREF_KS:
1022
 
        return 1;
1023
 
 
1024
 
      default:
1025
 
        return 0;
 
1154
  switch( type )
 
1155
    {
 
1156
    case SIGSUBPKT_NOTATION:
 
1157
      if(n>=8)
 
1158
        return can_handle_critical_notation(buffer+8,(buffer[4]<<8)|buffer[5]);
 
1159
      else
 
1160
        return 0;
 
1161
    case SIGSUBPKT_SIGNATURE:
 
1162
    case SIGSUBPKT_SIG_CREATED:
 
1163
    case SIGSUBPKT_SIG_EXPIRE:
 
1164
    case SIGSUBPKT_KEY_EXPIRE:
 
1165
    case SIGSUBPKT_EXPORTABLE:
 
1166
    case SIGSUBPKT_REVOCABLE:
 
1167
    case SIGSUBPKT_REV_KEY:
 
1168
    case SIGSUBPKT_ISSUER:/* issuer key ID */
 
1169
    case SIGSUBPKT_PREF_SYM:
 
1170
    case SIGSUBPKT_PREF_HASH:
 
1171
    case SIGSUBPKT_PREF_COMPR:
 
1172
    case SIGSUBPKT_KEY_FLAGS:
 
1173
    case SIGSUBPKT_PRIMARY_UID:
 
1174
    case SIGSUBPKT_FEATURES:
 
1175
    case SIGSUBPKT_TRUST:
 
1176
    case SIGSUBPKT_REGEXP:
 
1177
      /* Is it enough to show the policy or keyserver? */
 
1178
    case SIGSUBPKT_POLICY:
 
1179
    case SIGSUBPKT_PREF_KS:
 
1180
      return 1;
 
1181
 
 
1182
    default:
 
1183
      return 0;
1026
1184
    }
1027
1185
}
1028
1186
 
1106
1264
                *ret_n = n;
1107
1265
            offset = parse_one_sig_subpkt(buffer, n, type );
1108
1266
            switch( offset ) {
1109
 
              case -3:
 
1267
              case -2:
1110
1268
                log_error("subpacket of type %d too short\n", type);
1111
1269
                return NULL;
1112
 
              case -2:
1113
 
                return NULL;
1114
1270
              case -1:
1115
 
                BUG(); /* not yet needed */
 
1271
                return NULL;
1116
1272
              default:
1117
1273
                break;
1118
1274
            }
1130
1286
    return NULL; /* end of packets; not found */
1131
1287
 
1132
1288
  too_short:
1133
 
    log_error("buffer shorter than subpacket\n");
 
1289
    if(opt.verbose)
 
1290
      log_info("buffer shorter than subpacket\n");
1134
1291
    if( start )
1135
1292
        *start = -1;
1136
1293
    return NULL;
1182
1339
    }
1183
1340
}
1184
1341
 
1185
 
static int
1186
 
parse_signature( iobuf_t inp, int pkttype, unsigned long pktlen,
 
1342
int
 
1343
parse_signature( IOBUF inp, int pkttype, unsigned long pktlen,
1187
1344
                                          PKT_signature *sig )
1188
1345
{
1189
1346
    int md5_len=0;
1200
1357
    if( sig->version == 4 )
1201
1358
        is_v4=1;
1202
1359
    else if( sig->version != 2 && sig->version != 3 ) {
1203
 
        log_error("packet(%d) with unknown version %d\n", pkttype, sig->version);
1204
 
        rc = GPG_ERR_INV_PACKET;
 
1360
        log_error("packet(%d) with unknown version %d\n",
 
1361
                  pkttype, sig->version);
 
1362
        rc = gpg_error (GPG_ERR_INV_PACKET);
1205
1363
        goto leave;
1206
1364
    }
1207
1365
 
1222
1380
        n = read_16(inp); pktlen -= 2; /* length of hashed data */
1223
1381
        if( n > 10000 ) {
1224
1382
            log_error("signature packet: hashed data too long\n");
1225
 
            rc = GPG_ERR_INV_PACKET;
 
1383
            rc = G10ERR_INVALID_PACKET;
1226
1384
            goto leave;
1227
1385
        }
1228
1386
        if( n ) {
1240
1398
        n = read_16(inp); pktlen -= 2; /* length of unhashed data */
1241
1399
        if( n > 10000 ) {
1242
1400
            log_error("signature packet: unhashed data too long\n");
1243
 
            rc = GPG_ERR_INV_PACKET;
 
1401
            rc = G10ERR_INVALID_PACKET;
1244
1402
            goto leave;
1245
1403
        }
1246
1404
        if( n ) {
1259
1417
 
1260
1418
    if( pktlen < 5 ) { /* sanity check */
1261
1419
        log_error("packet(%d) too short\n", pkttype);
1262
 
        rc = GPG_ERR_INV_PACKET;
 
1420
        rc = G10ERR_INVALID_PACKET;
1263
1421
        goto leave;
1264
1422
    }
1265
1423
 
1266
1424
    sig->digest_start[0] = iobuf_get_noeof(inp); pktlen--;
1267
1425
    sig->digest_start[1] = iobuf_get_noeof(inp); pktlen--;
1268
1426
 
1269
 
    if( is_v4 && sig->pubkey_algo ) { /*extract required information */
 
1427
    if( is_v4 && sig->pubkey_algo )
 
1428
      { /*extract required information */
1270
1429
        const byte *p;
1271
1430
        size_t len;
1272
1431
 
1273
1432
        /* set sig->flags.unknown_critical if there is a
1274
1433
         * critical bit set for packets which we do not understand */
1275
1434
        if( !parse_sig_subpkt (sig->hashed, SIGSUBPKT_TEST_CRITICAL, NULL)
1276
 
           || !parse_sig_subpkt (sig->unhashed, SIGSUBPKT_TEST_CRITICAL,
1277
 
                                                                        NULL) )
1278
 
        {
1279
 
            sig->flags.unknown_critical = 1;
1280
 
        }
 
1435
            || !parse_sig_subpkt (sig->unhashed, SIGSUBPKT_TEST_CRITICAL,
 
1436
                                  NULL) )
 
1437
          sig->flags.unknown_critical = 1;
1281
1438
 
1282
1439
        p = parse_sig_subpkt (sig->hashed, SIGSUBPKT_SIG_CREATED, NULL );
1283
1440
        if(p)
1284
1441
          sig->timestamp = buffer_to_u32(p);
1285
 
        else if(!(sig->pubkey_algo>=100 && sig->pubkey_algo<=110))
1286
 
          log_error("signature packet without timestamp\n");
 
1442
        else if(!(sig->pubkey_algo>=100 && sig->pubkey_algo<=110)
 
1443
                && opt.verbose)
 
1444
          log_info ("signature packet without timestamp\n");
1287
1445
 
1288
1446
        p = parse_sig_subpkt2( sig, SIGSUBPKT_ISSUER, NULL );
1289
 
        if( p )
1290
 
        {
1291
 
          sig->keyid[0] = buffer_to_u32(p);
1292
 
          sig->keyid[1] = buffer_to_u32(p+4);
1293
 
        }
1294
 
        else if(!(sig->pubkey_algo>=100 && sig->pubkey_algo<=110))
1295
 
          log_error("signature packet without keyid\n");
 
1447
        if(p)
 
1448
          {
 
1449
            sig->keyid[0] = buffer_to_u32(p);
 
1450
            sig->keyid[1] = buffer_to_u32(p+4);
 
1451
          }
 
1452
        else if(!(sig->pubkey_algo>=100 && sig->pubkey_algo<=110)
 
1453
                && opt.verbose)
 
1454
          log_info ("signature packet without keyid\n");
1296
1455
 
1297
1456
        p=parse_sig_subpkt(sig->hashed,SIGSUBPKT_SIG_EXPIRE,NULL);
1298
 
        if(p)
 
1457
        if(p && buffer_to_u32(p))
1299
1458
          sig->expiredate=sig->timestamp+buffer_to_u32(p);
1300
1459
        if(sig->expiredate && sig->expiredate<=make_timestamp())
1301
 
            sig->flags.expired=1;
 
1460
          sig->flags.expired=1;
1302
1461
 
1303
1462
        p=parse_sig_subpkt(sig->hashed,SIGSUBPKT_POLICY,NULL);
1304
1463
        if(p)
1345
1504
        /* Find all revocation keys. */
1346
1505
        if(sig->sig_class==0x1F)
1347
1506
          parse_revkeys(sig);
1348
 
    }
 
1507
      }
1349
1508
 
1350
1509
    if( list_mode ) {
1351
 
        printf(":signature packet: algo %d, keyid %08lX%08lX\n"
1352
 
               "\tversion %d, created %lu, md5len %d, sigclass %02x\n"
 
1510
        fprintf (listfp, ":signature packet: algo %d, keyid %08lX%08lX\n"
 
1511
               "\tversion %d, created %lu, md5len %d, sigclass 0x%02x\n"
1353
1512
               "\tdigest algo %d, begin of digest %02x %02x\n",
1354
1513
                sig->pubkey_algo,
1355
1514
                (ulong)sig->keyid[0], (ulong)sig->keyid[1],
1365
1524
    ndata = pubkey_get_nsig(sig->pubkey_algo);
1366
1525
    if( !ndata ) {
1367
1526
        if( list_mode )
1368
 
            printf("\tunknown algorithm %d\n", sig->pubkey_algo );
 
1527
            fprintf (listfp, "\tunknown algorithm %d\n", sig->pubkey_algo );
1369
1528
        unknown_pubkey_warning( sig->pubkey_algo );
1370
 
        /* we store the plain material in data[0], so that we are able
 
1529
        /* We store the plain material in data[0], so that we are able
1371
1530
         * to write it back with build_packet() */
1372
 
        sig->data[0] = gcry_mpi_set_opaque(NULL, read_rest(inp, pktlen),
1373
 
                                           pktlen*8 );
1374
 
        pktlen = 0;
 
1531
        if (pktlen > (5 * MAX_EXTERN_MPI_BITS/8))
 
1532
          {
 
1533
            /* However we include a limit to avoid too trivial DoS
 
1534
               attacks by having gpg allocate too much memory.  */
 
1535
            log_error ("signature packet: too much data\n");
 
1536
            rc = G10ERR_INVALID_PACKET;
 
1537
          }
 
1538
        else
 
1539
          {
 
1540
            sig->data[0]= gcry_mpi_set_opaque (NULL, read_rest(inp, pktlen, 0),
 
1541
                                               pktlen*8 );
 
1542
            pktlen = 0;
 
1543
          }
1375
1544
    }
1376
1545
    else {
1377
1546
        for( i=0; i < ndata; i++ ) {
1379
1548
            sig->data[i] = mpi_read(inp, &n, 0 );
1380
1549
            pktlen -=n;
1381
1550
            if( list_mode ) {
1382
 
                printf("\tdata: ");
1383
 
                mpi_print(stdout, sig->data[i], mpi_print_mode );
1384
 
                putchar('\n');
 
1551
                fprintf (listfp, "\tdata: ");
 
1552
                mpi_print(listfp, sig->data[i], mpi_print_mode );
 
1553
                putc ('\n', listfp);
1385
1554
            }
1386
1555
            if (!sig->data[i])
1387
 
                rc = GPG_ERR_INV_PACKET;
 
1556
                rc = G10ERR_INVALID_PACKET;
1388
1557
        }
1389
1558
    }
1390
1559
 
1391
1560
  leave:
1392
 
    skip_rest(inp, pktlen);
 
1561
    iobuf_skip_rest(inp, pktlen, 0);
1393
1562
    return rc;
1394
1563
}
1395
1564
 
1396
1565
 
1397
1566
static int
1398
 
parse_onepass_sig( iobuf_t inp, int pkttype, unsigned long pktlen,
 
1567
parse_onepass_sig( IOBUF inp, int pkttype, unsigned long pktlen,
1399
1568
                                             PKT_onepass_sig *ops )
1400
1569
{
1401
1570
    int version;
1403
1572
 
1404
1573
    if( pktlen < 13 ) {
1405
1574
        log_error("packet(%d) too short\n", pkttype);
1406
 
        rc = GPG_ERR_INV_PACKET;
 
1575
        rc = gpg_error (GPG_ERR_INV_PACKET);
1407
1576
        goto leave;
1408
1577
    }
1409
1578
    version = iobuf_get_noeof(inp); pktlen--;
1410
1579
    if( version != 3 ) {
1411
1580
        log_error("onepass_sig with unknown version %d\n", version);
1412
 
        rc = GPG_ERR_INV_PACKET;
 
1581
        rc = gpg_error (GPG_ERR_INV_PACKET);
1413
1582
        goto leave;
1414
1583
    }
1415
1584
    ops->sig_class = iobuf_get_noeof(inp); pktlen--;
1419
1588
    ops->keyid[1] = read_32(inp); pktlen -= 4;
1420
1589
    ops->last = iobuf_get_noeof(inp); pktlen--;
1421
1590
    if( list_mode )
1422
 
        printf(":onepass_sig packet: keyid %08lX%08lX\n"
1423
 
               "\tversion %d, sigclass %02x, digest %d, pubkey %d, last=%d\n",
 
1591
        fprintf (listfp,
 
1592
                 ":onepass_sig packet: keyid %08lX%08lX\n"
 
1593
                 "\tversion %d, sigclass 0x%02x, digest %d, pubkey %d, "
 
1594
                 "last=%d\n",
1424
1595
                (ulong)ops->keyid[0], (ulong)ops->keyid[1],
1425
1596
                version, ops->sig_class,
1426
1597
                ops->digest_algo, ops->pubkey_algo, ops->last );
1427
1598
 
1428
1599
 
1429
1600
  leave:
1430
 
    skip_rest(inp, pktlen);
 
1601
    iobuf_skip_rest(inp, pktlen, 0);
1431
1602
    return rc;
1432
1603
}
1433
1604
 
1434
1605
 
1435
1606
static gcry_mpi_t
1436
 
read_protected_v3_mpi (iobuf_t inp, unsigned long *length)
 
1607
read_protected_v3_mpi (IOBUF inp, unsigned long *length)
1437
1608
{
1438
1609
  int c;
1439
1610
  unsigned int nbits, nbytes;
1464
1635
  buf = p = xmalloc (2 + nbytes);
1465
1636
  *p++ = nbits >> 8;
1466
1637
  *p++ = nbits;
1467
 
  for (; nbytes && length; nbytes--, --*length)
 
1638
  for (; nbytes && *length; nbytes--, --*length)
1468
1639
    *p++ = iobuf_get (inp);
1469
1640
  if (nbytes)
1470
1641
    {
1471
 
      log_error ("packet shorter tham mpi\n");
 
1642
      log_error ("packet shorter than mpi\n");
1472
1643
      xfree (buf);
1473
1644
      return NULL;
1474
1645
    }
1475
1646
 
1476
 
  /* convert buffer into an opaque gcry_mpi_t */
 
1647
  /* convert buffer into an opaque MPI */
1477
1648
  val = gcry_mpi_set_opaque (NULL, buf, (p-buf)*8); 
1478
1649
  return val;
1479
1650
}
1480
1651
 
1481
1652
 
1482
1653
static int
1483
 
parse_key( iobuf_t inp, int pkttype, unsigned long pktlen,
 
1654
parse_key( IOBUF inp, int pkttype, unsigned long pktlen,
1484
1655
                              byte *hdr, int hdrlen, PACKET *pkt )
1485
1656
{
1486
1657
    int i, version, algorithm;
1495
1666
        /* early versions of G10 use old PGP comments packets;
1496
1667
         * luckily all those comments are started by a hash */
1497
1668
        if( list_mode ) {
1498
 
            printf(":rfc1991 comment packet: \"" );
 
1669
            fprintf (listfp, ":rfc1991 comment packet: \"" );
1499
1670
            for( ; pktlen; pktlen-- ) {
1500
1671
                int c;
1501
1672
                c = iobuf_get_noeof(inp);
1502
1673
                if( c >= ' ' && c <= 'z' )
1503
 
                    putchar(c);
 
1674
                    putc (c, listfp);
1504
1675
                else
1505
 
                    printf("\\x%02x", c );
 
1676
                    fprintf (listfp, "\\x%02x", c );
1506
1677
            }
1507
 
            printf("\"\n");
 
1678
            fprintf (listfp, "\"\n");
1508
1679
        }
1509
 
        skip_rest(inp, pktlen);
 
1680
        iobuf_skip_rest(inp, pktlen, 0);
1510
1681
        return 0;
1511
1682
    }
1512
1683
    else if( version == 4 )
1513
1684
        is_v4=1;
1514
1685
    else if( version != 2 && version != 3 ) {
1515
1686
        log_error("packet(%d) with unknown version %d\n", pkttype, version);
1516
 
        rc = GPG_ERR_INV_PACKET;
 
1687
        rc = gpg_error (GPG_ERR_INV_PACKET);
1517
1688
        goto leave;
1518
1689
    }
1519
1690
 
1520
1691
    if( pktlen < 11 ) {
1521
1692
        log_error("packet(%d) too short\n", pkttype);
1522
 
        rc = GPG_ERR_INV_PACKET;
 
1693
        rc = gpg_error (GPG_ERR_INV_PACKET);
1523
1694
        goto leave;
1524
1695
    }
1525
1696
 
1540
1711
    }
1541
1712
    algorithm = iobuf_get_noeof(inp); pktlen--;
1542
1713
    if( list_mode )
1543
 
        printf(":%s key packet:\n"
 
1714
        fprintf (listfp, ":%s key packet:\n"
1544
1715
               "\tversion %d, algo %d, created %lu, expires %lu\n",
1545
1716
                pkttype == PKT_PUBLIC_KEY? "public" :
1546
1717
                pkttype == PKT_SECRET_KEY? "secret" :
1582
1753
    npkey = pubkey_get_npkey( algorithm );
1583
1754
    if( !npkey ) {
1584
1755
        if( list_mode )
1585
 
            printf("\tunknown algorithm %d\n", algorithm );
 
1756
            fprintf (listfp, "\tunknown algorithm %d\n", algorithm );
1586
1757
        unknown_pubkey_warning( algorithm );
1587
1758
    }
1588
1759
 
1593
1764
        size_t snlen = 0;
1594
1765
 
1595
1766
        if( !npkey ) {
1596
 
            sk->skey[0] = gcry_mpi_set_opaque( NULL, read_rest(inp, pktlen),
 
1767
            sk->skey[0] = gcry_mpi_set_opaque (NULL, read_rest(inp, pktlen, 0),
1597
1768
                                               pktlen*8 );
1598
1769
            pktlen = 0;
1599
1770
            goto leave;
1602
1773
        for(i=0; i < npkey; i++ ) {
1603
1774
            n = pktlen; sk->skey[i] = mpi_read(inp, &n, 0 ); pktlen -=n;
1604
1775
            if( list_mode ) {
1605
 
                printf(  "\tskey[%d]: ", i);
1606
 
                mpi_print(stdout, sk->skey[i], mpi_print_mode  );
1607
 
                putchar('\n');
 
1776
                fprintf (listfp,   "\tskey[%d]: ", i);
 
1777
                mpi_print(listfp, sk->skey[i], mpi_print_mode  );
 
1778
                putc ('\n', listfp);
1608
1779
            }
1609
1780
            if (!sk->skey[i])
1610
 
                rc = GPG_ERR_INV_PACKET;
 
1781
                rc = G10ERR_INVALID_PACKET;
1611
1782
        }
1612
1783
        if (rc) /* one of the MPIs were bad */
1613
1784
            goto leave;
1618
1789
            sk->protect.s2k.count = 0;
1619
1790
            if( sk->protect.algo == 254 || sk->protect.algo == 255 ) {
1620
1791
                if( pktlen < 3 ) {
1621
 
                    rc = GPG_ERR_INV_PACKET;
 
1792
                    rc = G10ERR_INVALID_PACKET;
1622
1793
                    goto leave;
1623
1794
                }
1624
1795
                sk->protect.sha1chk = (sk->protect.algo == 254);
1634
1805
                        temp[i] = iobuf_get_noeof(inp);
1635
1806
                    if( i < 4 || memcmp( temp, "GNU", 3 ) ) {
1636
1807
                        if( list_mode )
1637
 
                            printf(  "\tunknown S2K %d\n",
 
1808
                            fprintf (listfp,   "\tunknown S2K %d\n",
1638
1809
                                                sk->protect.s2k.mode );
1639
 
                        rc = GPG_ERR_INV_PACKET;
 
1810
                        rc = G10ERR_INVALID_PACKET;
1640
1811
                        goto leave;
1641
1812
                    }
1642
1813
                    /* here we know that it is a gnu extension
1655
1826
                    break;
1656
1827
                }
1657
1828
                switch( sk->protect.s2k.mode ) {
1658
 
                  case 0: if( list_mode ) printf(  "\tsimple S2K" );
1659
 
                    break;
1660
 
                  case 1: if( list_mode ) printf(  "\tsalted S2K" );
1661
 
                    break;
1662
 
                  case 3: if( list_mode ) printf(  "\titer+salt S2K" );
1663
 
                    break;
1664
 
                  case 1001: if( list_mode ) printf(  "\tgnu-dummy S2K" );
1665
 
                    break;
1666
 
                  case 1002: if (list_mode) printf("\tgnu-divert-to-card S2K");
 
1829
                  case 0: if( list_mode ) fprintf (listfp, "\tsimple S2K" );
 
1830
                    break;
 
1831
                  case 1: if( list_mode ) fprintf (listfp, "\tsalted S2K" );
 
1832
                    break;
 
1833
                  case 3: if( list_mode ) fprintf (listfp, "\titer+salt S2K" );
 
1834
                    break;
 
1835
                  case 1001: if( list_mode ) fprintf (listfp,
 
1836
                                                      "\tgnu-dummy S2K" );
 
1837
                    break;
 
1838
                  case 1002: if (list_mode) fprintf (listfp,
 
1839
                                                  "\tgnu-divert-to-card S2K");
1667
1840
                    break;
1668
1841
                  default:
1669
1842
                    if( list_mode )
1670
 
                        printf(  "\tunknown %sS2K %d\n",
 
1843
                        fprintf (listfp,   "\tunknown %sS2K %d\n",
1671
1844
                                 sk->protect.s2k.mode < 1000? "":"GNU ",
1672
1845
                                                   sk->protect.s2k.mode );
1673
 
                    rc = GPG_ERR_INV_PACKET;
 
1846
                    rc = G10ERR_INVALID_PACKET;
1674
1847
                    goto leave;
1675
1848
                }
1676
1849
 
1677
1850
                if( list_mode ) {
1678
 
                    printf(", algo: %d,%s hash: %d",
 
1851
                    fprintf (listfp, ", algo: %d,%s hash: %d",
1679
1852
                                     sk->protect.algo,
1680
1853
                                     sk->protect.sha1chk?" SHA1 protection,"
1681
1854
                                                        :" simple checksum,",
1682
1855
                                     sk->protect.s2k.hash_algo );
1683
1856
                    if( sk->protect.s2k.mode == 1
1684
1857
                        || sk->protect.s2k.mode == 3 ) {
1685
 
                        printf(", salt: ");
 
1858
                        fprintf (listfp, ", salt: ");
1686
1859
                        for(i=0; i < 8; i++ )
1687
 
                            printf("%02x", sk->protect.s2k.salt[i]);
 
1860
                            fprintf (listfp, "%02x", sk->protect.s2k.salt[i]);
1688
1861
                    }
1689
 
                    putchar('\n');
 
1862
                    putc ('\n', listfp);
1690
1863
                }
1691
1864
 
1692
1865
                if( sk->protect.s2k.mode == 3 ) {
1693
1866
                    if( pktlen < 1 ) {
1694
 
                        rc = GPG_ERR_INV_PACKET;
 
1867
                        rc = G10ERR_INVALID_PACKET;
1695
1868
                        goto leave;
1696
1869
                    }
1697
1870
                    sk->protect.s2k.count = iobuf_get(inp);
1698
1871
                    pktlen--;
1699
1872
                    if( list_mode )
1700
 
                        printf("\tprotect count: %lu\n",
 
1873
                        fprintf (listfp, "\tprotect count: %lu\n",
1701
1874
                                            (ulong)sk->protect.s2k.count);
1702
1875
                }
1703
1876
                else if( sk->protect.s2k.mode == 1002 ) {
1704
1877
                    /* Read the serial number. */
1705
1878
                    if (pktlen < 1) {
1706
 
                        rc = GPG_ERR_INV_PACKET;
 
1879
                      rc = G10ERR_INVALID_PACKET;
1707
1880
                        goto leave;
1708
1881
                    }
1709
1882
                    snlen = iobuf_get (inp);
1710
1883
                    pktlen--;
1711
1884
                    if (pktlen < snlen || snlen == -1) {
1712
 
                        rc = GPG_ERR_INV_PACKET;
 
1885
                        rc = G10ERR_INVALID_PACKET;
1713
1886
                        goto leave;
1714
1887
                    }
1715
1888
                }
1721
1894
                sk->protect.s2k.mode = 0;
1722
1895
                sk->protect.s2k.hash_algo = DIGEST_ALGO_MD5;
1723
1896
                if( list_mode )
1724
 
                    printf(  "\tprotect algo: %d  (hash algo: %d)\n",
 
1897
                    fprintf (listfp,   "\tprotect algo: %d  (hash algo: %d)\n",
1725
1898
                         sk->protect.algo, sk->protect.s2k.hash_algo );
1726
1899
            }
1727
1900
            /* It is really ugly that we don't know the size
1733
1906
             * enlarge temp.
1734
1907
             */
1735
1908
            switch( sk->protect.algo ) {
1736
 
              case 7: case 8: case 9: /* reserved for AES */
 
1909
              case 7: case 8: case 9: /* AES */
1737
1910
              case 10: /* Twofish */
 
1911
              case 11: case 12: /* Camellia */
1738
1912
                sk->protect.ivlen = 16;
1739
1913
                break;
1740
1914
              default:
1742
1916
            }
1743
1917
            if( sk->protect.s2k.mode == 1001 )
1744
1918
                sk->protect.ivlen = 0;
1745
 
            else if( sk->protect.s2k.mode == 1002 ) {
1746
 
                if (snlen > 16)
1747
 
                    log_info ("WARNING: serial number of card truncated\n");
 
1919
            else if( sk->protect.s2k.mode == 1002 )
1748
1920
                sk->protect.ivlen = snlen < 16? snlen : 16;
1749
1921
 
1750
 
            }
1751
1922
            if( pktlen < sk->protect.ivlen ) {
1752
 
                rc = GPG_ERR_INV_PACKET;
 
1923
                rc = G10ERR_INVALID_PACKET;
1753
1924
                goto leave;
1754
1925
            }
1755
1926
            for(i=0; i < sk->protect.ivlen && pktlen; i++, pktlen-- )
1756
1927
                temp[i] = iobuf_get_noeof(inp);
1757
1928
            if( list_mode ) {
1758
 
                printf( sk->protect.s2k.mode == 1002? "\tserial-number: "
1759
 
                                                    : "\tprotect IV: ");
 
1929
                fprintf (listfp,
 
1930
                         sk->protect.s2k.mode == 1002? "\tserial-number: "
 
1931
                                                     : "\tprotect IV: ");
1760
1932
                for(i=0; i < sk->protect.ivlen; i++ )
1761
 
                    printf(" %02x", temp[i] );
1762
 
                putchar('\n');
 
1933
                    fprintf (listfp, " %02x", temp[i] );
 
1934
                putc ('\n', listfp);
1763
1935
            }
1764
1936
            memcpy(sk->protect.iv, temp, sk->protect.ivlen );
1765
1937
        }
1769
1941
         * If the user is so careless, not to protect his secret key,
1770
1942
         * we can assume, that he operates an open system :=(.
1771
1943
         * So we put the key into secure memory when we unprotect it. */
1772
 
        if( sk->protect.s2k.mode == 1001
 
1944
        if( sk->protect.s2k.mode == 1001 
1773
1945
            || sk->protect.s2k.mode == 1002 ) {
1774
1946
            /* better set some dummy stuff here */
1775
 
            sk->skey[npkey] = gcry_mpi_set_opaque(NULL, xstrdup ("dummydata"),
1776
 
                                                  10*8);
 
1947
            sk->skey[npkey] = gcry_mpi_set_opaque(NULL,
 
1948
                                                  xstrdup("dummydata"), 10*8);
1777
1949
            pktlen = 0;
1778
1950
        }
1779
1951
        else if( is_v4 && sk->is_protected ) {
1780
1952
            /* ugly; the length is encrypted too, so we read all
1781
1953
             * stuff up to the end of the packet into the first
1782
1954
             * skey element */
1783
 
            sk->skey[npkey] = gcry_mpi_set_opaque(NULL, read_rest(inp, pktlen),
1784
 
                                                  pktlen*8 );
 
1955
            sk->skey[npkey] = gcry_mpi_set_opaque (NULL,
 
1956
                                                   read_rest(inp, pktlen, 0),
 
1957
                                                   pktlen*8);
1785
1958
            pktlen = 0;
1786
1959
            if( list_mode ) {
1787
 
                printf("\tencrypted stuff follows\n");
 
1960
                fprintf (listfp, "\tencrypted stuff follows\n");
1788
1961
            }
1789
1962
        }
1790
1963
        else { /* v3 method: the mpi length is not encrypted */
1792
1965
                if ( sk->is_protected ) {
1793
1966
                    sk->skey[i] = read_protected_v3_mpi (inp, &pktlen);
1794
1967
                    if( list_mode ) 
1795
 
                        printf(  "\tskey[%d]: [encrypted]\n", i);
 
1968
                        fprintf (listfp,   "\tskey[%d]: [encrypted]\n", i);
1796
1969
                }
1797
1970
                else {
1798
1971
                    n = pktlen;
1799
1972
                    sk->skey[i] = mpi_read(inp, &n, 0 );
1800
1973
                    pktlen -=n;
1801
1974
                    if( list_mode ) {
1802
 
                        printf(  "\tskey[%d]: ", i);
1803
 
                        mpi_print(stdout, sk->skey[i], mpi_print_mode  );
1804
 
                        putchar('\n');
 
1975
                        fprintf (listfp,   "\tskey[%d]: ", i);
 
1976
                        mpi_print(listfp, sk->skey[i], mpi_print_mode  );
 
1977
                        putc ('\n', listfp);
1805
1978
                    }
1806
1979
                }
1807
1980
 
1808
1981
                if (!sk->skey[i])
1809
 
                    rc = GPG_ERR_INV_PACKET;
 
1982
                    rc = G10ERR_INVALID_PACKET;
1810
1983
            }
1811
1984
            if (rc)
1812
1985
                goto leave;
1813
1986
 
1814
1987
            sk->csum = read_16(inp); pktlen -= 2;
1815
1988
            if( list_mode ) {
1816
 
                printf("\tchecksum: %04hx\n", sk->csum);
 
1989
                fprintf (listfp, "\tchecksum: %04hx\n", sk->csum);
1817
1990
            }
1818
1991
        }
1819
1992
    }
1821
1994
        PKT_public_key *pk = pkt->pkt.public_key;
1822
1995
 
1823
1996
        if( !npkey ) {
1824
 
            pk->pkey[0] = gcry_mpi_set_opaque( NULL, read_rest(inp, pktlen),
1825
 
                                               pktlen*8 );
 
1997
            pk->pkey[0] = gcry_mpi_set_opaque ( NULL,
 
1998
                                                read_rest(inp, pktlen, 0),
 
1999
                                                pktlen*8 );
1826
2000
            pktlen = 0;
1827
2001
            goto leave;
1828
2002
        }
1830
2004
        for(i=0; i < npkey; i++ ) {
1831
2005
            n = pktlen; pk->pkey[i] = mpi_read(inp, &n, 0 ); pktlen -=n;
1832
2006
            if( list_mode ) {
1833
 
                printf(  "\tpkey[%d]: ", i);
1834
 
                mpi_print(stdout, pk->pkey[i], mpi_print_mode  );
1835
 
                putchar('\n');
 
2007
                fprintf (listfp,   "\tpkey[%d]: ", i);
 
2008
                mpi_print(listfp, pk->pkey[i], mpi_print_mode  );
 
2009
                putc ('\n', listfp);
1836
2010
            }
1837
2011
            if (!pk->pkey[i])
1838
 
                rc = GPG_ERR_INV_PACKET;
 
2012
                rc = G10ERR_INVALID_PACKET;
1839
2013
        }
1840
2014
        if (rc)
1841
2015
            goto leave;
1842
2016
    }
1843
2017
 
1844
2018
  leave:
1845
 
    skip_rest(inp, pktlen);
 
2019
    iobuf_skip_rest(inp, pktlen, 0);
1846
2020
    return rc;
1847
2021
}
1848
2022
 
1859
2033
  int buflen=uid->attrib_len;
1860
2034
  byte type;
1861
2035
 
1862
 
  xfree (uid->attribs);
 
2036
  xfree(uid->attribs);
1863
2037
 
1864
2038
  while(buflen)
1865
2039
    {
1903
2077
  return count;
1904
2078
 
1905
2079
 too_short:
1906
 
  log_error("buffer shorter than attribute subpacket\n");
 
2080
  if(opt.verbose)
 
2081
    log_info("buffer shorter than attribute subpacket\n");
1907
2082
  uid->attribs=attribs;
1908
2083
  uid->numattribs=count;
1909
2084
  return count;
1910
2085
}
1911
2086
 
1912
 
static void setup_user_id(PACKET *packet)
1913
 
{
1914
 
  packet->pkt.user_id->ref = 1;
1915
 
  packet->pkt.user_id->attribs = NULL;
1916
 
  packet->pkt.user_id->attrib_data = NULL;
1917
 
  packet->pkt.user_id->attrib_len = 0;
1918
 
  packet->pkt.user_id->is_primary = 0;
1919
 
  packet->pkt.user_id->is_revoked = 0;
1920
 
  packet->pkt.user_id->is_expired = 0;
1921
 
  packet->pkt.user_id->expiredate = 0;
1922
 
  packet->pkt.user_id->created = 0;
1923
 
  packet->pkt.user_id->help_key_usage = 0;
1924
 
  packet->pkt.user_id->help_key_expire = 0;
1925
 
  packet->pkt.user_id->prefs = NULL;
1926
 
  packet->pkt.user_id->namehash = NULL;
1927
 
}
1928
2087
 
1929
2088
static int
1930
 
parse_user_id( iobuf_t inp, int pkttype, unsigned long pktlen, PACKET *packet )
 
2089
parse_user_id( IOBUF inp, int pkttype, unsigned long pktlen, PACKET *packet )
1931
2090
{
1932
2091
    byte *p;
1933
2092
 
1938
2097
       allocatable, and a very large pktlen could actually cause our
1939
2098
       allocation to wrap around in xmalloc to a small number. */
1940
2099
 
1941
 
     if(pktlen>2048)
1942
 
       {
1943
 
         log_error("packet(%d) too large\n", pkttype);
1944
 
         skip_rest(inp, pktlen);
1945
 
         return GPG_ERR_INV_PACKET;
1946
 
       }
1947
 
 
1948
 
    packet->pkt.user_id = xmalloc (sizeof *packet->pkt.user_id  + pktlen);
 
2100
    if (pktlen > 2048)
 
2101
      {
 
2102
        log_error ("packet(%d) too large\n", pkttype);
 
2103
        iobuf_skip_rest(inp, pktlen, 0);
 
2104
        return G10ERR_INVALID_PACKET;
 
2105
      }
 
2106
    
 
2107
    packet->pkt.user_id = xmalloc_clear(sizeof *packet->pkt.user_id + pktlen);
1949
2108
    packet->pkt.user_id->len = pktlen;
1950
 
 
1951
 
    setup_user_id(packet);
 
2109
    packet->pkt.user_id->ref=1;
1952
2110
 
1953
2111
    p = packet->pkt.user_id->name;
1954
2112
    for( ; pktlen; pktlen--, p++ )
1957
2115
 
1958
2116
    if( list_mode ) {
1959
2117
        int n = packet->pkt.user_id->len;
1960
 
        printf(":user ID packet: \"");
 
2118
        fprintf (listfp, ":user ID packet: \"");
1961
2119
        /* fixme: Hey why don't we replace this with print_string?? */
1962
2120
        for(p=packet->pkt.user_id->name; n; p++, n-- ) {
1963
2121
            if( *p >= ' ' && *p <= 'z' )
1964
 
                putchar(*p);
 
2122
                putc (*p, listfp);
1965
2123
            else
1966
 
                printf("\\x%02x", *p );
 
2124
                fprintf (listfp, "\\x%02x", *p );
1967
2125
        }
1968
 
        printf("\"\n");
 
2126
        fprintf (listfp, "\"\n");
1969
2127
    }
1970
2128
    return 0;
1971
2129
}
2004
2162
}
2005
2163
 
2006
2164
static int
2007
 
parse_attribute( iobuf_t inp, int pkttype, unsigned long pktlen, PACKET *packet )
 
2165
parse_attribute( IOBUF inp, int pkttype, unsigned long pktlen, PACKET *packet )
2008
2166
{
2009
2167
    byte *p;
2010
2168
 
2011
2169
#define EXTRA_UID_NAME_SPACE 71
2012
 
    packet->pkt.user_id = xmalloc (sizeof *packet->pkt.user_id
2013
 
                                  + EXTRA_UID_NAME_SPACE);
2014
 
 
2015
 
    setup_user_id(packet);
2016
 
 
2017
 
    packet->pkt.user_id->attrib_data = xmalloc (pktlen);
 
2170
    packet->pkt.user_id = xmalloc_clear(sizeof *packet->pkt.user_id
 
2171
                                        + EXTRA_UID_NAME_SPACE);
 
2172
    packet->pkt.user_id->ref=1;
 
2173
    packet->pkt.user_id->attrib_data = xmalloc(pktlen);
2018
2174
    packet->pkt.user_id->attrib_len = pktlen;
 
2175
 
2019
2176
    p = packet->pkt.user_id->attrib_data;
2020
2177
    for( ; pktlen; pktlen--, p++ )
2021
2178
        *p = iobuf_get_noeof(inp);
2028
2185
    make_attribute_uidname(packet->pkt.user_id, EXTRA_UID_NAME_SPACE);
2029
2186
 
2030
2187
    if( list_mode ) {
2031
 
        printf(":attribute packet: %s\n", packet->pkt.user_id->name );
 
2188
        fprintf (listfp, ":attribute packet: %s\n", packet->pkt.user_id->name );
2032
2189
    }
2033
2190
    return 0;
2034
2191
}
2035
2192
 
2036
2193
 
2037
2194
static int
2038
 
parse_comment( iobuf_t inp, int pkttype, unsigned long pktlen, PACKET *packet )
 
2195
parse_comment( IOBUF inp, int pkttype, unsigned long pktlen, PACKET *packet )
2039
2196
{
2040
2197
    byte *p;
2041
2198
 
2042
 
    packet->pkt.comment = xmalloc (sizeof *packet->pkt.comment + pktlen - 1);
 
2199
    /* Cap comment packet at a reasonable value to avoid an integer
 
2200
       overflow in the malloc below.  Comment packets are actually not
 
2201
       anymore define my OpenPGP and we even stopped to use our
 
2202
       private comment packet. */
 
2203
    if (pktlen>65536)
 
2204
      {
 
2205
        log_error ("packet(%d) too large\n", pkttype);
 
2206
        iobuf_skip_rest (inp, pktlen, 0);
 
2207
        return G10ERR_INVALID_PACKET;
 
2208
      }
 
2209
    packet->pkt.comment = xmalloc(sizeof *packet->pkt.comment + pktlen - 1);
2043
2210
    packet->pkt.comment->len = pktlen;
2044
2211
    p = packet->pkt.comment->data;
2045
2212
    for( ; pktlen; pktlen--, p++ )
2047
2214
 
2048
2215
    if( list_mode ) {
2049
2216
        int n = packet->pkt.comment->len;
2050
 
        printf(":%scomment packet: \"", pkttype == PKT_OLD_COMMENT?
 
2217
        fprintf (listfp, ":%scomment packet: \"", pkttype == PKT_OLD_COMMENT?
2051
2218
                                         "OpenPGP draft " : "" );
2052
2219
        for(p=packet->pkt.comment->data; n; p++, n-- ) {
2053
2220
            if( *p >= ' ' && *p <= 'z' )
2054
 
                putchar(*p);
 
2221
                putc (*p, listfp);
2055
2222
            else
2056
 
                printf("\\x%02x", *p );
 
2223
                fprintf (listfp, "\\x%02x", *p );
2057
2224
        }
2058
 
        printf("\"\n");
 
2225
        fprintf (listfp, "\"\n");
2059
2226
    }
2060
2227
    return 0;
2061
2228
}
2062
2229
 
2063
2230
 
2064
2231
static void
2065
 
parse_trust( iobuf_t inp, int pkttype, unsigned long pktlen, PACKET *pkt )
 
2232
parse_trust( IOBUF inp, int pkttype, unsigned long pktlen, PACKET *pkt )
2066
2233
{
2067
2234
  int c;
2068
2235
 
2070
2237
    {
2071
2238
      c = iobuf_get_noeof(inp);
2072
2239
      pktlen--;
2073
 
      pkt->pkt.ring_trust = xmalloc ( sizeof *pkt->pkt.ring_trust );
 
2240
      pkt->pkt.ring_trust = xmalloc( sizeof *pkt->pkt.ring_trust );
2074
2241
      pkt->pkt.ring_trust->trustval = c;
2075
2242
      pkt->pkt.ring_trust->sigcache = 0;
2076
2243
      if (!c && pktlen==1)
2082
2249
            pkt->pkt.ring_trust->sigcache = c;
2083
2250
        }
2084
2251
      if( list_mode )
2085
 
        printf(":trust packet: flag=%02x sigcache=%02x\n",
 
2252
        fprintf (listfp, ":trust packet: flag=%02x sigcache=%02x\n",
2086
2253
               pkt->pkt.ring_trust->trustval,
2087
2254
               pkt->pkt.ring_trust->sigcache);
2088
2255
    }
2089
2256
  else
2090
2257
    {
2091
2258
      if( list_mode )
2092
 
        printf(":trust packet: empty\n");
 
2259
        fprintf (listfp, ":trust packet: empty\n");
2093
2260
    }
2094
 
  skip_rest (inp, pktlen);
 
2261
  iobuf_skip_rest (inp, pktlen, 0);
2095
2262
}
2096
2263
 
2097
2264
 
2098
2265
static int
2099
 
parse_plaintext( iobuf_t inp, int pkttype, unsigned long pktlen,
2100
 
                                        PACKET *pkt, int new_ctb )
 
2266
parse_plaintext( IOBUF inp, int pkttype, unsigned long pktlen,
 
2267
                 PACKET *pkt, int new_ctb, int partial )
2101
2268
{
2102
2269
    int rc = 0;
2103
 
    int mode, namelen, partial=0;
 
2270
    int mode, namelen;
2104
2271
    PKT_plaintext *pt;
2105
2272
    byte *p;
2106
2273
    int c, i;
2107
2274
 
2108
 
    if( pktlen && pktlen < 6 ) {
 
2275
    if( !partial && pktlen < 6 ) {
2109
2276
        log_error("packet(%d) too short (%lu)\n", pkttype, (ulong)pktlen);
2110
 
        rc = GPG_ERR_INV_PACKET;
 
2277
        rc = gpg_error (GPG_ERR_INV_PACKET);
2111
2278
        goto leave;
2112
2279
    }
2113
 
    /* A packet length of zero indicates partial body length.  A zero
2114
 
       data length isn't a zero length packet due to the header (mode,
2115
 
       name, etc), so this is accurate. */
2116
 
    if(pktlen==0)
2117
 
      partial=1;
2118
2280
    mode = iobuf_get_noeof(inp); if( pktlen ) pktlen--;
2119
2281
    namelen = iobuf_get_noeof(inp); if( pktlen ) pktlen--;
2120
 
    pt = pkt->pkt.plaintext = xmalloc (sizeof *pkt->pkt.plaintext + namelen -1);
 
2282
    /* Note that namelen will never exceed 255 bytes. */
 
2283
    pt = pkt->pkt.plaintext = xmalloc(sizeof *pkt->pkt.plaintext + namelen -1);
2121
2284
    pt->new_ctb = new_ctb;
2122
2285
    pt->mode = mode;
2123
2286
    pt->namelen = namelen;
2139
2302
    pktlen = 0;
2140
2303
 
2141
2304
    if( list_mode ) {
2142
 
        printf(":literal data packet:\n"
2143
 
               "\tmode %c, created %lu, name=\"",
2144
 
                    mode >= ' ' && mode <'z'? mode : '?',
 
2305
        fprintf (listfp, ":literal data packet:\n"
 
2306
               "\tmode %c (%X), created %lu, name=\"",
 
2307
                    mode >= ' ' && mode <'z'? mode : '?', mode,
2145
2308
                    (ulong)pt->timestamp );
2146
2309
        for(p=pt->name,i=0; i < namelen; p++, i++ ) {
2147
2310
            if( *p >= ' ' && *p <= 'z' )
2148
 
                putchar(*p);
 
2311
                putc (*p, listfp);
2149
2312
            else
2150
 
                printf("\\x%02x", *p );
 
2313
                fprintf (listfp, "\\x%02x", *p );
2151
2314
        }
2152
 
        printf("\",\n\traw data: %lu bytes\n", (ulong)pt->len );
 
2315
        fprintf (listfp, "\",\n\traw data: ");
 
2316
        if(partial)
 
2317
          fprintf (listfp, "unknown length\n");
 
2318
        else
 
2319
          fprintf (listfp, "%lu bytes\n", (ulong)pt->len );
2153
2320
    }
2154
2321
 
2155
2322
  leave:
2158
2325
 
2159
2326
 
2160
2327
static int
2161
 
parse_compressed( iobuf_t inp, int pkttype, unsigned long pktlen,
 
2328
parse_compressed( IOBUF inp, int pkttype, unsigned long pktlen,
2162
2329
                  PACKET *pkt, int new_ctb )
2163
2330
{
2164
2331
    PKT_compressed *zd;
2167
2334
     * (this should be the last object in a file or
2168
2335
     *  the compress algorithm should know the length)
2169
2336
     */
2170
 
    zd = pkt->pkt.compressed =  xmalloc (sizeof *pkt->pkt.compressed );
 
2337
    zd = pkt->pkt.compressed =  xmalloc(sizeof *pkt->pkt.compressed );
2171
2338
    zd->algorithm = iobuf_get_noeof(inp);
2172
2339
    zd->len = 0; /* not used */ 
2173
2340
    zd->new_ctb = new_ctb;
2174
2341
    zd->buf = inp;
2175
2342
    if( list_mode )
2176
 
        printf(":compressed packet: algo=%d\n", zd->algorithm);
 
2343
        fprintf (listfp, ":compressed packet: algo=%d\n", zd->algorithm);
2177
2344
    return 0;
2178
2345
}
2179
2346
 
2180
2347
 
2181
2348
static int
2182
 
parse_encrypted( iobuf_t inp, int pkttype, unsigned long pktlen,
2183
 
                                       PACKET *pkt, int new_ctb )
 
2349
parse_encrypted( IOBUF inp, int pkttype, unsigned long pktlen,
 
2350
                 PACKET *pkt, int new_ctb, int partial )
2184
2351
{
2185
2352
    int rc = 0;
2186
2353
    PKT_encrypted *ed;
2187
2354
    unsigned long orig_pktlen = pktlen;
2188
2355
 
2189
 
    ed = pkt->pkt.encrypted =  xmalloc (sizeof *pkt->pkt.encrypted );
 
2356
    ed = pkt->pkt.encrypted =  xmalloc(sizeof *pkt->pkt.encrypted );
2190
2357
    ed->len = pktlen;
2191
2358
    /* we don't know the extralen which is (cipher_blocksize+2)
2192
2359
       because the algorithm ist not specified in this packet.
2196
2363
    ed->extralen = 0;
2197
2364
    ed->buf = NULL;
2198
2365
    ed->new_ctb = new_ctb;
 
2366
    ed->is_partial = partial;
2199
2367
    ed->mdc_method = 0;
2200
2368
    if( pkttype == PKT_ENCRYPTED_MDC ) {
2201
2369
        /* fixme: add some pktlen sanity checks */
2208
2376
            log_error("encrypted_mdc packet with unknown version %d\n",
2209
2377
                                                                version);
2210
2378
            /*skip_rest(inp, pktlen); should we really do this? */
2211
 
            rc = GPG_ERR_INV_PACKET;
 
2379
            rc = gpg_error (GPG_ERR_INV_PACKET);
2212
2380
            goto leave;
2213
2381
        }
2214
2382
        ed->mdc_method = DIGEST_ALGO_SHA1;
2215
2383
    }
2216
2384
    if( orig_pktlen && pktlen < 10 ) { /* actually this is blocksize+2 */
2217
2385
        log_error("packet(%d) too short\n", pkttype);
2218
 
        rc = GPG_ERR_INV_PACKET;
2219
 
        skip_rest(inp, pktlen);
 
2386
        rc = G10ERR_INVALID_PACKET;
 
2387
        iobuf_skip_rest(inp, pktlen, partial);
2220
2388
        goto leave;
2221
2389
    }
2222
2390
    if( list_mode ) {
2223
2391
        if( orig_pktlen )
2224
 
            printf(":encrypted data packet:\n\tlength: %lu\n", orig_pktlen);
 
2392
            fprintf (listfp, ":encrypted data packet:\n\tlength: %lu\n",
 
2393
                     orig_pktlen);
2225
2394
        else
2226
 
            printf(":encrypted data packet:\n\tlength: unknown\n");
 
2395
            fprintf (listfp, ":encrypted data packet:\n\tlength: unknown\n");
2227
2396
        if( ed->mdc_method )
2228
 
            printf("\tmdc_method: %d\n", ed->mdc_method );
 
2397
            fprintf (listfp, "\tmdc_method: %d\n", ed->mdc_method );
2229
2398
    }
2230
2399
 
2231
2400
    ed->buf = inp;
2232
 
    pktlen = 0;
2233
2401
 
2234
2402
  leave:
2235
2403
    return rc;
2236
2404
}
2237
2405
 
2238
2406
 
 
2407
/* Note, that this code is not anymore used in real life because now
 
2408
   the MDC checking is done right after the encryption in
 
2409
   decrypt_data. */
2239
2410
static int
2240
 
parse_mdc( iobuf_t inp, int pkttype, unsigned long pktlen,
 
2411
parse_mdc( IOBUF inp, int pkttype, unsigned long pktlen,
2241
2412
                                   PACKET *pkt, int new_ctb )
2242
2413
{
2243
2414
    int rc = 0;
2244
2415
    PKT_mdc *mdc;
2245
2416
    byte *p;
2246
2417
 
2247
 
    mdc = pkt->pkt.mdc=  xmalloc (sizeof *pkt->pkt.mdc );
 
2418
    mdc = pkt->pkt.mdc = xmalloc(sizeof *pkt->pkt.mdc );
2248
2419
    if( list_mode )
2249
 
        printf(":mdc packet: length=%lu\n", pktlen);
 
2420
        fprintf (listfp, ":mdc packet: length=%lu\n", pktlen);
2250
2421
    if( !new_ctb || pktlen != 20 ) {
2251
2422
        log_error("mdc_packet with invalid encoding\n");
2252
 
        rc = GPG_ERR_INV_PACKET;
 
2423
        rc = gpg_error (GPG_ERR_INV_PACKET);
2253
2424
        goto leave;
2254
2425
    }
2255
2426
    p = mdc->hash;
2273
2444
 */
2274
2445
 
2275
2446
static int
2276
 
parse_gpg_control( iobuf_t inp,
2277
 
                   int pkttype, unsigned long pktlen, PACKET *packet )
 
2447
parse_gpg_control( IOBUF inp, int pkttype,
 
2448
                   unsigned long pktlen, PACKET *packet, int partial )
2278
2449
{
2279
2450
    byte *p;
2280
2451
    const byte *sesmark;
2282
2453
    int i;
2283
2454
 
2284
2455
    if ( list_mode )
2285
 
        printf(":packet 63: length %lu ",  pktlen);
 
2456
        fprintf (listfp, ":packet 63: length %lu ",  pktlen);
2286
2457
 
2287
2458
    sesmark = get_session_marker ( &sesmarklen );
2288
2459
    if ( pktlen < sesmarklen+1 ) /* 1 is for the control bytes */
2291
2462
        if ( sesmark[i] != iobuf_get_noeof(inp) )
2292
2463
            goto skipit;
2293
2464
    }
 
2465
    if (pktlen > 4096)
 
2466
      goto skipit; /* Definitely too large.  We skip it to avoid an
 
2467
                      overflow in the malloc. */
2294
2468
    if ( list_mode )
2295
2469
        puts ("- gpg control packet");
2296
2470
 
2297
 
    packet->pkt.gpg_control = xmalloc (sizeof *packet->pkt.gpg_control
 
2471
    packet->pkt.gpg_control = xmalloc(sizeof *packet->pkt.gpg_control
2298
2472
                                      + pktlen - 1);
2299
2473
    packet->pkt.gpg_control->control = iobuf_get_noeof(inp); pktlen--;
2300
2474
    packet->pkt.gpg_control->datalen = pktlen;
2309
2483
        int c;
2310
2484
 
2311
2485
        i=0;
2312
 
        printf("- private (rest length %lu)\n",  pktlen);
2313
 
        if( iobuf_in_block_mode(inp) ) {
 
2486
        fprintf (listfp, "- private (rest length %lu)\n",  pktlen);
 
2487
        if( partial ) {
2314
2488
            while( (c=iobuf_get(inp)) != -1 )
2315
2489
                dump_hex_line(c, &i);
2316
2490
        }
2318
2492
            for( ; pktlen; pktlen-- )
2319
2493
                dump_hex_line(iobuf_get(inp), &i);
2320
2494
        }
2321
 
        putchar('\n');
 
2495
        putc ('\n', listfp);
2322
2496
    }
2323
 
    skip_rest(inp,pktlen);
2324
 
    return GPG_ERR_INV_PACKET;
 
2497
    iobuf_skip_rest(inp,pktlen, 0);
 
2498
    return gpg_error (GPG_ERR_INV_PACKET);
2325
2499
}
2326
2500
 
2327
2501
/* create a gpg control packet to be used internally as a placeholder */
2331
2505
    PACKET *packet;
2332
2506
    byte *p;
2333
2507
 
2334
 
    packet = xmalloc ( sizeof *packet );
 
2508
    packet = xmalloc( sizeof *packet );
2335
2509
    init_packet(packet);
2336
2510
    packet->pkttype = PKT_GPG_CONTROL;
2337
 
    packet->pkt.gpg_control = xmalloc (sizeof *packet->pkt.gpg_control
 
2511
    packet->pkt.gpg_control = xmalloc(sizeof *packet->pkt.gpg_control
2338
2512
                                      + datalen - 1);
2339
2513
    packet->pkt.gpg_control->control = type;
2340
2514
    packet->pkt.gpg_control->datalen = datalen;