/[winpt]/trunk/Gnupg/parse-packet.c
ViewVC logotype

Annotation of /trunk/Gnupg/parse-packet.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 313 - (hide annotations)
Sun May 13 09:43:51 2007 UTC (17 years, 9 months ago) by twoaday
File MIME type: text/plain
File size: 68423 byte(s)


1 werner 46 /* parse-packet.c - read packets
2     * Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
3     *
4     * This file is part of GnuPG.
5     *
6     * GnuPG is free software; you can redistribute it and/or modify
7     * it under the terms of the GNU General Public License as published by
8     * the Free Software Foundation; either version 2 of the License, or
9     * (at your option) any later version.
10     *
11     * GnuPG is distributed in the hope that it will be useful,
12     * but WITHOUT ANY WARRANTY; without even the implied warranty of
13     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14     * GNU General Public License for more details.
15     *
16     * You should have received a copy of the GNU General Public License
17     * along with this program; if not, write to the Free Software
18     * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19     */
20    
21     #ifdef HAVE_CONFIG_H
22     #include <config.h>
23     #endif
24     #include <stdio.h>
25     #include <stdlib.h>
26     #include <string.h>
27     #include <time.h>
28     #include <assert.h>
29     #include <process.h>
30     #include <sys/types.h>
31    
32     #include "openpgp.h"
33     #include "packet.h"
34     #include "md.h"
35    
36     static int mpi_print_mode = 0;
37     static int list_mode = 0;
38    
39     static int parse( gpg_iobuf_t inp, PACKET *pkt, int onlykeypkts,
40     _off_t *retpos, int *skip, gpg_iobuf_t out, int do_skip );
41     static int copy_packet( gpg_iobuf_t inp, gpg_iobuf_t out, int pkttype,
42     unsigned long pktlen );
43     static void skip_packet( gpg_iobuf_t inp, int pkttype, unsigned long pktlen );
44     static void skip_rest( gpg_iobuf_t inp, unsigned long pktlen );
45     static void *read_rest( gpg_iobuf_t inp, size_t pktlen );
46     static int parse_symkeyenc( gpg_iobuf_t inp, int pkttype, unsigned long pktlen,
47     PACKET *packet );
48     static int parse_pubkeyenc( gpg_iobuf_t inp, int pkttype, unsigned long pktlen,
49     PACKET *packet );
50     static int parse_signature( gpg_iobuf_t inp, int pkttype, unsigned long pktlen,
51     PKT_signature *sig );
52     static int parse_onepass_sig( gpg_iobuf_t inp, int pkttype, unsigned long pktlen,
53     PKT_onepass_sig *ops );
54     static int parse_key( gpg_iobuf_t inp, int pkttype, unsigned long pktlen,
55     byte *hdr, int hdrlen, PACKET *packet );
56     static int parse_user_id( gpg_iobuf_t inp, int pkttype, unsigned long pktlen,
57     PACKET *packet );
58     static int parse_attribute( gpg_iobuf_t inp, int pkttype, unsigned long pktlen,
59     PACKET *packet );
60     static int parse_comment( gpg_iobuf_t inp, int pkttype, unsigned long pktlen,
61     PACKET *packet );
62     static void parse_trust( gpg_iobuf_t inp, int pkttype, unsigned long pktlen,
63     PACKET *packet );
64     static int parse_plaintext( gpg_iobuf_t inp, int pkttype, unsigned long pktlen,
65     PACKET *packet, int new_ctb);
66     static int parse_compressed( gpg_iobuf_t inp, int pkttype, unsigned long pktlen,
67     PACKET *packet, int new_ctb );
68     static int parse_encrypted( gpg_iobuf_t inp, int pkttype, unsigned long pktlen,
69     PACKET *packet, int new_ctb);
70     static int parse_mdc( gpg_iobuf_t inp, int pkttype, unsigned long pktlen,
71     PACKET *packet, int new_ctb);
72     static int parse_gpg_control( gpg_iobuf_t inp, int pkttype, unsigned long pktlen,
73     PACKET *packet );
74    
75     static unsigned short
76     read_16(gpg_iobuf_t inp)
77     {
78     unsigned short a;
79     a = gpg_iobuf_get_noeof(inp) << 8;
80     a |= gpg_iobuf_get_noeof(inp);
81     return a;
82     }
83    
84     static unsigned long
85     read_32(gpg_iobuf_t inp)
86     {
87     unsigned long a;
88     a = gpg_iobuf_get_noeof(inp) << 24;
89     a |= gpg_iobuf_get_noeof(inp) << 16;
90     a |= gpg_iobuf_get_noeof(inp) << 8;
91     a |= gpg_iobuf_get_noeof(inp);
92     return a;
93     }
94    
95     static unsigned long
96     buffer_to_u32( const unsigned char *buffer )
97     {
98     unsigned long a;
99     a = *buffer << 24;
100     a |= buffer[1] << 16;
101     a |= buffer[2] << 8;
102     a |= buffer[3];
103     return a;
104     }
105    
106 twoaday 313 int
107     set_packet_list_mode( int mode )
108     {
109     int old = list_mode;
110     list_mode = mode;
111     mpi_print_mode = 0;
112     return old;
113     }
114 werner 46
115 twoaday 2 static void
116 werner 46 unknown_pubkey_warning( int algo )
117     {
118     static byte unknown_pubkey_algos[256];
119    
120     algo &= 0xff;
121     if( !unknown_pubkey_algos[algo] ) {
122     unknown_pubkey_algos[algo] = 1;
123     }
124     }
125    
126    
127     static gpg_mpi_t
128     mpi_read( gpg_iobuf_t inp, size_t *ret_n, int flags )
129     {
130     gpg_mpi_t a;
131     int i = 0, j = 0;
132     u32 t;
133     size_t n;
134    
135     a = calloc( 1, sizeof *a );
136     a->nbits = read_16( inp );
137     n = (a->nbits + 7) / 8;
138     a->alloced = n;
139     a->d = calloc( n, 4 );
140     i = 4 - n % 4;
141     i %= 4;
142     j = (n+4-1) / 4;
143     for( ; j > 0; j-- ) {
144     t = 0;
145     for( ; i < 4; i++ ) {
146     t <<= 8;
147     t |= gpg_iobuf_get( inp ) & 0xff;
148     }
149     i = 0;
150     a->d[j-1] = t;
151     }
152     if( *ret_n )
153     *ret_n = n + 2;
154    
155     return a;
156     }
157    
158     void
159     mpi_print( FILE *fp, gpg_mpi_t a, int mode )
160     {
161     int i;
162     for( i = 0; i < a->alloced / 4; i++ )
163     printf( "%08lx", a->d[i] );
164     }
165    
166     static void
167     mpi_set_protect_flag( gpg_mpi_t a )
168     {
169     a->flags = 1;
170     }
171    
172     static gpg_mpi_t
173     mpi_set_opaque( gpg_mpi_t a, void *p, int len )
174     {
175     if( !a ) {
176     a = calloc( 1, sizeof *a );
177     }
178    
179     if( a->flags & 4 )
180     safe_free( a->d );
181     else {
182     safe_free( a->d );
183     }
184    
185     a->d = p;
186     a->alloced = 0;
187     a->nlimbs = 0;
188     a->nbits = len;
189     a->flags = 4;
190     return a;
191     }
192    
193     void
194     free_symkey_enc( PKT_symkey_enc *enc )
195     {
196     safe_free(enc);
197     }
198    
199     void
200     free_pubkey_enc( PKT_pubkey_enc *enc )
201     {
202     int n, i;
203     n = pubkey_get_nenc( enc->pubkey_algo );
204     if( !n )
205     safe_free(enc->data[0]);
206     for(i=0; i < n; i++ )
207     safe_free( enc->data[i] );
208     safe_free(enc);
209     }
210    
211     void
212     free_seckey_enc( PKT_signature *sig )
213     {
214     int n, i;
215    
216     n = pubkey_get_nsig( sig->pubkey_algo );
217     if( !n )
218     safe_free(sig->data[0]);
219     for(i=0; i < n; i++ )
220     safe_free( sig->data[i] );
221    
222     safe_free(sig->revkey);
223     safe_free(sig->hashed);
224     safe_free(sig->unhashed);
225     safe_free(sig);
226     }
227    
228    
229    
230     void
231     release_public_key_parts( PKT_public_key *pk )
232     {
233     int n, i;
234     n = pubkey_get_npkey( pk->pubkey_algo );
235     if( !n )
236     safe_free(pk->pkey[0]);
237     for(i=0; i < n; i++ ) {
238     safe_free( pk->pkey[i] );
239     pk->pkey[i] = NULL;
240     }
241     if (pk->prefs) {
242     safe_free (pk->prefs);
243     pk->prefs = NULL;
244     }
245     if( pk->namehash ) {
246     safe_free(pk->namehash);
247     pk->namehash = NULL;
248     }
249     if (pk->user_id) {
250     free_user_id (pk->user_id);
251     pk->user_id = NULL;
252     }
253     if (pk->revkey) {
254     safe_free(pk->revkey);
255     pk->revkey=NULL;
256     pk->numrevkeys=0;
257     }
258     }
259    
260    
261     void
262     free_public_key( PKT_public_key *pk )
263     {
264     release_public_key_parts( pk );
265     safe_free(pk);
266     }
267    
268    
269     int
270     pubkey_get_nenc( int algo )
271     {
272     switch( algo ) {
273     case 1:
274     case 2:
275     case 3: return 1;
276     case 16:
277     case 20: return 2;
278     }
279     return 0;
280     }
281    
282     int
283     pubkey_get_nsig( algo )
284     {
285     switch( algo ) {
286     case 1:
287     case 2:
288     case 3: return 1;
289     case 17:
290     case 16:
291     case 20: return 2;
292     }
293     return 0;
294     }
295    
296     int
297     pubkey_get_npkey( int algo )
298     {
299     if( is_ELGAMAL( algo ) ) return 3;
300     else if ( algo == 17 ) return 4;
301     else if ( is_RSA( algo ) ) return 2;
302     return 0;
303 twoaday 309 }
304 werner 46
305     int
306     pubkey_get_nskey( int algo )
307     {
308     if ( is_ELGAMAL( algo ) ) return 4;
309     else if ( algo == 17 ) return 5;
310     else if ( is_RSA( algo ) )return 6;
311     return 0;
312 twoaday 133 }
313 werner 46
314    
315    
316     /*
317     * Return a copy of the preferences
318     */
319     prefitem_t *
320     copy_prefs (const prefitem_t *prefs)
321     {
322     size_t n;
323     prefitem_t *new;
324    
325     if (!prefs)
326     return NULL;
327    
328     for (n=0; prefs[n].type; n++)
329     ;
330     new = malloc ( sizeof (*new) * (n+1));
331     for (n=0; prefs[n].type; n++) {
332     new[n].type = prefs[n].type;
333     new[n].value = prefs[n].value;
334     }
335     new[n].type = PREFTYPE_NONE;
336     new[n].value = 0;
337    
338     return new;
339     }
340    
341     /****************
342     * Replace all common parts of a sk by the one from the public key.
343     * This is a hack and a better solution will be to just store the real secret
344     * parts somewhere and don't duplicate all the other stuff.
345     */
346     void
347     copy_public_parts_to_secret_key( PKT_public_key *pk, PKT_secret_key *sk )
348     {
349     sk->expiredate = pk->expiredate;
350     sk->pubkey_algo = pk->pubkey_algo;
351     sk->pubkey_usage= pk->pubkey_usage;
352     sk->req_usage = pk->req_usage;
353     sk->req_algo = pk->req_algo;
354     sk->has_expired = pk->has_expired;
355     sk->is_revoked = pk->is_revoked;
356     sk->is_valid = pk->is_valid;
357     sk->main_keyid[0]= pk->main_keyid[0];
358     sk->main_keyid[1]= pk->main_keyid[1];
359     sk->keyid[0] = pk->keyid[0];
360     sk->keyid[1] = pk->keyid[1];
361     }
362    
363     void
364     release_secret_key_parts( PKT_secret_key *sk )
365     {
366     int n, i;
367    
368     n = pubkey_get_nskey( sk->pubkey_algo );
369     if( !n )
370     safe_free(sk->skey[0]);
371     for(i=0; i < n; i++ ) {
372     safe_free( sk->skey[i] );
373     sk->skey[i] = NULL;
374     }
375     }
376    
377     void
378     free_secret_key( PKT_secret_key *sk )
379     {
380     release_secret_key_parts( sk );
381     safe_free(sk);
382     }
383    
384     void
385     free_comment( PKT_comment *rem )
386     {
387     safe_free(rem);
388     }
389    
390     void
391     free_attributes(PKT_user_id *uid)
392     {
393     safe_free(uid->attribs);
394     safe_free(uid->attrib_data);
395    
396     uid->attribs=NULL;
397     uid->attrib_data=NULL;
398     uid->attrib_len=0;
399     }
400    
401     void
402     free_user_id (PKT_user_id *uid)
403     {
404     assert (uid->ref > 0);
405     if (--uid->ref)
406     return;
407    
408     free_attributes(uid);
409    
410     if (uid->prefs)
411     safe_free (uid->prefs);
412     safe_free (uid);
413     }
414    
415     void
416     free_compressed( PKT_compressed *zd )
417     {
418     if( zd->buf ) { /* have to skip some bytes */
419     /* don't have any information about the length, so
420     * we assume this is the last packet */
421     while( gpg_iobuf_read( zd->buf, NULL, 1<<30 ) != -1 )
422     ;
423     }
424     safe_free(zd);
425     }
426    
427     void
428     free_encrypted( PKT_encrypted *ed )
429     {
430     if( ed->buf ) { /* have to skip some bytes */
431     if( gpg_iobuf_in_block_mode(ed->buf) ) {
432     while( gpg_iobuf_read( ed->buf, NULL, 1<<30 ) != -1 )
433     ;
434     }
435     else {
436     while( ed->len ) { /* skip the packet */
437     int n = gpg_iobuf_read( ed->buf, NULL, ed->len );
438     if( n == -1 )
439     ed->len = 0;
440     else
441     ed->len -= n;
442     }
443     }
444     }
445     safe_free(ed);
446     }
447    
448    
449     void
450     free_plaintext( PKT_plaintext *pt )
451     {
452     if( pt->buf ) { /* have to skip some bytes */
453     if( gpg_iobuf_in_block_mode(pt->buf) ) {
454     while( gpg_iobuf_read( pt->buf, NULL, 1<<30 ) != -1 )
455     ;
456     }
457     else {
458     while( pt->len ) { /* skip the packet */
459     int n = gpg_iobuf_read( pt->buf, NULL, pt->len );
460     if( n == -1 )
461     pt->len = 0;
462     else
463     pt->len -= n;
464     }
465     }
466     }
467     safe_free(pt);
468     }
469    
470     /****************
471     * Free the packet in pkt.
472     */
473     void
474     gpg_free_packet( PACKET *pkt )
475     {
476     if( !pkt || !pkt->pkt.generic )
477     return;
478    
479     switch( pkt->pkttype ) {
480     case PKT_SIGNATURE:
481     free_seckey_enc( pkt->pkt.signature );
482     break;
483     case PKT_PUBKEY_ENC:
484     free_pubkey_enc( pkt->pkt.pubkey_enc );
485     break;
486     case PKT_SYMKEY_ENC:
487     free_symkey_enc( pkt->pkt.symkey_enc );
488     break;
489     case PKT_PUBLIC_KEY:
490     case PKT_PUBLIC_SUBKEY:
491     free_public_key( pkt->pkt.public_key );
492     break;
493     case PKT_SECRET_KEY:
494     case PKT_SECRET_SUBKEY:
495     free_secret_key( pkt->pkt.secret_key );
496     break;
497     case PKT_COMMENT:
498     free_comment( pkt->pkt.comment );
499     break;
500     case PKT_USER_ID:
501     free_user_id( pkt->pkt.user_id );
502     break;
503     case PKT_COMPRESSED:
504     free_compressed( pkt->pkt.compressed);
505     break;
506     case PKT_ENCRYPTED:
507     case PKT_ENCRYPTED_MDC:
508     free_encrypted( pkt->pkt.encrypted );
509     break;
510     case PKT_PLAINTEXT:
511     free_plaintext( pkt->pkt.plaintext );
512     break;
513     default:
514     safe_free( pkt->pkt.generic );
515     break;
516     }
517     pkt->pkt.generic = NULL;
518     }
519    
520    
521    
522     /****************
523     * Parse a Packet and return it in packet
524     * Returns: 0 := valid packet in pkt
525     * -1 := no more packets
526     * >0 := error
527     * Note: The function may return an error and a partly valid packet;
528     * caller must free this packet.
529     */
530     int
531     gpg_parse_packet( gpg_iobuf_t inp, PACKET *pkt )
532     {
533     int skip, rc;
534    
535     do {
536     rc = parse( inp, pkt, 0, NULL, &skip, NULL, 0 );
537     } while( skip );
538     return rc;
539     }
540    
541     /****************
542     * Like parse packet, but only return secret or public (sub)key packets.
543     */
544     int
545     search_packet( gpg_iobuf_t inp, PACKET *pkt, _off_t *retpos, int with_uid )
546     {
547     int skip, rc;
548    
549     do {
550     rc = parse( inp, pkt, with_uid?2:1, retpos, &skip, NULL, 0 );
551     } while( skip );
552     return rc;
553     }
554    
555     /****************
556     * Copy all packets from INP to OUT, thereby removing unused spaces.
557     */
558     int
559     copy_all_packets( gpg_iobuf_t inp, gpg_iobuf_t out )
560     {
561     PACKET pkt;
562     int skip, rc=0;
563     do {
564     gpg_init_packet(&pkt);
565     } while( !(rc = parse( inp, &pkt, 0, NULL, &skip, out, 0 )));
566     return rc;
567     }
568    
569     /****************
570     * Copy some packets from INP to OUT, thereby removing unused spaces.
571     * Stop at offset STOPoff (i.e. don't copy packets at this or later offsets)
572     */
573     int
574     copy_some_packets( gpg_iobuf_t inp, gpg_iobuf_t out, _off_t stopoff )
575     {
576     PACKET pkt;
577     int skip, rc=0;
578     do {
579     if( gpg_iobuf_tell(inp) >= stopoff )
580     return 0;
581     gpg_init_packet(&pkt);
582     } while( !(rc = parse( inp, &pkt, 0, NULL, &skip, out, 0 )) );
583     return rc;
584     }
585    
586     /****************
587     * Skip over N packets
588     */
589     int
590     skip_some_packets( gpg_iobuf_t inp, unsigned n )
591     {
592     int skip, rc=0;
593     PACKET pkt;
594    
595     for( ;n && !rc; n--) {
596     gpg_init_packet(&pkt);
597     rc = parse( inp, &pkt, 0, NULL, &skip, NULL, 1 );
598     }
599     return rc;
600     }
601    
602     /****************
603     * Parse packet. Set the variable skip points to 1 if the packet
604     * should be skipped; this is the case if either ONLYKEYPKTS is set
605     * and the parsed packet isn't one or the
606     * packet-type is 0, indicating deleted stuff.
607     * if OUT is not NULL, a special copymode is used.
608     */
609     static int
610     parse( gpg_iobuf_t inp, PACKET *pkt, int onlykeypkts, _off_t *retpos,
611     int *skip, gpg_iobuf_t out, int do_skip
612     )
613     {
614     int rc=0, c, ctb, pkttype, lenbytes;
615     unsigned long pktlen;
616     byte hdr[8];
617     int hdrlen;
618     int new_ctb = 0;
619     int with_uid = (onlykeypkts == 2);
620    
621     *skip = 0;
622     /*assert( !pkt->pkt.generic );*/
623     if( retpos )
624     *retpos = gpg_iobuf_tell(inp);
625    
626     if( (ctb = gpg_iobuf_get(inp)) == -1 ) {
627     rc = -1;
628     goto leave;
629     }
630     hdrlen=0;
631     hdr[hdrlen++] = ctb;
632     if( !(ctb & 0x80) ) {
633     printf("%s: invalid packet (ctb=%02x)\n", gpg_iobuf_where(inp), ctb );
634     rc = G10ERR_INVALID_PACKET;
635     goto leave;
636     }
637     pktlen = 0;
638     new_ctb = !!(ctb & 0x40);
639     if( new_ctb ) {
640     pkttype = ctb & 0x3f;
641     if( (c = gpg_iobuf_get(inp)) == -1 ) {
642     printf("%s: 1st length byte missing\n", gpg_iobuf_where(inp) );
643     rc = G10ERR_INVALID_PACKET;
644     goto leave;
645     }
646     if (pkttype == PKT_COMPRESSED) {
647     gpg_iobuf_set_partial_block_mode(inp, c & 0xff);
648     pktlen = 0;/* to indicate partial length */
649     }
650     else {
651     hdr[hdrlen++] = c;
652     if( c < 192 )
653     pktlen = c;
654     else if( c < 224 ) {
655     pktlen = (c - 192) * 256;
656     if( (c = gpg_iobuf_get(inp)) == -1 ) {
657     printf("%s: 2nd length byte missing\n",
658     gpg_iobuf_where(inp) );
659     rc = G10ERR_INVALID_PACKET;
660     goto leave;
661     }
662     hdr[hdrlen++] = c;
663     pktlen += c + 192;
664     }
665     else if( c == 255 ) {
666     pktlen = (hdr[hdrlen++] = gpg_iobuf_get_noeof(inp)) << 24;
667     pktlen |= (hdr[hdrlen++] = gpg_iobuf_get_noeof(inp)) << 16;
668     pktlen |= (hdr[hdrlen++] = gpg_iobuf_get_noeof(inp)) << 8;
669     if( (c = gpg_iobuf_get(inp)) == -1 ) {
670     printf("%s: 4 byte length invalid\n",
671     gpg_iobuf_where(inp) );
672     rc = G10ERR_INVALID_PACKET;
673     goto leave;
674     }
675     pktlen |= (hdr[hdrlen++] = c );
676     }
677     else { /* partial body length */
678     gpg_iobuf_set_partial_block_mode(inp, c & 0xff);
679     pktlen = 0;/* to indicate partial length */
680     }
681     }
682     }
683     else {
684     pkttype = (ctb>>2)&0xf;
685     lenbytes = ((ctb&3)==3)? 0 : (1<<(ctb & 3));
686     if( !lenbytes ) {
687     pktlen = 0; /* don't know the value */
688     if( pkttype != PKT_COMPRESSED )
689     gpg_iobuf_set_block_mode(inp, 1);
690     }
691     else {
692     for( ; lenbytes; lenbytes-- ) {
693     pktlen <<= 8;
694     pktlen |= hdr[hdrlen++] = gpg_iobuf_get_noeof(inp);
695     }
696     }
697     }
698    
699     if (pktlen == 0xffffffff) {
700     /* with a some probability this is caused by a problem in the
701     * the uncompressing layer - in some error cases it just loops
702     * and spits out 0xff bytes. */
703 twoaday 313 printf ("%s: garbled packet detected\n", gpg_iobuf_where(inp) );
704 twoaday 214 rc = G10ERR_INVALID_PACKET;
705     goto leave;
706 werner 46 }
707    
708     if( out && pkttype ) {
709     if( gpg_iobuf_write( out, hdr, hdrlen ) == -1 )
710     rc = G10ERR_WRITE_FILE;
711     else
712     rc = copy_packet(inp, out, pkttype, pktlen );
713     goto leave;
714     }
715    
716     if (with_uid && pkttype == PKT_USER_ID)
717     ;
718     else if( do_skip
719     || !pkttype
720     || (onlykeypkts && pkttype != PKT_PUBLIC_SUBKEY
721     && pkttype != PKT_PUBLIC_KEY
722     && pkttype != PKT_SECRET_SUBKEY
723     && pkttype != PKT_SECRET_KEY ) ) {
724     skip_rest(inp, pktlen);
725     *skip = 1;
726     rc = 0;
727     goto leave;
728     }
729    
730     pkt->pkttype = pkttype;
731     rc = G10ERR_UNKNOWN_PACKET; /* default error */
732     switch( pkttype ) {
733     case PKT_PUBLIC_KEY:
734     case PKT_PUBLIC_SUBKEY:
735     pkt->pkt.public_key = calloc(1, sizeof *pkt->pkt.public_key );
736     rc = parse_key(inp, pkttype, pktlen, hdr, hdrlen, pkt );
737     break;
738     case PKT_SECRET_KEY:
739     case PKT_SECRET_SUBKEY:
740     pkt->pkt.secret_key = calloc(1,sizeof *pkt->pkt.secret_key );
741     rc = parse_key(inp, pkttype, pktlen, hdr, hdrlen, pkt );
742     break;
743     case PKT_SYMKEY_ENC:
744     rc = parse_symkeyenc( inp, pkttype, pktlen, pkt );
745     break;
746     case PKT_PUBKEY_ENC:
747     rc = parse_pubkeyenc(inp, pkttype, pktlen, pkt );
748     break;
749     case PKT_SIGNATURE:
750     pkt->pkt.signature = calloc(1,sizeof *pkt->pkt.signature );
751     rc = parse_signature(inp, pkttype, pktlen, pkt->pkt.signature );
752     break;
753     case PKT_ONEPASS_SIG:
754     pkt->pkt.onepass_sig = calloc(1,sizeof *pkt->pkt.onepass_sig );
755     rc = parse_onepass_sig(inp, pkttype, pktlen, pkt->pkt.onepass_sig );
756     break;
757     case PKT_USER_ID:
758     rc = parse_user_id(inp, pkttype, pktlen, pkt );
759     break;
760     case PKT_ATTRIBUTE:
761     pkt->pkttype = pkttype = PKT_USER_ID; /* we store it in the userID */
762     rc = parse_attribute(inp, pkttype, pktlen, pkt);
763     break;
764     case PKT_OLD_COMMENT:
765     case PKT_COMMENT:
766     rc = parse_comment(inp, pkttype, pktlen, pkt);
767     break;
768     case PKT_RING_TRUST:
769     parse_trust(inp, pkttype, pktlen, pkt);
770     rc = 0;
771     break;
772     case PKT_PLAINTEXT:
773     rc = parse_plaintext(inp, pkttype, pktlen, pkt, new_ctb );
774     break;
775     case PKT_COMPRESSED:
776     rc = parse_compressed(inp, pkttype, pktlen, pkt, new_ctb );
777     break;
778     case PKT_ENCRYPTED:
779     case PKT_ENCRYPTED_MDC:
780     rc = parse_encrypted(inp, pkttype, pktlen, pkt, new_ctb );
781     break;
782     case PKT_MDC:
783     rc = parse_mdc(inp, pkttype, pktlen, pkt, new_ctb );
784     break;
785     case PKT_GPG_CONTROL:
786     rc = parse_gpg_control(inp, pkttype, pktlen, pkt );
787     break;
788     default:
789     skip_packet(inp, pkttype, pktlen);
790     break;
791     }
792    
793     leave:
794     if( !rc && gpg_iobuf_error(inp) )
795     rc = G10ERR_INV_KEYRING;
796     return rc;
797     }
798    
799     static void
800     dump_hex_line( int c, int *i )
801     {
802     if( *i && !(*i%8) ) {
803     if( *i && !(*i%24) )
804     printf("\n%4d:", *i );
805     else
806     putchar(' ');
807     }
808     if( c == -1 )
809     printf(" EOF" );
810     else
811     printf(" %02x", c );
812     ++*i;
813     }
814    
815    
816     static int
817     copy_packet( gpg_iobuf_t inp, gpg_iobuf_t out, int pkttype, unsigned long pktlen )
818     {
819     int n;
820     char buf[100];
821    
822     if( gpg_iobuf_in_block_mode(inp) ) {
823     while( (n = gpg_iobuf_read( inp, buf, 100 )) != -1 )
824     if( gpg_iobuf_write(out, buf, n ) )
825     return G10ERR_WRITE_FILE; /* write error */
826     }
827     else if( !pktlen && pkttype == PKT_COMPRESSED ) {
828     printf("copy_packet: compressed!\n");
829     /* compressed packet, copy till EOF */
830     while( (n = gpg_iobuf_read( inp, buf, 100 )) != -1 )
831     if( gpg_iobuf_write(out, buf, n ) )
832     return G10ERR_WRITE_FILE; /* write error */
833     }
834     else {
835     for( ; pktlen; pktlen -= n ) {
836     n = pktlen > 100 ? 100 : pktlen;
837     n = gpg_iobuf_read( inp, buf, n );
838     if( n == -1 )
839     return G10ERR_READ_FILE;
840     if( gpg_iobuf_write(out, buf, n ) )
841     return G10ERR_WRITE_FILE; /* write error */
842     }
843     }
844     return 0;
845     }
846    
847    
848     static void
849     skip_packet( gpg_iobuf_t inp, int pkttype, unsigned long pktlen )
850     {
851     if( list_mode ) {
852     if( pkttype == PKT_MARKER )
853     fputs(":marker packet:\n", stdout );
854     else
855     printf(":unknown packet: type %2d, length %lu\n", pkttype, pktlen);
856     if( pkttype ) {
857     int c, i=0 ;
858     if( pkttype != PKT_MARKER )
859     fputs("dump:", stdout );
860     if( gpg_iobuf_in_block_mode(inp) ) {
861     while( (c=gpg_iobuf_get(inp)) != -1 )
862     dump_hex_line(c, &i);
863     }
864     else {
865     for( ; pktlen; pktlen-- )
866     dump_hex_line(gpg_iobuf_get(inp), &i);
867     }
868     putchar('\n');
869     return;
870     }
871     }
872     skip_rest(inp,pktlen);
873     }
874    
875     static void
876     skip_rest( gpg_iobuf_t inp, unsigned long pktlen )
877     {
878     if( gpg_iobuf_in_block_mode(inp) ) {
879     while( gpg_iobuf_get(inp) != -1 )
880     ;
881     }
882     else {
883     for( ; pktlen; pktlen-- )
884     if( gpg_iobuf_get(inp) == -1 )
885     break;
886     }
887     }
888    
889    
890     static void *
891     read_rest( gpg_iobuf_t inp, size_t pktlen )
892     {
893     byte *p;
894     int i;
895    
896     if( gpg_iobuf_in_block_mode(inp) ) {
897     printf("read_rest: can't store stream data\n");
898     p = NULL;
899     }
900     else {
901     p = malloc( pktlen );
902     for(i=0; pktlen; pktlen--, i++ )
903     p[i] = gpg_iobuf_get(inp);
904     }
905     return p;
906     }
907    
908    
909    
910     static int
911     parse_symkeyenc( gpg_iobuf_t inp, int pkttype, unsigned long pktlen, PACKET *packet )
912     {
913     PKT_symkey_enc *k;
914     int rc = 0;
915     int i, version, s2kmode, cipher_algo, hash_algo, seskeylen, minlen;
916    
917     if( pktlen < 4 ) {
918     printf("packet(%d) too short\n", pkttype);
919     rc = G10ERR_INVALID_PACKET;
920     goto leave;
921     }
922     version = gpg_iobuf_get_noeof(inp); pktlen--;
923     if( version != 4 ) {
924     printf("packet(%d) with unknown version %d\n", pkttype, version);
925     rc = G10ERR_INVALID_PACKET;
926     goto leave;
927     }
928     if( pktlen > 200 ) { /* (we encode the seskeylen in a byte) */
929     printf("packet(%d) too large\n", pkttype);
930     rc = G10ERR_INVALID_PACKET;
931     goto leave;
932     }
933     cipher_algo = gpg_iobuf_get_noeof(inp); pktlen--;
934     s2kmode = gpg_iobuf_get_noeof(inp); pktlen--;
935     hash_algo = gpg_iobuf_get_noeof(inp); pktlen--;
936     switch( s2kmode ) {
937     case 0: /* simple s2k */
938     minlen = 0;
939     break;
940     case 1: /* salted s2k */
941     minlen = 8;
942     break;
943     case 3: /* iterated+salted s2k */
944     minlen = 9;
945     break;
946     default:
947     printf("unknown S2K %d\n", s2kmode );
948     goto leave;
949     }
950     if( minlen > pktlen ) {
951     printf("packet with S2K %d too short\n", s2kmode );
952     rc = G10ERR_INVALID_PACKET;
953     goto leave;
954     }
955     seskeylen = pktlen - minlen;
956     k = packet->pkt.symkey_enc = calloc(1, sizeof *packet->pkt.symkey_enc
957     + seskeylen - 1 );
958     k->version = version;
959     k->cipher_algo = cipher_algo;
960     k->s2k.mode = s2kmode;
961     k->s2k.hash_algo = hash_algo;
962     if( s2kmode == 1 || s2kmode == 3 ) {
963     for(i=0; i < 8 && pktlen; i++, pktlen-- )
964     k->s2k.salt[i] = gpg_iobuf_get_noeof(inp);
965     }
966     if( s2kmode == 3 ) {
967     k->s2k.count = gpg_iobuf_get(inp); pktlen--;
968     }
969     k->seskeylen = seskeylen;
970     for(i=0; i < seskeylen && pktlen; i++, pktlen-- )
971     k->seskey[i] = gpg_iobuf_get_noeof(inp);
972     assert( !pktlen );
973    
974     if( list_mode ) {
975     printf(":symkey enc packet: version %d, cipher %d, s2k %d, hash %d\n",
976     version, cipher_algo, s2kmode, hash_algo);
977     if( s2kmode == 1 || s2kmode == 3 ) {
978     printf("\tsalt ");
979     for(i=0; i < 8; i++ )
980     printf("%02x", k->s2k.salt[i]);
981     if( s2kmode == 3 )
982     printf(", count %lu\n", (ulong)k->s2k.count );
983     printf("\n");
984     }
985     }
986    
987     leave:
988     skip_rest(inp, pktlen);
989     return rc;
990     }
991    
992     static int
993     parse_pubkeyenc( gpg_iobuf_t inp, int pkttype, unsigned long pktlen, PACKET *packet )
994     {
995     unsigned int n;
996     int rc = 0;
997     int i, ndata;
998     PKT_pubkey_enc *k;
999    
1000     k = packet->pkt.pubkey_enc = calloc(1, sizeof *packet->pkt.pubkey_enc);
1001     if( pktlen < 12 ) {
1002     printf("packet(%d) too short\n", pkttype);
1003     rc = G10ERR_INVALID_PACKET;
1004     goto leave;
1005     }
1006     k->version = gpg_iobuf_get_noeof(inp); pktlen--;
1007     if( k->version != 2 && k->version != 3 ) {
1008     printf("packet(%d) with unknown version %d\n", pkttype, k->version);
1009     rc = G10ERR_INVALID_PACKET;
1010     goto leave;
1011     }
1012     k->keyid[0] = read_32(inp); pktlen -= 4;
1013     k->keyid[1] = read_32(inp); pktlen -= 4;
1014     k->pubkey_algo = gpg_iobuf_get_noeof(inp); pktlen--;
1015     k->throw_keyid = 0; /* only used as flag for build_packet */
1016     if( list_mode )
1017     printf(":pubkey enc packet: version %d, algo %d, keyid %08lX%08lX\n",
1018     k->version, k->pubkey_algo, (ulong)k->keyid[0], (ulong)k->keyid[1]);
1019    
1020     ndata = pubkey_get_nenc(k->pubkey_algo);
1021     if( !ndata ) {
1022     if( list_mode )
1023     printf("\tunsupported algorithm %d\n", k->pubkey_algo );
1024     unknown_pubkey_warning( k->pubkey_algo );
1025     k->data[0] = NULL; /* no need to store the encrypted data */
1026     }
1027     else {
1028     for( i=0; i < ndata; i++ ) {
1029     n = pktlen;
1030     k->data[i] = mpi_read(inp, &n, 0); pktlen -=n;
1031     if( list_mode ) {
1032     printf("\tdata: ");
1033     mpi_print(stdout, k->data[i], mpi_print_mode );
1034     putchar('\n');
1035     }
1036     if (!k->data[i])
1037     rc = G10ERR_INVALID_PACKET;
1038     }
1039     }
1040    
1041     leave:
1042     skip_rest(inp, pktlen);
1043     return rc;
1044     }
1045    
1046    
1047     /****************
1048     * Returns: >= 0 offset into buffer
1049     * -1 unknown type
1050     * -2 unsupported type
1051     * -3 subpacket too short
1052     */
1053     int
1054     parse_one_sig_subpkt( const byte *buffer, size_t n, int type )
1055     {
1056     switch( type ) {
1057     case SIGSUBPKT_REV_KEY:
1058     if(n < 22)
1059     break;
1060     return 0;
1061     case SIGSUBPKT_SIG_CREATED:
1062     case SIGSUBPKT_SIG_EXPIRE:
1063     case SIGSUBPKT_KEY_EXPIRE:
1064     if( n < 4 )
1065     break;
1066     return 0;
1067     case SIGSUBPKT_KEY_FLAGS:
1068     case SIGSUBPKT_KS_FLAGS:
1069     case SIGSUBPKT_PREF_SYM:
1070     case SIGSUBPKT_PREF_HASH:
1071     case SIGSUBPKT_PREF_COMPR:
1072     case SIGSUBPKT_POLICY:
1073     case SIGSUBPKT_FEATURES:
1074     return 0;
1075     case SIGSUBPKT_EXPORTABLE:
1076     case SIGSUBPKT_REVOCABLE:
1077     if( !n )
1078     break;
1079     return 0;
1080     case SIGSUBPKT_ISSUER: /* issuer key ID */
1081     if( n < 8 )
1082     break;
1083     return 0;
1084     case SIGSUBPKT_NOTATION:
1085     if( n < 8 ) /* minimum length needed */
1086     break;
1087     return 0;
1088     case SIGSUBPKT_REVOC_REASON:
1089     if( !n )
1090     break;
1091     return 0;
1092     case SIGSUBPKT_PRIMARY_UID:
1093     if ( n != 1 )
1094     break;
1095     return 0;
1096     case SIGSUBPKT_PRIV_VERIFY_CACHE:
1097     /* We used this in gpg 1.0.5 and 1.0.6 to cache signature
1098     * verification results - it is no longer used.
1099     * "GPG" 0x00 <mode> <stat>
1100     * where mode == 1: valid data, stat == 0: invalid signature
1101     * stat == 1: valid signature
1102     * (because we use private data, we check our marker) */
1103     if( n < 6 )
1104     break;
1105     if( buffer[0] != 'G' || buffer[1] != 'P'
1106     || buffer[2] != 'G' || buffer[3] )
1107     return -2;
1108     return 4;
1109     default: return -1;
1110     }
1111     return -3;
1112     }
1113    
1114    
1115     static int
1116     can_handle_critical( const byte *buffer, size_t n, int type )
1117     {
1118     switch( type ) {
1119     case SIGSUBPKT_NOTATION:
1120     if( n >= 8 && (*buffer & 0x80) )
1121     return 1; /* human readable is handled */
1122     return 0;
1123    
1124     case SIGSUBPKT_SIG_CREATED:
1125     case SIGSUBPKT_SIG_EXPIRE:
1126     case SIGSUBPKT_KEY_EXPIRE:
1127     case SIGSUBPKT_EXPORTABLE:
1128     case SIGSUBPKT_REVOCABLE:
1129     case SIGSUBPKT_REV_KEY:
1130     case SIGSUBPKT_ISSUER:/* issuer key ID */
1131     case SIGSUBPKT_PREF_SYM:
1132     case SIGSUBPKT_PREF_HASH:
1133     case SIGSUBPKT_PREF_COMPR:
1134     case SIGSUBPKT_KEY_FLAGS:
1135     case SIGSUBPKT_PRIMARY_UID:
1136     case SIGSUBPKT_FEATURES:
1137     case SIGSUBPKT_POLICY: /* Is it enough to show the policy? */
1138     return 1;
1139    
1140     default:
1141     return 0;
1142     }
1143     }
1144    
1145    
1146     const byte *
1147     enum_sig_subpkt( const subpktarea_t *pktbuf, sigsubpkttype_t reqtype,
1148     size_t *ret_n, int *start, int *critical )
1149     {
1150     const byte *buffer;
1151     int buflen;
1152     int type;
1153     int critical_dummy;
1154     int offset;
1155     size_t n;
1156     int seq = 0;
1157     int reqseq = start? *start: 0;
1158    
1159     if(!critical)
1160     critical=&critical_dummy;
1161    
1162     if( !pktbuf || reqseq == -1 ) {
1163     /* return some value different from NULL to indicate that
1164     * there is no critical bit we do not understand. The caller
1165     * will never use the value. Yes I know, it is an ugly hack */
1166     return reqtype == SIGSUBPKT_TEST_CRITICAL? (const byte*)&pktbuf : NULL;
1167     }
1168     buffer = pktbuf->data;
1169     buflen = pktbuf->len;
1170     while( buflen ) {
1171     n = *buffer++; buflen--;
1172     if( n == 255 ) { /* 4 byte length header */
1173     if( buflen < 4 )
1174     goto too_short;
1175     n = (buffer[0] << 24) | (buffer[1] << 16)
1176     | (buffer[2] << 8) | buffer[3];
1177     buffer += 4;
1178     buflen -= 4;
1179     }
1180     else if( n >= 192 ) { /* 2 byte special encoded length header */
1181     if( buflen < 2 )
1182     goto too_short;
1183     n = (( n - 192 ) << 8) + *buffer + 192;
1184     buffer++;
1185     buflen--;
1186     }
1187     if( buflen < n )
1188     goto too_short;
1189     type = *buffer;
1190     if( type & 0x80 ) {
1191     type &= 0x7f;
1192     *critical = 1;
1193     }
1194     else
1195     *critical = 0;
1196     if( !(++seq > reqseq) )
1197     ;
1198     else if( reqtype == SIGSUBPKT_TEST_CRITICAL ) {
1199     if( *critical ) {
1200     if( n-1 > buflen+1 )
1201     goto too_short;
1202     if( !can_handle_critical(buffer+1, n-1, type ) ) {
1203     printf("subpacket of type %d has critical bit set\n",
1204     type);
1205     if( start )
1206     *start = seq;
1207     return NULL; /* this is an error */
1208     }
1209     }
1210     }
1211     else if( type == reqtype ) { /* found */
1212     buffer++;
1213     n--;
1214     if( n > buflen )
1215     goto too_short;
1216     if( ret_n )
1217     *ret_n = n;
1218     offset = parse_one_sig_subpkt(buffer, n, type );
1219     switch( offset ) {
1220     case -3:
1221     printf("subpacket of type %d too short\n", type);
1222     return NULL;
1223     case -2:
1224     return NULL;
1225     case -1:
1226     printf( "This is a BUG.\n%s:%d\n", __FILE__, __LINE__ );
1227     exit( -1 );
1228     default:
1229     break;
1230     }
1231     if( start )
1232     *start = seq;
1233     return buffer+offset;
1234     }
1235     buffer += n; buflen -=n;
1236     }
1237     if( reqtype == SIGSUBPKT_TEST_CRITICAL )
1238     return buffer; /* as value true to indicate that there is no */
1239     /* critical bit we don't understand */
1240     if( start )
1241     *start = -1;
1242     return NULL; /* end of packets; not found */
1243    
1244     too_short:
1245     printf("buffer shorter than subpacket\n");
1246     if( start )
1247     *start = -1;
1248     return NULL;
1249     }
1250    
1251    
1252     const byte *
1253     gpg_parse_sig_subpkt (const subpktarea_t *buffer, sigsubpkttype_t reqtype,
1254     size_t *ret_n)
1255     {
1256     return enum_sig_subpkt( buffer, reqtype, ret_n, NULL, NULL );
1257     }
1258    
1259     const byte *
1260     gpg_parse_sig_subpkt2 (PKT_signature *sig, sigsubpkttype_t reqtype,
1261     size_t *ret_n )
1262     {
1263     const byte *p;
1264    
1265     p = gpg_parse_sig_subpkt (sig->hashed, reqtype, ret_n );
1266     if( !p )
1267     p = gpg_parse_sig_subpkt (sig->unhashed, reqtype, ret_n );
1268     return p;
1269     }
1270    
1271     /* Find all revocation keys. Look in hashed area only. */
1272     void parse_revkeys(PKT_signature *sig)
1273     {
1274     struct revocation_key *revkey;
1275     int seq=0;
1276     size_t len;
1277    
1278     if( sig->sig_class != 0x1F )
1279     return;
1280    
1281     while((revkey=
1282     (struct revocation_key *)enum_sig_subpkt(sig->hashed,
1283     SIGSUBPKT_REV_KEY,
1284     &len,&seq,NULL))) {
1285     if( len==sizeof(struct revocation_key) &&
1286     (revkey->rclass&0x80)) /* 0x80 bit must be set */ {
1287     sig->revkey=realloc(sig->revkey,
1288     sizeof(struct revocation_key *)*(sig->numrevkeys+1));
1289     sig->revkey[sig->numrevkeys]=revkey;
1290     sig->numrevkeys++;
1291     }
1292     }
1293     }
1294    
1295     static int
1296     parse_signature( gpg_iobuf_t inp, int pkttype, unsigned long pktlen,
1297     PKT_signature *sig )
1298     {
1299     int md5_len=0;
1300     unsigned n;
1301     int is_v4=0;
1302     int rc=0;
1303     int i, ndata;
1304    
1305     if( pktlen < 16 ) {
1306     printf("packet(%d) too short\n", pkttype);
1307     goto leave;
1308     }
1309     sig->version = gpg_iobuf_get_noeof(inp); pktlen--;
1310     if( sig->version == 4 )
1311     is_v4=1;
1312     else if( sig->version != 2 && sig->version != 3 ) {
1313     printf("packet(%d) with unknown version %d\n", pkttype, sig->version);
1314     rc = G10ERR_INVALID_PACKET;
1315     goto leave;
1316     }
1317    
1318     if( !is_v4 ) {
1319     md5_len = gpg_iobuf_get_noeof(inp); pktlen--;
1320     }
1321     sig->sig_class = gpg_iobuf_get_noeof(inp); pktlen--;
1322     if( !is_v4 ) {
1323     sig->timestamp = read_32(inp); pktlen -= 4;
1324     sig->keyid[0] = read_32(inp); pktlen -= 4;
1325     sig->keyid[1] = read_32(inp); pktlen -= 4;
1326     }
1327     sig->pubkey_algo = gpg_iobuf_get_noeof(inp); pktlen--;
1328     sig->digest_algo = gpg_iobuf_get_noeof(inp); pktlen--;
1329     sig->flags.exportable=1;
1330     sig->flags.revocable=1;
1331     if( is_v4 ) { /* read subpackets */
1332     n = read_16(inp); pktlen -= 2; /* length of hashed data */
1333     if( n > 10000 ) {
1334     printf("signature packet: hashed data too long\n");
1335     rc = G10ERR_INVALID_PACKET;
1336     goto leave;
1337     }
1338     if( n ) {
1339     sig->hashed = malloc (sizeof (*sig->hashed) + n - 1 );
1340     sig->hashed->size = n;
1341     sig->hashed->len = n;
1342     if( gpg_iobuf_read (inp, sig->hashed->data, n ) != n ) {
1343     printf ("premature eof while reading "
1344     "hashed signature data\n");
1345     rc = -1;
1346     goto leave;
1347     }
1348     pktlen -= n;
1349     }
1350     n = read_16(inp); pktlen -= 2; /* length of unhashed data */
1351     if( n > 10000 ) {
1352     printf("signature packet: unhashed data too long\n");
1353     rc = G10ERR_INVALID_PACKET;
1354     goto leave;
1355     }
1356     if( n ) {
1357     /* we add 8 extra bytes so that we have space for the signature
1358     * status cache. Well we are wastin this if there is a cache
1359     * packet already, but in the other case it avoids an realloc */
1360     sig->unhashed = malloc (sizeof(*sig->unhashed) + n + 8 - 1 );
1361     sig->unhashed->size = n + 8;
1362     sig->unhashed->len = n;
1363     if( gpg_iobuf_read(inp, sig->unhashed->data, n ) != n ) {
1364     printf("premature eof while reading "
1365     "unhashed signature data\n");
1366     rc = -1;
1367     goto leave;
1368     }
1369     pktlen -= n;
1370     }
1371     }
1372    
1373     if( pktlen < 5 ) { /* sanity check */
1374     printf("packet(%d) too short\n", pkttype);
1375     rc = G10ERR_INVALID_PACKET;
1376     goto leave;
1377     }
1378    
1379     sig->digest_start[0] = gpg_iobuf_get_noeof(inp); pktlen--;
1380     sig->digest_start[1] = gpg_iobuf_get_noeof(inp); pktlen--;
1381    
1382     if( is_v4 && sig->pubkey_algo ) { /*extract required information */
1383     const byte *p;
1384    
1385     /* set sig->flags.unknown_critical if there is a
1386     * critical bit set for packets which we do not understand */
1387     if( !gpg_parse_sig_subpkt (sig->hashed, SIGSUBPKT_TEST_CRITICAL, NULL)
1388     || !gpg_parse_sig_subpkt (sig->unhashed, SIGSUBPKT_TEST_CRITICAL,
1389     NULL) )
1390     {
1391     sig->flags.unknown_critical = 1;
1392     }
1393    
1394     p = gpg_parse_sig_subpkt (sig->hashed, SIGSUBPKT_SIG_CREATED, NULL );
1395     if( !p )
1396     printf("signature packet without timestamp\n");
1397     else
1398     sig->timestamp = buffer_to_u32(p);
1399     p = gpg_parse_sig_subpkt2( sig, SIGSUBPKT_ISSUER, NULL );
1400     if( !p )
1401     printf("signature packet without keyid\n");
1402     else {
1403     sig->keyid[0] = buffer_to_u32(p);
1404     sig->keyid[1] = buffer_to_u32(p+4);
1405     }
1406    
1407     p=gpg_parse_sig_subpkt(sig->hashed,SIGSUBPKT_SIG_EXPIRE,NULL);
1408     if(p)
1409     sig->expiredate=sig->timestamp+buffer_to_u32(p);
1410     if(sig->expiredate && sig->expiredate<=time(NULL))
1411     sig->flags.expired=1;
1412    
1413     p=gpg_parse_sig_subpkt(sig->hashed,SIGSUBPKT_POLICY,NULL);
1414     if(p)
1415     sig->flags.policy_url=1;
1416    
1417     p=gpg_parse_sig_subpkt(sig->hashed,SIGSUBPKT_NOTATION,NULL);
1418     if(p)
1419     sig->flags.notation=1;
1420    
1421     p=gpg_parse_sig_subpkt(sig->hashed,SIGSUBPKT_REVOCABLE,NULL);
1422     if(p && *p==0)
1423     sig->flags.revocable=0;
1424    
1425     /* We accept the exportable subpacket from either the hashed
1426     or unhashed areas as older versions of gpg put it in the
1427     unhashed area. In theory, anyway, we should never see this
1428     packet off of a local keyring. */
1429    
1430     p=gpg_parse_sig_subpkt2(sig,SIGSUBPKT_EXPORTABLE,NULL);
1431     if(p && *p==0)
1432     sig->flags.exportable=0;
1433    
1434     /* Find all revocation keys. */
1435     if(sig->sig_class==0x1F)
1436     parse_revkeys(sig);
1437     }
1438    
1439     if( list_mode ) {
1440     printf(":signature packet: algo %d, keyid %08lX%08lX\n"
1441     "\tversion %d, created %lu, md5len %d, sigclass %02x\n"
1442     "\tdigest algo %d, begin of digest %02x %02x\n",
1443     sig->pubkey_algo,
1444     (ulong)sig->keyid[0], (ulong)sig->keyid[1],
1445     sig->version, (ulong)sig->timestamp, md5_len, sig->sig_class,
1446     sig->digest_algo,
1447     sig->digest_start[0], sig->digest_start[1] );
1448     if( is_v4 ) {
1449     gpg_parse_sig_subpkt (sig->hashed, SIGSUBPKT_LIST_HASHED, NULL );
1450     gpg_parse_sig_subpkt (sig->unhashed, SIGSUBPKT_LIST_UNHASHED, NULL);
1451     }
1452     }
1453    
1454     ndata = pubkey_get_nsig(sig->pubkey_algo);
1455     if( !ndata ) {
1456     if( list_mode )
1457     printf("\tunknown algorithm %d\n", sig->pubkey_algo );
1458     unknown_pubkey_warning( sig->pubkey_algo );
1459     /* we store the plain material in data[0], so that we are able
1460     * to write it back with build_packet() */
1461     sig->data[0] = mpi_set_opaque(NULL, read_rest(inp, pktlen), pktlen );
1462     pktlen = 0;
1463     }
1464     else {
1465     for( i=0; i < ndata; i++ ) {
1466     n = pktlen;
1467     sig->data[i] = mpi_read(inp, &n, 0 );
1468     pktlen -=n;
1469     if( list_mode ) {
1470     printf("\tdata: ");
1471     mpi_print(stdout, sig->data[i], mpi_print_mode );
1472     putchar('\n');
1473     }
1474     if (!sig->data[i])
1475     rc = G10ERR_INVALID_PACKET;
1476     }
1477     }
1478    
1479     leave:
1480     skip_rest(inp, pktlen);
1481     return rc;
1482     }
1483    
1484    
1485     static int
1486     parse_onepass_sig( gpg_iobuf_t inp, int pkttype, unsigned long pktlen,
1487     PKT_onepass_sig *ops )
1488     {
1489     int version;
1490     int rc = 0;
1491    
1492     if( pktlen < 13 ) {
1493     printf("packet(%d) too short\n", pkttype);
1494     rc = G10ERR_INVALID_PACKET;
1495     goto leave;
1496     }
1497     version = gpg_iobuf_get_noeof(inp); pktlen--;
1498     if( version != 3 ) {
1499     printf("onepass_sig with unknown version %d\n", version);
1500     rc = G10ERR_INVALID_PACKET;
1501     goto leave;
1502     }
1503     ops->sig_class = gpg_iobuf_get_noeof(inp); pktlen--;
1504     ops->digest_algo = gpg_iobuf_get_noeof(inp); pktlen--;
1505     ops->pubkey_algo = gpg_iobuf_get_noeof(inp); pktlen--;
1506     ops->keyid[0] = read_32(inp); pktlen -= 4;
1507     ops->keyid[1] = read_32(inp); pktlen -= 4;
1508     ops->last = gpg_iobuf_get_noeof(inp); pktlen--;
1509     if( list_mode )
1510     printf(":onepass_sig packet: keyid %08lX%08lX\n"
1511     "\tversion %d, sigclass %02x, digest %d, pubkey %d, last=%d\n",
1512     (ulong)ops->keyid[0], (ulong)ops->keyid[1],
1513     version, ops->sig_class,
1514     ops->digest_algo, ops->pubkey_algo, ops->last );
1515    
1516    
1517     leave:
1518     skip_rest(inp, pktlen);
1519     return rc;
1520     }
1521    
1522    
1523     static int
1524     parse_key( gpg_iobuf_t inp, int pkttype, unsigned long pktlen,
1525     byte *hdr, int hdrlen, PACKET *pkt )
1526     {
1527     int i, version, algorithm;
1528     unsigned n;
1529     unsigned long timestamp, expiredate, max_expiredate;
1530     int npkey, nskey;
1531     int is_v4=0;
1532     int rc=0;
1533    
1534     version = gpg_iobuf_get_noeof(inp); pktlen--;
1535     if( pkttype == PKT_PUBLIC_SUBKEY && version == '#' ) {
1536     /* early versions of G10 use old PGP comments packets;
1537     * luckily all those comments are started by a hash */
1538     if( list_mode ) {
1539     printf(":rfc1991 comment packet: \"" );
1540     for( ; pktlen; pktlen-- ) {
1541     int c;
1542     c = gpg_iobuf_get_noeof(inp);
1543     if( c >= ' ' && c <= 'z' )
1544     putchar(c);
1545     else
1546     printf("\\x%02x", c );
1547     }
1548     printf("\"\n");
1549     }
1550     skip_rest(inp, pktlen);
1551     return 0;
1552     }
1553     else if( version == 4 )
1554     is_v4=1;
1555     else if( version != 2 && version != 3 ) {
1556     printf("packet(%d) with unknown version %d\n", pkttype, version);
1557     rc = G10ERR_INVALID_PACKET;
1558     goto leave;
1559     }
1560    
1561     if( pktlen < 11 ) {
1562     printf("packet(%d) too short\n", pkttype);
1563     rc = G10ERR_INVALID_PACKET;
1564     goto leave;
1565     }
1566    
1567     timestamp = read_32(inp); pktlen -= 4;
1568     if( is_v4 ) {
1569     expiredate = 0; /* have to get it from the selfsignature */
1570     max_expiredate = 0;
1571     }
1572     else {
1573     unsigned short ndays;
1574     ndays = read_16(inp); pktlen -= 2;
1575     if( ndays )
1576     expiredate = timestamp + ndays * 86400L;
1577     else
1578     expiredate = 0;
1579    
1580     max_expiredate=expiredate;
1581     }
1582     algorithm = gpg_iobuf_get_noeof(inp); pktlen--;
1583     if( list_mode )
1584     printf(":%s key packet:\n"
1585     "\tversion %d, algo %d, created %lu, expires %lu\n",
1586     pkttype == PKT_PUBLIC_KEY? "public" :
1587     pkttype == PKT_SECRET_KEY? "secret" :
1588     pkttype == PKT_PUBLIC_SUBKEY? "public sub" :
1589     pkttype == PKT_SECRET_SUBKEY? "secret sub" : "??",
1590     version, algorithm, timestamp, expiredate );
1591    
1592     if( pkttype == PKT_SECRET_KEY || pkttype == PKT_SECRET_SUBKEY ) {
1593     PKT_secret_key *sk = pkt->pkt.secret_key;
1594    
1595     sk->timestamp = timestamp;
1596     sk->expiredate = expiredate;
1597     sk->max_expiredate = max_expiredate;
1598     sk->hdrbytes = hdrlen;
1599     sk->version = version;
1600     sk->is_primary = pkttype == PKT_SECRET_KEY;
1601     sk->pubkey_algo = algorithm;
1602     sk->req_usage = 0;
1603     sk->pubkey_usage = 0; /* not yet used */
1604     }
1605     else {
1606     PKT_public_key *pk = pkt->pkt.public_key;
1607    
1608     pk->timestamp = timestamp;
1609     pk->expiredate = expiredate;
1610     pk->max_expiredate = max_expiredate;
1611     pk->hdrbytes = hdrlen;
1612     pk->version = version;
1613     pk->is_primary = pkttype == PKT_PUBLIC_KEY;
1614     pk->pubkey_algo = algorithm;
1615     pk->req_usage = 0;
1616     pk->pubkey_usage = 0; /* not yet used */
1617     pk->is_revoked = 0;
1618     pk->keyid[0] = 0;
1619     pk->keyid[1] = 0;
1620     }
1621     nskey = pubkey_get_nskey( algorithm );
1622     npkey = pubkey_get_npkey( algorithm );
1623     if( !npkey ) {
1624     if( list_mode )
1625     printf("\tunknown algorithm %d\n", algorithm );
1626     unknown_pubkey_warning( algorithm );
1627     }
1628    
1629    
1630     if( pkttype == PKT_SECRET_KEY || pkttype == PKT_SECRET_SUBKEY ) {
1631     PKT_secret_key *sk = pkt->pkt.secret_key;
1632     byte temp[16];
1633     size_t snlen = 0;
1634    
1635     if( !npkey ) {
1636     sk->skey[0] = mpi_set_opaque( NULL,
1637     read_rest(inp, pktlen), pktlen );
1638     pktlen = 0;
1639     goto leave;
1640     }
1641    
1642     for(i=0; i < npkey; i++ ) {
1643     n = pktlen; sk->skey[i] = mpi_read(inp, &n, 0 ); pktlen -=n;
1644     if( list_mode ) {
1645     printf( "\tskey[%d]: ", i);
1646     mpi_print(stdout, sk->skey[i], mpi_print_mode );
1647     putchar('\n');
1648     }
1649     if (!sk->skey[i])
1650     rc = G10ERR_INVALID_PACKET;
1651     }
1652     if (rc) /* one of the MPIs were bad */
1653     goto leave;
1654     sk->protect.algo = gpg_iobuf_get_noeof(inp); pktlen--;
1655     sk->protect.sha1chk = 0;
1656     if( sk->protect.algo ) {
1657     sk->is_protected = 1;
1658     sk->protect.s2k.count = 0;
1659     if( sk->protect.algo == 254 || sk->protect.algo == 255 ) {
1660     if( pktlen < 3 ) {
1661     rc = G10ERR_INVALID_PACKET;
1662     goto leave;
1663     }
1664     sk->protect.sha1chk = (sk->protect.algo == 254);
1665     sk->protect.algo = gpg_iobuf_get_noeof(inp); pktlen--;
1666     /* Note that a sk->protect.algo > 110 is illegal, but
1667     I'm not erroring on it here as otherwise there
1668     would be no way to delete such a key. */
1669     sk->protect.s2k.mode = gpg_iobuf_get_noeof(inp); pktlen--;
1670     sk->protect.s2k.hash_algo = gpg_iobuf_get_noeof(inp); pktlen--;
1671     /* check for the special GNU extension */
1672     if( is_v4 && sk->protect.s2k.mode == 101 ) {
1673     for(i=0; i < 4 && pktlen; i++, pktlen-- )
1674     temp[i] = gpg_iobuf_get_noeof(inp);
1675     if( i < 4 || memcmp( temp, "GNU", 3 ) ) {
1676     if( list_mode )
1677     printf( "\tunknown S2K %d\n",
1678     sk->protect.s2k.mode );
1679     rc = G10ERR_INVALID_PACKET;
1680     goto leave;
1681     }
1682     /* here we know that it is a gnu extension
1683     * What follows is the GNU protection mode:
1684     * All values have special meanings
1685     * and they are mapped in the mode with a base of 1000.
1686     */
1687     sk->protect.s2k.mode = 1000 + temp[3];
1688     }
1689     switch( sk->protect.s2k.mode ) {
1690     case 1:
1691     case 3:
1692     for(i=0; i < 8 && pktlen; i++, pktlen-- )
1693     temp[i] = gpg_iobuf_get_noeof(inp);
1694     memcpy(sk->protect.s2k.salt, temp, 8 );
1695     break;
1696     }
1697     switch( sk->protect.s2k.mode ) {
1698     case 0: if( list_mode ) printf( "\tsimple S2K" );
1699     break;
1700     case 1: if( list_mode ) printf( "\tsalted S2K" );
1701     break;
1702     case 3: if( list_mode ) printf( "\titer+salt S2K" );
1703     break;
1704     case 1001: if( list_mode ) printf( "\tgnu-dummy S2K" );
1705     break;
1706     case 1002: if (list_mode) printf("\tgnu-divert-to-card S2K");
1707     break;
1708     default:
1709     if( list_mode )
1710     printf( "\tunknown %sS2K %d\n",
1711     sk->protect.s2k.mode < 1000? "":"GNU ",
1712     sk->protect.s2k.mode );
1713     rc = G10ERR_INVALID_PACKET;
1714     goto leave;
1715     }
1716    
1717     if( list_mode ) {
1718     printf(", algo: %d,%s hash: %d",
1719     sk->protect.algo,
1720     sk->protect.sha1chk?" SHA1 protection,"
1721     :" simple checksum,",
1722     sk->protect.s2k.hash_algo );
1723     if( sk->protect.s2k.mode == 1
1724     || sk->protect.s2k.mode == 3 ) {
1725     printf(", salt: ");
1726     for(i=0; i < 8; i++ )
1727     printf("%02x", sk->protect.s2k.salt[i]);
1728     }
1729     putchar('\n');
1730     }
1731    
1732     if( sk->protect.s2k.mode == 3 ) {
1733     if( pktlen < 1 ) {
1734     rc = G10ERR_INVALID_PACKET;
1735     goto leave;
1736     }
1737     sk->protect.s2k.count = gpg_iobuf_get(inp);
1738     pktlen--;
1739     if( list_mode )
1740     printf("\tprotect count: %lu\n",
1741     (ulong)sk->protect.s2k.count);
1742     }
1743     else if( sk->protect.s2k.mode == 1002 ) {
1744     /* Read the serial number. */
1745     if (pktlen < 1) {
1746     rc = G10ERR_INVALID_PACKET;
1747     goto leave;
1748     }
1749     snlen = gpg_iobuf_get (inp);
1750     pktlen--;
1751     if (pktlen < snlen || snlen == -1) {
1752     rc = G10ERR_INVALID_PACKET;
1753     goto leave;
1754     }
1755     }
1756     }
1757     /* Note that a sk->protect.algo > 110 is illegal, but I'm
1758     not erroring on it here as otherwise there would be no
1759     way to delete such a key. */
1760     else { /* old version; no S2K, so we set mode to 0, hash MD5 */
1761     sk->protect.s2k.mode = 0;
1762     sk->protect.s2k.hash_algo = DIGEST_ALGO_MD5;
1763     if( list_mode )
1764     printf( "\tprotect algo: %d (hash algo: %d)\n",
1765     sk->protect.algo, sk->protect.s2k.hash_algo );
1766     }
1767     /* It is really ugly that we don't know the size
1768     * of the IV here in cases we are not aware of the algorithm.
1769     * so a
1770     * sk->protect.ivlen = cipher_get_blocksize(sk->protect.algo);
1771     * won't work. The only solution I see is to hardwire it here.
1772     * NOTE: if you change the ivlen above 16, don't forget to
1773     * enlarge temp.
1774     */
1775     switch( sk->protect.algo ) {
1776     case 7: case 8: case 9: /* reserved for AES */
1777     case 10: /* Twofish */
1778     sk->protect.ivlen = 16;
1779     break;
1780     default:
1781     sk->protect.ivlen = 8;
1782     }
1783     if( sk->protect.s2k.mode == 1001 )
1784     sk->protect.ivlen = 0;
1785     else if( sk->protect.s2k.mode == 1002 )
1786     sk->protect.ivlen = snlen < 16? snlen : 16;
1787    
1788     if( pktlen < sk->protect.ivlen ) {
1789     rc = G10ERR_INVALID_PACKET;
1790     goto leave;
1791     }
1792     for(i=0; i < sk->protect.ivlen && pktlen; i++, pktlen-- )
1793     temp[i] = gpg_iobuf_get_noeof(inp);
1794     if( list_mode ) {
1795     printf( sk->protect.s2k.mode == 1002? "\tserial-number: "
1796     : "\tprotect IV: ");
1797     for(i=0; i < sk->protect.ivlen; i++ )
1798     printf(" %02x", temp[i] );
1799     putchar('\n');
1800     }
1801     memcpy( sk->protect.iv, temp, sk->protect.ivlen );
1802     }
1803     else
1804     sk->is_protected = 0;
1805     /* It does not make sense to read it into secure memory.
1806     * If the user is so careless, not to protect his secret key,
1807     * we can assume, that he operates an open system :=(.
1808     * So we put the key into secure memory when we unprotect it. */
1809     if( sk->protect.s2k.mode == 1001
1810     || sk->protect.s2k.mode == 1002 ) {
1811     /* better set some dummy stuff here */
1812     sk->skey[npkey] = mpi_set_opaque(NULL, strdup("dummydata"), 10);
1813     pktlen = 0;
1814     }
1815     else if( is_v4 && sk->is_protected ) {
1816     /* ugly; the length is encrypted too, so we read all
1817     * stuff up to the end of the packet into the first
1818     * skey element */
1819     sk->skey[npkey] = mpi_set_opaque(NULL,
1820     read_rest(inp, pktlen), pktlen );
1821     pktlen = 0;
1822     if( list_mode ) {
1823     printf("\tencrypted stuff follows\n");
1824     }
1825     }
1826     else { /* v3 method: the mpi length is not encrypted */
1827     for(i=npkey; i < nskey; i++ ) {
1828     n = pktlen; sk->skey[i] = mpi_read(inp, &n, 0 ); pktlen -=n;
1829     if( sk->is_protected && sk->skey[i] )
1830     mpi_set_protect_flag(sk->skey[i]);
1831     if( list_mode ) {
1832     printf( "\tskey[%d]: ", i);
1833     if( sk->is_protected )
1834     printf( "[encrypted]\n");
1835     else {
1836     mpi_print(stdout, sk->skey[i], mpi_print_mode );
1837     putchar('\n');
1838     }
1839     }
1840     if (!sk->skey[i])
1841     rc = G10ERR_INVALID_PACKET;
1842     }
1843     if (rc)
1844     goto leave;
1845    
1846     sk->csum = read_16(inp); pktlen -= 2;
1847     if( list_mode ) {
1848     printf("\tchecksum: %04hx\n", sk->csum);
1849     }
1850     }
1851     }
1852     else {
1853     PKT_public_key *pk = pkt->pkt.public_key;
1854    
1855     if( !npkey ) {
1856     pk->pkey[0] = mpi_set_opaque( NULL,
1857     read_rest(inp, pktlen), pktlen );
1858     pktlen = 0;
1859     goto leave;
1860     }
1861    
1862     for(i=0; i < npkey; i++ ) {
1863     n = pktlen; pk->pkey[i] = mpi_read(inp, &n, 0 ); pktlen -=n;
1864     if( list_mode ) {
1865     printf( "\tpkey[%d]: ", i);
1866     mpi_print(stdout, pk->pkey[i], mpi_print_mode );
1867     putchar('\n');
1868     }
1869     if (!pk->pkey[i])
1870     rc = G10ERR_INVALID_PACKET;
1871     }
1872     if (rc)
1873     goto leave;
1874     }
1875    
1876     leave:
1877     skip_rest(inp, pktlen);
1878     return rc;
1879     }
1880    
1881     /* Attribute subpackets have the same format as v4 signature
1882     subpackets. This is not part of OpenPGP, but is done in several
1883     versions of PGP nevertheless. */
1884     int
1885     parse_attribute_subpkts(PKT_user_id *uid)
1886     {
1887     size_t n;
1888     int count=0;
1889     struct user_attribute *attribs=NULL;
1890     const byte *buffer=uid->attrib_data;
1891     int buflen=uid->attrib_len;
1892     byte type;
1893    
1894     safe_free(uid->attribs);
1895    
1896     while(buflen) {
1897     n = *buffer++; buflen--;
1898     if( n == 255 ) { /* 4 byte length header */
1899     if( buflen < 4 )
1900     goto too_short;
1901     n = (buffer[0] << 24) | (buffer[1] << 16)
1902     | (buffer[2] << 8) | buffer[3];
1903     buffer += 4;
1904     buflen -= 4;
1905     }
1906     else if( n >= 192 ) { /* 2 byte special encoded length header */
1907     if( buflen < 2 )
1908     goto too_short;
1909     n = (( n - 192 ) << 8) + *buffer + 192;
1910     buffer++;
1911     buflen--;
1912     }
1913     if( buflen < n )
1914     goto too_short;
1915    
1916     attribs=realloc(attribs,(count+1)*sizeof(struct user_attribute));
1917     memset(&attribs[count],0,sizeof(struct user_attribute));
1918    
1919     type=*buffer;
1920     buffer++;
1921     buflen--;
1922     n--;
1923    
1924     attribs[count].type=type;
1925     attribs[count].data=buffer;
1926     attribs[count].len=n;
1927     buffer+=n;
1928     buflen-=n;
1929     count++;
1930     }
1931    
1932     uid->attribs=attribs;
1933     uid->numattribs=count;
1934     return count;
1935    
1936     too_short:
1937     printf("buffer shorter than attribute subpacket\n");
1938     uid->attribs=attribs;
1939     uid->numattribs=count;
1940     return count;
1941     }
1942    
1943     static void
1944     setup_user_id(PACKET *packet)
1945     {
1946     packet->pkt.user_id->ref = 1;
1947     packet->pkt.user_id->attribs = NULL;
1948     packet->pkt.user_id->attrib_data = NULL;
1949     packet->pkt.user_id->attrib_len = 0;
1950     packet->pkt.user_id->is_primary = 0;
1951     packet->pkt.user_id->is_revoked = 0;
1952     packet->pkt.user_id->is_expired = 0;
1953     packet->pkt.user_id->expiredate = 0;
1954     packet->pkt.user_id->created = 0;
1955     packet->pkt.user_id->help_key_usage = 0;
1956     packet->pkt.user_id->help_key_expire = 0;
1957     packet->pkt.user_id->prefs = NULL;
1958     }
1959    
1960     static int
1961     parse_user_id( gpg_iobuf_t inp, int pkttype, unsigned long pktlen, PACKET *packet )
1962     {
1963     byte *p;
1964    
1965     packet->pkt.user_id = malloc(sizeof *packet->pkt.user_id + pktlen);
1966     packet->pkt.user_id->len = pktlen;
1967    
1968     setup_user_id(packet);
1969    
1970     p = packet->pkt.user_id->name;
1971     for( ; pktlen; pktlen--, p++ )
1972     *p = gpg_iobuf_get_noeof(inp);
1973     *p = 0;
1974    
1975     if( list_mode ) {
1976     int n = packet->pkt.user_id->len;
1977     printf(":user ID packet: \"");
1978     /* fixme: Hey why don't we replace this with print_string?? */
1979     for(p=packet->pkt.user_id->name; n; p++, n-- ) {
1980     if( *p >= ' ' && *p <= 'z' )
1981     putchar(*p);
1982     else
1983     printf("\\x%02x", *p );
1984     }
1985     printf("\"\n");
1986     }
1987     return 0;
1988     }
1989    
1990     /* Returns 0 for error, 1 for valid */
1991     int parse_image_header(const struct user_attribute *attr,byte *type,u32 *len)
1992     {
1993     u16 headerlen;
1994    
1995     if(attr->len<3)
1996     return 0;
1997    
1998     /* For historical reasons (i.e. "oops!"), the header length is
1999     little endian. */
2000     headerlen=(attr->data[1]<<8) | attr->data[0];
2001    
2002     if(headerlen>attr->len)
2003     return 0;
2004    
2005     if(type && attr->len>=4)
2006     {
2007     if(attr->data[2]==1) /* header version 1 */
2008     *type=attr->data[3];
2009     else
2010     *type=0;
2011     }
2012    
2013     *len=attr->len-headerlen;
2014    
2015     if(*len==0)
2016     return 0;
2017    
2018     return 1;
2019     }
2020    
2021     /* style==0 for extension, 1 for name, 2 for MIME type. Remember that
2022     the "name" style string could be used in a user ID name field, so
2023     make sure it is not too big (see
2024     parse-packet.c:parse_attribute). */
2025     char *image_type_to_string(byte type,int style)
2026     {
2027     char *string;
2028    
2029     switch(type) {
2030     case 1: /* jpeg */
2031     if(style==0)
2032     string="jpg";
2033     else if(style==1)
2034     string="jpeg";
2035     else
2036     string="image/jpeg";
2037     break;
2038    
2039     default:
2040     if(style==0)
2041     string="bin";
2042     else if(style==1)
2043     string="unknown";
2044     else
2045     string="image/x-unknown";
2046     break;
2047     }
2048    
2049     return string;
2050     }
2051    
2052     void
2053     make_attribute_uidname(PKT_user_id *uid)
2054     {
2055     if(uid->numattribs<=0)
2056     sprintf(uid->name,"[bad attribute packet of size %lu]",uid->attrib_len);
2057     else if(uid->numattribs>1)
2058     sprintf(uid->name,"[%d attributes of size %lu]",
2059     uid->numattribs,uid->attrib_len);
2060     else {
2061     /* Only one attribute, so list it as the "user id" */
2062    
2063     if(uid->attribs->type==ATTRIB_IMAGE) {
2064     u32 len;
2065     byte type;
2066    
2067     if(parse_image_header(uid->attribs,&type,&len))
2068     sprintf(uid->name,"[%s image of size %lu]",
2069     image_type_to_string(type,1),(ulong)len);
2070     else
2071     sprintf(uid->name,"[invalid image]");
2072     }
2073     else
2074     sprintf(uid->name,"[unknown attribute of size %lu]",
2075     (ulong)uid->attribs->len);
2076     }
2077    
2078     uid->len = strlen(uid->name);
2079     }
2080    
2081     static int
2082     parse_attribute( gpg_iobuf_t inp, int pkttype, unsigned long pktlen, PACKET *packet )
2083     {
2084     byte *p;
2085    
2086     packet->pkt.user_id = malloc(sizeof *packet->pkt.user_id + 70);
2087    
2088     setup_user_id(packet);
2089    
2090     packet->pkt.user_id->attrib_data = malloc(pktlen);
2091     packet->pkt.user_id->attrib_len = pktlen;
2092     p = packet->pkt.user_id->attrib_data;
2093     for( ; pktlen; pktlen--, p++ )
2094     *p = gpg_iobuf_get_noeof(inp);
2095    
2096     /* Now parse out the individual attribute subpackets. This is
2097     somewhat pointless since there is only one currently defined
2098     attribute type (jpeg), but it is correct by the spec. */
2099     parse_attribute_subpkts(packet->pkt.user_id);
2100    
2101     make_attribute_uidname(packet->pkt.user_id);
2102    
2103     if( list_mode ) {
2104     printf(":attribute packet: %s\n", packet->pkt.user_id->name );
2105     }
2106     return 0;
2107     }
2108    
2109    
2110     static int
2111     parse_comment( gpg_iobuf_t inp, int pkttype, unsigned long pktlen, PACKET *packet )
2112     {
2113     byte *p;
2114    
2115     packet->pkt.comment = malloc(sizeof *packet->pkt.comment + pktlen - 1);
2116     packet->pkt.comment->len = pktlen;
2117     p = packet->pkt.comment->data;
2118     for( ; pktlen; pktlen--, p++ )
2119     *p = gpg_iobuf_get_noeof(inp);
2120    
2121     if( list_mode ) {
2122     int n = packet->pkt.comment->len;
2123     printf(":%scomment packet: \"", pkttype == PKT_OLD_COMMENT?
2124     "OpenPGP draft " : "" );
2125     for(p=packet->pkt.comment->data; n; p++, n-- ) {
2126     if( *p >= ' ' && *p <= 'z' )
2127     putchar(*p);
2128     else
2129     printf("\\x%02x", *p );
2130     }
2131     printf("\"\n");
2132     }
2133     return 0;
2134     }
2135    
2136    
2137     static void
2138     parse_trust( gpg_iobuf_t inp, int pkttype, unsigned long pktlen, PACKET *pkt )
2139     {
2140     int c;
2141    
2142     if (pktlen) {
2143     c = gpg_iobuf_get_noeof(inp);
2144     pktlen--;
2145     pkt->pkt.ring_trust = malloc( sizeof *pkt->pkt.ring_trust );
2146     pkt->pkt.ring_trust->trustval = c;
2147     pkt->pkt.ring_trust->sigcache = 0;
2148     if (!c && pktlen==1) {
2149     c = gpg_iobuf_get_noeof (inp);
2150     pktlen--;
2151     /* we require that bit 7 of the sigcache is 0 (easier eof handling)*/
2152     if ( !(c & 0x80) )
2153     pkt->pkt.ring_trust->sigcache = c;
2154     }
2155     if( list_mode )
2156     printf(":trust packet: flag=%02x sigcache=%02x\n",
2157     pkt->pkt.ring_trust->trustval,
2158     pkt->pkt.ring_trust->sigcache);
2159     }
2160     else {
2161     if( list_mode )
2162     printf(":trust packet: empty\n");
2163     }
2164     skip_rest (inp, pktlen);
2165     }
2166    
2167    
2168     static int
2169     parse_plaintext( gpg_iobuf_t inp, int pkttype, unsigned long pktlen,
2170     PACKET *pkt, int new_ctb )
2171     {
2172     int rc = 0;
2173     int mode, namelen, partial=0;
2174     PKT_plaintext *pt;
2175     byte *p;
2176     int c, i;
2177    
2178     if( pktlen && pktlen < 6 ) {
2179     printf("packet(%d) too short (%lu)\n", pkttype, (ulong)pktlen);
2180     rc = G10ERR_INVALID_PACKET;
2181     goto leave;
2182     }
2183     /* A packet length of zero indicates partial body length. A zero
2184     data length isn't a zero length packet due to the header (mode,
2185     name, etc), so this is accurate. */
2186     if(pktlen==0)
2187     partial=1;
2188     mode = gpg_iobuf_get_noeof(inp); if( pktlen ) pktlen--;
2189     namelen = gpg_iobuf_get_noeof(inp); if( pktlen ) pktlen--;
2190     pt = pkt->pkt.plaintext = malloc(sizeof *pkt->pkt.plaintext + namelen -1);
2191     pt->new_ctb = new_ctb;
2192     pt->mode = mode;
2193     pt->namelen = namelen;
2194     pt->is_partial = partial;
2195     if( pktlen ) {
2196     for( i=0; pktlen > 4 && i < namelen; pktlen--, i++ )
2197     pt->name[i] = gpg_iobuf_get_noeof(inp);
2198     }
2199     else {
2200     for( i=0; i < namelen; i++ )
2201     if( (c=gpg_iobuf_get(inp)) == -1 )
2202     break;
2203     else
2204     pt->name[i] = c;
2205     }
2206     pt->timestamp = read_32(inp); if( pktlen) pktlen -= 4;
2207     pt->len = pktlen;
2208     pt->buf = inp;
2209     pktlen = 0;
2210    
2211     if( list_mode ) {
2212     printf(":literal data packet:\n"
2213     "\tmode %c, created %lu, name=\"",
2214     mode >= ' ' && mode <'z'? mode : '?',
2215     (ulong)pt->timestamp );
2216     for(p=pt->name,i=0; i < namelen; p++, i++ ) {
2217     if( *p >= ' ' && *p <= 'z' )
2218     putchar(*p);
2219     else
2220     printf("\\x%02x", *p );
2221     }
2222     printf("\",\n\traw data: %lu bytes\n", (ulong)pt->len );
2223     }
2224    
2225     leave:
2226     return rc;
2227     }
2228    
2229    
2230     static int
2231     parse_compressed( gpg_iobuf_t inp, int pkttype, unsigned long pktlen,
2232     PACKET *pkt, int new_ctb )
2233     {
2234     PKT_compressed *zd;
2235    
2236     /* pktlen is here 0, but data follows
2237     * (this should be the last object in a file or
2238     * the compress algorithm should know the length)
2239     */
2240     zd = pkt->pkt.compressed = malloc(sizeof *pkt->pkt.compressed );
2241     zd->algorithm = gpg_iobuf_get_noeof(inp);
2242     zd->len = 0; /* not used */
2243     zd->new_ctb = new_ctb;
2244     zd->buf = inp;
2245     if( list_mode )
2246     printf(":compressed packet: algo=%d\n", zd->algorithm);
2247     return 0;
2248     }
2249    
2250    
2251     static int
2252     parse_encrypted( gpg_iobuf_t inp, int pkttype, unsigned long pktlen,
2253     PACKET *pkt, int new_ctb )
2254     {
2255     int rc = 0;
2256     PKT_encrypted *ed;
2257     unsigned long orig_pktlen = pktlen;
2258    
2259     ed = pkt->pkt.encrypted = malloc(sizeof *pkt->pkt.encrypted );
2260     ed->len = pktlen;
2261     /* we don't know the extralen which is (cipher_blocksize+2)
2262     because the algorithm ist not specified in this packet.
2263     However, it is only important to know this for some sanity
2264     checks on the packet length - it doesn't matter that we can't
2265     do it */
2266     ed->extralen = 0;
2267     ed->buf = NULL;
2268     ed->new_ctb = new_ctb;
2269     ed->mdc_method = 0;
2270     if( pkttype == PKT_ENCRYPTED_MDC ) {
2271     /* fixme: add some pktlen sanity checks */
2272     int version;
2273    
2274     version = gpg_iobuf_get_noeof(inp);
2275     if (orig_pktlen)
2276     pktlen--;
2277     if( version != 1 ) {
2278     printf("encrypted_mdc packet with unknown version %d\n",
2279     version);
2280     /*skip_rest(inp, pktlen); should we really do this? */
2281     rc = G10ERR_INVALID_PACKET;
2282     goto leave;
2283     }
2284     ed->mdc_method = DIGEST_ALGO_SHA1;
2285     }
2286     if( orig_pktlen && pktlen < 10 ) { /* actually this is blocksize+2 */
2287     printf("packet(%d) too short\n", pkttype);
2288     rc = G10ERR_INVALID_PACKET;
2289     skip_rest(inp, pktlen);
2290     goto leave;
2291     }
2292     if( list_mode ) {
2293     if( orig_pktlen )
2294     printf(":encrypted data packet:\n\tlength: %lu\n", orig_pktlen);
2295     else
2296     printf(":encrypted data packet:\n\tlength: unknown\n");
2297     if( ed->mdc_method )
2298     printf("\tmdc_method: %d\n", ed->mdc_method );
2299     }
2300    
2301     ed->buf = inp;
2302     pktlen = 0;
2303    
2304     leave:
2305     return rc;
2306     }
2307    
2308    
2309     static int
2310     parse_mdc( gpg_iobuf_t inp, int pkttype, unsigned long pktlen,
2311     PACKET *pkt, int new_ctb )
2312     {
2313     int rc = 0;
2314     PKT_mdc *mdc;
2315     byte *p;
2316    
2317     mdc = pkt->pkt.mdc= malloc(sizeof *pkt->pkt.mdc );
2318     if( list_mode )
2319     printf(":mdc packet: length=%lu\n", pktlen);
2320     if( !new_ctb || pktlen != 20 ) {
2321     printf("mdc_packet with invalid encoding\n");
2322     rc = G10ERR_INVALID_PACKET;
2323     goto leave;
2324     }
2325     p = mdc->hash;
2326     for( ; pktlen; pktlen--, p++ )
2327     *p = gpg_iobuf_get_noeof(inp);
2328    
2329     leave:
2330     return rc;
2331     }
2332    
2333    
2334     /*
2335     * This packet is internally generated by PGG (by armor.c) to
2336     * transfer some information to the lower layer. To make sure that
2337     * this packet is really a GPG faked one and not one comming from outside,
2338     * we first check that tehre is a unique tag in it.
2339     * The format of such a control packet is:
2340     * n byte session marker
2341     * 1 byte control type CTRLPKT_xxxxx
2342     * m byte control data
2343     */
2344    
2345     /* Return a string which is used as a kind of process ID */
2346     const byte *
2347     get_session_marker( size_t *rlen )
2348     {
2349     static byte marker[SIZEOF_UNSIGNED_LONG*2];
2350     static int initialized;
2351    
2352     if ( !initialized ) {
2353     volatile ulong aa, bb; /* we really want the uninitialized value */
2354     ulong a, b;
2355    
2356     initialized = 1;
2357     /* also this marker is guessable it is not easy to use this
2358     * for a faked control packet because an attacker does not
2359     * have enough control about the time the verification does
2360     * take place. Of course, we can add just more random but
2361     * than we need the random generator even for verification
2362     * tasks - which does not make sense. */
2363     a = aa ^ (ulong)getpid();
2364     b = bb ^ (ulong)time(NULL);
2365     memcpy( marker, &a, SIZEOF_UNSIGNED_LONG );
2366     memcpy( marker+SIZEOF_UNSIGNED_LONG, &b, SIZEOF_UNSIGNED_LONG );
2367     }
2368     *rlen = sizeof(marker);
2369     return marker;
2370     }
2371    
2372     static int
2373     parse_gpg_control( gpg_iobuf_t inp,
2374     int pkttype, unsigned long pktlen, PACKET *packet )
2375     {
2376     byte *p;
2377     const byte *sesmark;
2378     size_t sesmarklen;
2379     int i;
2380    
2381     if ( list_mode )
2382     printf(":packet 63: length %lu ", pktlen);
2383    
2384     sesmark = get_session_marker ( &sesmarklen );
2385     if ( pktlen < sesmarklen+1 ) /* 1 is for the control bytes */
2386     goto skipit;
2387     for( i=0; i < sesmarklen; i++, pktlen-- ) {
2388     if ( sesmark[i] != gpg_iobuf_get_noeof(inp) )
2389     goto skipit;
2390     }
2391     if ( list_mode )
2392     puts ("- gpg control packet");
2393    
2394     packet->pkt.gpg_control = malloc(sizeof *packet->pkt.gpg_control
2395     + pktlen - 1);
2396     packet->pkt.gpg_control->control = gpg_iobuf_get_noeof(inp); pktlen--;
2397     packet->pkt.gpg_control->datalen = pktlen;
2398     p = packet->pkt.gpg_control->data;
2399     for( ; pktlen; pktlen--, p++ )
2400     *p = gpg_iobuf_get_noeof(inp);
2401    
2402     return 0;
2403    
2404     skipit:
2405     if ( list_mode ) {
2406     int c;
2407    
2408     i=0;
2409     printf("- private (rest length %lu)\n", pktlen);
2410     if( gpg_iobuf_in_block_mode(inp) ) {
2411     while( (c=gpg_iobuf_get(inp)) != -1 )
2412     dump_hex_line(c, &i);
2413     }
2414     else {
2415     for( ; pktlen; pktlen-- )
2416     dump_hex_line(gpg_iobuf_get(inp), &i);
2417     }
2418     putchar('\n');
2419     }
2420     skip_rest(inp,pktlen);
2421     return G10ERR_INVALID_PACKET;
2422     }
2423    
2424    
2425     u32
2426     gpg_keyid_from_pk( PKT_public_key * pk, byte *fprint )
2427     {
2428     gpg_md_t md;
2429     int npkey, pktlen, i;
2430     const byte *mdbuf;
2431    
2432     if( pk->version == 3 && pk->pubkey_algo == PUBKEY_ALGO_RSA )
2433     return pk->pkey[0]->d[0];
2434     else {
2435     md = gpg_md_open( DIGEST_ALGO_SHA1 );
2436     gpg_md_putc( md, 0x99 );
2437     npkey = pubkey_get_npkey( pk->pubkey_algo );
2438     pktlen = 6;
2439     for( i = 0 ; i <npkey; i++ )
2440     pktlen = pktlen + 2 + pk->pkey[i]->alloced;
2441     gpg_md_putc( md, pktlen>>8 );
2442     gpg_md_putc( md, pktlen );
2443     gpg_md_putc( md, 4 );
2444     gpg_md_putc( md, pk->timestamp >> 24 );
2445     gpg_md_putc( md, pk->timestamp >> 16 );
2446     gpg_md_putc( md, pk->timestamp >> 8 );
2447     gpg_md_putc( md, pk->timestamp );
2448     gpg_md_putc( md, pk->pubkey_algo );
2449     for( i=0; i <npkey; i++ ) {
2450     const u32 * d = pk->pkey[i]->d;
2451     int nbits = pk->pkey[i]->nbits,
2452     n = pk->pkey[i]->alloced;
2453     gpg_md_putc( md, nbits >> 8 );
2454     gpg_md_putc( md, nbits );
2455     n = n>4? n/4 : n;
2456     while( n-- ) {
2457     if( pk->pkey[i]->alloced > 3 )
2458     gpg_md_putc( md, d[n] >> 24 );
2459     if( pk->pkey[i]->alloced > 2 )
2460     gpg_md_putc( md, d[n] >> 16 );
2461     if( pk->pkey[i]->alloced > 1 )
2462     gpg_md_putc( md, d[n] >> 8 );
2463     if( pk->pkey[i]->alloced > 0 )
2464     gpg_md_putc( md, d[n] );
2465     }
2466     }
2467     gpg_md_final( md );
2468     mdbuf = gpg_md_read( md );
2469     }
2470     if( mdbuf && fprint )
2471     memcpy( fprint, mdbuf, 20 );
2472     return mdbuf? mdbuf[16] << 24 | mdbuf[17] << 16 | mdbuf[18] << 8 | mdbuf[19] : 0;
2473     }
2474    
2475    
2476     u32
2477 twoaday 313 gpg_keyid_from_sk (PKT_secret_key * sk, byte *fprint)
2478 werner 46 {
2479     PKT_public_key pk;
2480     int npkey = pubkey_get_npkey( sk->pubkey_algo );
2481     int i;
2482    
2483     pk.pubkey_algo = sk->pubkey_algo;
2484     pk.version = sk->version;
2485     pk.timestamp = sk->timestamp;
2486     pk.expiredate = sk->expiredate;
2487     pk.pubkey_algo = sk->pubkey_algo;
2488     for( i=0; i < npkey; i++ )
2489     pk.pkey[i] = sk->skey[i];
2490     return gpg_keyid_from_pk( &pk, fprint );
2491     }
2492    
2493    
2494     /****************
2495     * Read the next keyblock from stream A.
2496     * PENDING_PKT should be initialzed to NULL
2497     * and not chnaged form the caller.
2498     * Retunr: 0 = okay, -1 no more blocks or another errorcode.
2499     */
2500     int
2501     gpg_read_keyblock( gpg_iobuf_t a, PACKET **pending_pkt, gpg_kbnode_t *ret_root )
2502     {
2503     int rc;
2504     PACKET *pkt;
2505     gpg_kbnode_t root = NULL;
2506     int in_cert;
2507    
2508     if( *pending_pkt ) {
2509     root = gpg_new_kbnode( *pending_pkt );
2510     *pending_pkt = NULL;
2511     in_cert = 1;
2512     }
2513     else
2514     in_cert = 0;
2515     pkt = malloc( sizeof *pkt );
2516     gpg_init_packet(pkt);
2517     while( (rc=gpg_parse_packet(a, pkt)) != -1 ) {
2518     if( rc ) { /* ignore errors */
2519     if( rc != G10ERR_UNKNOWN_PACKET ) {
2520     printf("read_block: read error: %d\n", rc );
2521     rc = G10ERR_INV_KEYRING;
2522     goto ready;
2523     }
2524     gpg_free_packet( pkt );
2525     gpg_init_packet(pkt);
2526     continue;
2527     }
2528    
2529     if( !root && pkt->pkttype == PKT_SIGNATURE
2530     && pkt->pkt.signature->sig_class == 0x20 ) {
2531     /* this is a revocation certificate which is handled
2532     * in a special way */
2533     root = gpg_new_kbnode( pkt );
2534     pkt = NULL;
2535     goto ready;
2536     }
2537    
2538     /* make a linked list of all packets */
2539     switch( pkt->pkttype ) {
2540     case PKT_RING_TRUST:
2541     /* skip those packets */
2542     gpg_free_packet( pkt );
2543     gpg_init_packet(pkt);
2544     break;
2545    
2546     case PKT_PUBLIC_KEY:
2547     case PKT_SECRET_KEY:
2548     if( in_cert ) { /* store this packet */
2549     *pending_pkt = pkt;
2550     pkt = NULL;
2551     goto ready;
2552     }
2553     in_cert = 1;
2554     default:
2555     if( in_cert ) {
2556     if( !root )
2557     root = gpg_new_kbnode( pkt );
2558     else
2559     gpg_add_kbnode( root, gpg_new_kbnode( pkt ) );
2560     pkt = malloc( sizeof *pkt );
2561     }
2562     gpg_init_packet(pkt);
2563     break;
2564     }
2565     }
2566     ready:
2567     if( rc == -1 && root )
2568     rc = 0;
2569    
2570     if( rc )
2571     gpg_release_kbnode( root );
2572     else
2573     *ret_root = root;
2574     gpg_free_packet( pkt );
2575     free( pkt );
2576     return rc;
2577     }

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26