root/openpgpsdk/trunk/src/packet-parse.c

Revision 410 (checked in by rachel, 7 years ago)

Various changes to allow codebase to compile:
- some char* casts to unsigned in function calls
- use #ifndef OPENSSL_NO_IDEA to hide IDEA-only code
- Remove create_signed_key from Examples/Makefile because of missing function
Refs #41

  • Property svn:keywords set to Id
Line 
1 /** \file
2  * \brief Parser for OpenPGP packets
3  */
4
5 #include <openpgpsdk/packet.h>
6 #include <openpgpsdk/packet-parse.h>
7 #include <openpgpsdk/util.h>
8 #include <openpgpsdk/compress.h>
9 #include <openpgpsdk/errors.h>
10 #include "parse_local.h"
11
12 #include <assert.h>
13 #include <stdarg.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <unistd.h>
17 #include <errno.h>
18 #include <limits.h>
19
20 #include <openpgpsdk/final.h>
21
22 /**
23  * limited_read_data reads the specified amount of the subregion's data
24  * into a data_t structure
25  *
26  * \param data  Empty structure which will be filled with data
27  * \param len   Number of octets to read
28  * \param subregion
29  * \param pinfo How to parse
30  *
31  * \return 1 on success, 0 on failure
32  */
33 static int limited_read_data(ops_data_t *data,unsigned int len,
34                              ops_region_t *subregion,ops_parse_info_t *pinfo)
35     {
36     data->len = len;
37
38     assert(subregion->length-subregion->length_read >= len);
39
40     data->contents=malloc(data->len);
41     if (!data->contents)
42         return 0;
43
44     if (!ops_limited_read(data->contents, data->len,subregion,&pinfo->errors,
45                           &pinfo->rinfo,&pinfo->cbinfo))
46         return 0;
47    
48     return 1;
49     }
50
51 /**
52  * read_data reads the remainder of the subregion's data
53  * into a data_t structure
54  *
55  * \param data
56  * \param subregion
57  * \param pinfo
58  *
59  * \return 1 on success, 0 on failure
60  */
61 static int read_data(ops_data_t *data,ops_region_t *subregion,
62                      ops_parse_info_t *pinfo)
63     {
64     int len;
65
66     len=subregion->length-subregion->length_read;
67
68     return(limited_read_data(data,len,subregion,pinfo));
69     }
70
71 /**
72  * Reads the remainder of the subregion as a string.
73  * It is the user's responsibility to free the memory allocated here.
74  */
75
76 static int read_unsigned_string(unsigned char **str,ops_region_t *subregion,
77                                 ops_parse_info_t *pinfo)
78     {
79     int len=0;
80
81     len=subregion->length-subregion->length_read;
82
83     *str=malloc(len+1);
84     if(!(*str))
85         return 0;
86
87     if(len && !ops_limited_read(*str,len,subregion,&pinfo->errors,
88                                 &pinfo->rinfo,&pinfo->cbinfo))
89         return 0;
90
91     /*! ensure the string is NULL-terminated */
92
93     (*str)[len]=(char) NULL;
94
95     return 1;
96     }
97
98 static int read_string(char **str, ops_region_t *subregion, ops_parse_info_t *pinfo)
99     {
100     return (read_unsigned_string((unsigned char **)str, subregion, pinfo));
101     }
102
103 void ops_init_subregion(ops_region_t *subregion,ops_region_t *region)
104     {
105     memset(subregion,'\0',sizeof *subregion);
106     subregion->parent=region;
107     }
108
109 /*! \todo descr for CB macro */
110 /*! \todo check other callback functions to check they match this usage */
111 #define CB(cbinfo,t,pc) do { (pc)->tag=(t); if((cbinfo)->cb(pc,(cbinfo)) == OPS_RELEASE_MEMORY) ops_parser_content_free(pc); } while(0)
112 #define CBP(info,t,pc) CB(&(info)->cbinfo,t,pc)
113 /*! macro to save typing */
114 #define C               content.content
115 /*! set error code in content and run CallBack to handle error */
116 #define ERRCODE(cbinfo,err)     do { C.errcode.errcode=err; CB(cbinfo,OPS_PARSER_ERRCODE,&content); } while(0)
117 #define ERRCODEP(pinfo,err)     do { C.errcode.errcode=err; CBP(pinfo,OPS_PARSER_ERRCODE,&content); } while(0)
118 /*! set error text in content and run CallBack to handle error, then return */
119 #define ERR(cbinfo,err) do { C.error.error=err; CB(cbinfo,OPS_PARSER_ERROR,&content); return ops_false; } while(0)
120 #define ERRP(info,err)  do { C.error.error=err; CBP(info,OPS_PARSER_ERROR,&content); return ops_false; } while(0)
121 /*! set error text in content and run CallBack to handle warning, do not return */
122 #define WARN(warn)      do { C.error.error=warn; CB(OPS_PARSER_ERROR,&content);; } while(0)
123 #define WARNP(info,warn)        do { C.error.error=warn; CBP(info,OPS_PARSER_ERROR,&content); } while(0)
124 /*! \todo descr ERR1 macro */
125 #define ERR1P(info,fmt,x)       do { format_error(&content,(fmt),(x)); CBP(info,OPS_PARSER_ERROR,&content); return ops_false; } while(0)
126 #define ERR2P(info,fmt,x,y)     do { format_error(&content,(fmt),(x),(y)); CBP(info,OPS_PARSER_ERROR,&content); return ops_false; } while(0)
127 #define ERR4P(info,fmt,x,y,z,a) do { format_error(&content,(fmt),(x),(y),(z),(a)); CBP(info,OPS_PARSER_ERROR,&content); return ops_false; } while(0)
128
129 /* XXX: replace ops_ptag_t with something more appropriate for limiting
130    reads */
131
132 /* Note that this makes the parser non-reentrant, in a limited way */
133 /* It is the caller's responsibility to avoid overflow in the buffer */
134 static void format_error(ops_parser_content_t *content,
135                          const char *const fmt,...)
136     {
137     va_list va;
138     static char buf[8192];
139
140     va_start(va,fmt);
141     vsnprintf(buf,sizeof buf,fmt,va);
142     va_end(va);
143     content->content.error.error=buf;
144     }
145
146 /**
147  * low-level function to read data from reader function
148  *
149  * Use this function, rather than calling the reader directly.
150  *
151  * If the accumulate flag is set in *pinfo, the function
152  * adds the read data to the accumulated data, and updates
153  * the accumulated length. This is useful if, for example,
154  * the application wants access to the raw data as well as the
155  * parsed data.
156  *
157  * This function will also try to read the entire amount asked for, but not
158  * if it is over INT_MAX. Obviously many callers will know that they
159  * never ask for that much and so can avoid the extra complexity of
160  * dealing with return codes and filled-in lengths.
161  *
162  * \param *dest
163  * \param *plength
164  * \param flags
165  * \param *pinfo
166  *
167  * \return OPS_R_OK
168  * \return OPS_R_PARTIAL_READ
169  * \return OPS_R_EOF
170  * \return OPS_R_EARLY_EOF
171  *
172  * \sa #ops_reader_ret_t for details of return codes
173  */
174
175 static int sub_base_read(void *dest,size_t length,ops_error_t **errors,
176                          ops_reader_info_t *rinfo,ops_parse_cb_info_t *cbinfo)
177     {
178     size_t n;
179
180     /* reading more than this would look like an error */
181     if(length > INT_MAX)
182         length=INT_MAX;
183
184     for(n=0 ; n < length ; )
185         {
186         int r=rinfo->reader(dest+n,length-n,errors,rinfo,cbinfo);
187
188         assert(r <= (int)(length-n));
189
190         // XXX: should we save the error and return what was read so far?
191         if(r < 0)
192             return r;
193
194         if(r == 0)
195             break;
196
197         n+=r;
198         }
199
200     if(n == 0)
201         return 0;
202
203     if(rinfo->accumulate)
204         {
205         assert(rinfo->asize >= rinfo->alength);
206         if(rinfo->alength+n > rinfo->asize)
207             {
208             rinfo->asize=rinfo->asize*2+n;
209             rinfo->accumulated=realloc(rinfo->accumulated,rinfo->asize);
210             }
211         assert(rinfo->asize >= rinfo->alength+n);
212         memcpy(rinfo->accumulated+rinfo->alength,dest,n);
213         }
214     // we track length anyway, because it is used for packet offsets
215     rinfo->alength+=n;
216     // and also the position
217     rinfo->position+=n;
218
219     return n;
220     }
221
222 int ops_stacked_read(void *dest,size_t length,ops_error_t **errors,
223                      ops_reader_info_t *rinfo,ops_parse_cb_info_t *cbinfo)
224     { return sub_base_read(dest,length,errors,rinfo->next,cbinfo); }
225
226 /* This will do a full read so long as length < MAX_INT */
227 static int base_read(unsigned char *dest,size_t length,
228                      ops_parse_info_t *pinfo)
229     {
230     return sub_base_read(dest,length,&pinfo->errors,&pinfo->rinfo,
231                          &pinfo->cbinfo);
232     }
233
234 /* Read a full size_t's worth. If the return is < than length, then
235  * *last_read tells you why - < 0 for an error, == 0 for EOF */
236
237 static size_t full_read(unsigned char *dest,size_t length,int *last_read,
238                         ops_error_t **errors,ops_reader_info_t *rinfo,
239                         ops_parse_cb_info_t *cbinfo)
240     {
241     size_t t;
242     int r=0; /* preset in case some loon calls with length == 0 */
243
244     for(t=0 ; t < length ; )
245         {
246         r=sub_base_read(dest+t,length-t,errors,rinfo,cbinfo);
247
248         if(r <= 0)
249             {
250             *last_read=r;
251             return t;
252             }
253
254         t+=r;
255         }
256
257     *last_read=r;
258
259     return t;
260     }
261        
262        
263
264 /** Read a scalar value of selected length from reader.
265  *
266  * Read an unsigned scalar value from reader in Big Endian representation.
267  *
268  * This function does not know or care about packet boundaries. It
269  * also assumes that an EOF is an error.
270  *
271  * \param *result       The scalar value is stored here
272  * \param *reader       Our reader
273  * \param length        How many bytes to read
274  * \return              ops_true on success, ops_false on failure
275  */
276 static ops_boolean_t _read_scalar(unsigned *result,unsigned length,
277                                     ops_parse_info_t *pinfo)
278     {
279     unsigned t=0;
280
281     assert (length <= sizeof(*result));
282
283     while(length--)
284         {
285         unsigned char c[1];
286         int r;
287
288         r=base_read(c,1,pinfo);
289         if(r != 1)
290             return ops_false;
291         t=(t << 8)+c[0];
292         }
293
294     *result=t;
295     return ops_true;
296     }
297
298 /** Read bytes from a region within the packet.
299  *
300  * Read length bytes into the buffer pointed to by *dest.  Make sure
301  * we do not read over the packet boundary.  Updates the Packet Tag's
302  * ops_ptag_t::length_read.
303  *
304  * If length would make us read over the packet boundary, or if
305  * reading fails, we call the callback with an error.
306  *
307  * Note that if the region is indeterminate, this can return a short
308  * read - check region->last_read for the length. EOF is indicated by
309  * a success return and region->last_read == 0 in this case (for a
310  * region of known length, EOF is an error).
311  *
312  * This function makes sure to respect packet boundaries.
313  *
314  * \param *dest         The destination buffer
315  * \param length        How many bytes to read
316  * \param *region       Pointer to packet region
317  * \param *pinfo        How to parse, including callback function
318  * \return              ops_true on success, ops_false on error
319  */
320 ops_boolean_t ops_limited_read(unsigned char *dest,size_t length,
321                                ops_region_t *region,ops_error_t **errors,
322                                ops_reader_info_t *rinfo,
323                                ops_parse_cb_info_t *cbinfo)
324     {
325     ops_parser_content_t content;
326     size_t r;
327     int lr;
328
329     if(!region->indeterminate && region->length_read+length > region->length)
330         {
331         ERRCODE(cbinfo,OPS_E_P_NOT_ENOUGH_DATA);
332         return 0;
333         }
334
335     r=full_read(dest,length,&lr,errors,rinfo,cbinfo);
336
337     if(lr < 0)
338         {
339         ERRCODE(cbinfo,OPS_E_R_READ_FAILED);
340         return ops_false;
341         }
342
343     if(!region->indeterminate && r != length)
344         {
345         ERRCODE(cbinfo,OPS_E_R_READ_FAILED);
346         return ops_false;
347         }
348
349     region->last_read=r;
350     do
351         {
352         region->length_read+=r;
353         assert(!region->parent || region->length <= region->parent->length);
354         }
355     while((region=region->parent));
356
357     return ops_true;
358     }
359
360 ops_boolean_t ops_stacked_limited_read(unsigned char *dest,unsigned length,
361                                        ops_region_t *region,
362                                        ops_error_t **errors,
363                                        ops_reader_info_t *rinfo,
364                                        ops_parse_cb_info_t *cbinfo)
365     { return ops_limited_read(dest,length,region,errors,rinfo->next,cbinfo); }
366
367 static ops_boolean_t limited_read(unsigned char *dest,unsigned length,
368                                   ops_region_t *region,ops_parse_info_t *info)
369     {
370     return ops_limited_read(dest,length,region,&info->errors,
371                             &info->rinfo,&info->cbinfo);
372     }
373
374 static ops_boolean_t exact_limited_read(unsigned char *dest,unsigned length,
375                                         ops_region_t *region,
376                                         ops_parse_info_t *pinfo)
377     {
378     ops_boolean_t ret;
379
380     pinfo->exact_read=ops_true;
381     ret=limited_read(dest,length,region,pinfo);
382     pinfo->exact_read=ops_false;
383
384     return ret;
385     }
386
387 /** Skip over length bytes of this packet.
388  *
389  * Calls limited_read() to skip over some data.
390  *
391  * This function makes sure to respect packet boundaries.
392  *
393  * \param length        How many bytes to skip
394  * \param *region       Pointer to packet region
395  * \param *pinfo        How to parse
396  * \return              1 on success, 0 on error (calls the cb with OPS_PARSER_ERROR in limited_read()).
397  */
398 static int limited_skip(unsigned length,ops_region_t *region,
399                         ops_parse_info_t *pinfo)
400     {
401     unsigned char buf[8192];
402
403     while(length)
404         {
405         int n=length%8192;
406         if(!limited_read(buf,n,region,pinfo))
407             return 0;
408         length-=n;
409         }
410     return 1;
411     }
412
413 /** Read a scalar.
414  *
415  * Read a big-endian scalar of length bytes, respecting packet
416  * boundaries (by calling limited_read() to read the raw data).
417  *
418  * This function makes sure to respect packet boundaries.
419  *
420  * \param *dest         The scalar value is stored here
421  * \param length        How many bytes make up this scalar (at most 4)
422  * \param *region       Pointer to current packet region
423  * \param *pinfo        How to parse
424  * \param *cb           The callback
425  * \return              1 on success, 0 on error (calls the cb with OPS_PARSER_ERROR in limited_read()).
426  *
427  * \see RFC2440bis-12 3.1
428  */
429 static int limited_read_scalar(unsigned *dest,unsigned length,
430                                ops_region_t *region,
431                                ops_parse_info_t *pinfo)
432     {
433     unsigned char c[4];
434     unsigned t;
435     unsigned n;
436
437     assert(length <= 4);
438     assert(sizeof(*dest) >= 4);
439     if(!limited_read(c,length,region,pinfo))
440         return 0;
441
442     for(t=0,n=0 ; n < length ; ++n)
443         t=(t << 8)+c[n];
444     *dest=t;
445
446     return 1;
447     }
448
449 /** Read a scalar.
450  *
451  * Read a big-endian scalar of length bytes, respecting packet
452  * boundaries (by calling limited_read() to read the raw data).
453  *
454  * The value read is stored in a size_t, which is a different size
455  * from an unsigned on some platforms.
456  *
457  * This function makes sure to respect packet boundaries.
458  *
459  * \param *dest         The scalar value is stored here
460  * \param length        How many bytes make up this scalar (at most 4)
461  * \param *region       Pointer to current packet region
462  * \param *pinfo        How to parse
463  * \param *cb           The callback
464  * \return              1 on success, 0 on error (calls the cb with OPS_PARSER_ERROR in limited_read()).
465  *
466  * \see RFC2440bis-12 3.1
467  */
468 static int limited_read_size_t_scalar(size_t *dest,unsigned length,
469                                       ops_region_t *region,
470                                       ops_parse_info_t *pinfo)
471     {
472     unsigned tmp;
473
474     assert(sizeof(*dest) >= 4);
475
476     /* Note that because the scalar is at most 4 bytes, we don't care
477        if size_t is bigger than usigned */
478     if(!limited_read_scalar(&tmp,length,region,pinfo))
479         return 0;
480
481     *dest=tmp;
482     return 1;
483     }
484
485 /** Read a timestamp.
486  *
487  * Timestamps in OpenPGP are unix time, i.e. seconds since The Epoch (1.1.1970).  They are stored in an unsigned scalar
488  * of 4 bytes.
489  *
490  * This function reads the timestamp using limited_read_scalar().
491  *
492  * This function makes sure to respect packet boundaries.
493  *
494  * \param *dest         The timestamp is stored here
495  * \param *ptag         Pointer to current packet's Packet Tag.
496  * \param *reader       Our reader
497  * \param *cb           The callback
498  * \return              see limited_read_scalar()
499  *
500  * \see RFC2440bis-12 3.5
501  */
502 static int limited_read_time(time_t *dest,ops_region_t *region,
503                              ops_parse_info_t *pinfo)
504     {
505     return limited_read_scalar((unsigned *)dest,4,region,pinfo);
506     }
507
508 /** Read a multiprecision integer.
509  *
510  * Large numbers (multiprecision integers, MPI) are stored in OpenPGP in two parts.  First there is a 2 byte scalar
511  * indicating the length of the following MPI in Bits.  Then follow the bits that make up the actual number, most
512  * significant bits first (Big Endian).  The most significant bit in the MPI is supposed to be 1 (unless the MPI is
513  * encrypted - then it may be different as the bit count refers to the plain text but the bits are encrypted).
514  *
515  * Unused bits (i.e. those filling up the most significant byte from the left to the first bits that counts) are
516  * supposed to be cleared - I guess. XXX - does anything actually say so?
517  *
518  * This function makes sure to respect packet boundaries.
519  *
520  * \param **pgn         return the integer there - the BIGNUM is created by BN_bin2bn() and probably needs to be freed
521  *                              by the caller XXX right ben?
522  * \param *ptag         Pointer to current packet's Packet Tag.
523  * \param *reader       Our reader
524  * \param *cb           The callback
525  * \return              1 on success, 0 on error (by limited_read_scalar() or limited_read() or if the MPI is not properly formed (XXX
526  *                               see comment below - the callback is called with a OPS_PARSER_ERROR in case of an error)
527  *
528  * \see RFC2440bis-12 3.2
529  */
530 static int limited_read_mpi(BIGNUM **pbn,ops_region_t *region,
531                             ops_parse_info_t *pinfo)
532     {
533     unsigned length;
534     unsigned nonzero;
535     unsigned char buf[8192]; /* an MPI has a 2 byte length part.  Length
536                                 is given in bits, so the largest we should
537                                 ever need for the buffer is 8192 bytes. */
538     ops_parser_content_t content;
539     ops_boolean_t ret;
540
541     pinfo->reading_mpi_length=ops_true;
542     ret=limited_read_scalar(&length,2,region,pinfo);
543     pinfo->reading_mpi_length=ops_false;
544     if(!ret)
545         return 0;
546
547     nonzero=length&7; /* there should be this many zero bits in the MS byte */
548     if(!nonzero)
549         nonzero=8;
550     length=(length+7)/8;
551
552     assert(length <= 8192);
553     if(!limited_read(buf,length,region,pinfo))
554         return 0;
555
556     if((buf[0] >> nonzero) != 0 || !(buf[0]&(1 << (nonzero-1))))
557         {
558         ERRCODEP(pinfo,OPS_E_P_MPI_FORMAT_ERROR);  /* XXX: Ben, one part of this constraint does not apply to encrypted MPIs the draft says. -- peter */
559         return 0;
560         }
561
562     *pbn=BN_bin2bn(buf,length,NULL);
563     return 1;
564     }
565
566 /** Read some data with a New-Format length from reader.
567  *
568  * \sa Internet-Draft RFC2440bis-13.txt Section 4.2.2
569  *
570  * \param *length       Where the decoded length will be put
571  * \param *pinfo        How to parse
572  * \return              ops_true if OK, else ops_false
573  *
574  */
575
576 static ops_boolean_t read_new_length(unsigned *length,ops_parse_info_t *pinfo)
577     {
578     unsigned char c[1];
579
580     if(base_read(c,1,pinfo) != 1)
581         return ops_false;
582     if(c[0] < 192)
583         {
584         *length=c[0];
585         return ops_true;
586         }
587     if(c[0] < 255)
588         {
589         unsigned t=(c[0]-192) << 8;
590
591         if(base_read(c,1,pinfo) != 1)
592             return ops_false;
593         *length=t+c[0]+192;
594         return ops_true;
595         }
596     return _read_scalar(length,4,pinfo);
597     }
598
599 /** Read the length information for a new format Packet Tag.
600  *
601  * New style Packet Tags encode the length in one to five octets.  This function reads the right amount of bytes and
602  * decodes it to the proper length information.
603  *
604  * This function makes sure to respect packet boundaries.
605  *
606  * \param *length       return the length here
607  * \param *ptag         Pointer to current packet's Packet Tag.
608  * \param *reader       Our reader
609  * \param *cb           The callback
610  * \return              1 on success, 0 on error (by limited_read_scalar() or limited_read() or if the MPI is not properly formed (XXX
611  *                               see comment below)
612  *
613  * \see RFC2440bis-12 4.2.2
614  * \see ops_ptag_t
615  */
616 static int limited_read_new_length(unsigned *length,ops_region_t *region,
617                                    ops_parse_info_t *pinfo)
618     {
619     unsigned char c[1];
620
621     if(!limited_read(c,1,region,pinfo))
622         return 0;
623     if(c[0] < 192)
624         {
625         *length=c[0];
626         return 1;
627         }
628     if(c[0] < 255)
629         {
630         unsigned t=(c[0]-192) << 8;
631
632         if(!limited_read(c,1,region,pinfo))
633             return 0;
634         *length=t+c[0]+192;
635         return 1;
636         }
637     return limited_read_scalar(length,4,region,pinfo);
638     }
639
640 static void data_free(ops_data_t *data)
641     {
642     free(data->contents);
643     data->contents=NULL;
644     data->len=0;
645     }
646
647 static void string_free(char **str)
648     {
649     free(*str);
650     *str=NULL;
651     }
652
653 /*! Free packet memory, set pointer to NULL */
654 void ops_packet_free(ops_packet_t *packet)
655     {
656     free(packet->raw);
657     packet->raw=NULL;
658     }
659
660 void ops_headers_free(ops_headers_t *headers)
661     {
662     unsigned n;
663
664     for(n=0 ; n < headers->nheaders ; ++n)
665         {
666         free(headers->headers[n].key);
667         free(headers->headers[n].value);
668         }
669     free(headers->headers);
670     headers->headers=NULL;
671     }
672
673 void ops_signed_cleartext_trailer_free(ops_signed_cleartext_trailer_t *trailer)
674     {
675     free(trailer->hash);
676     trailer->hash=NULL;
677     }
678
679 void ops_cmd_get_passphrase_free(ops_secret_key_passphrase_t *skp)
680     {
681     free(skp->passphrase);
682     skp->passphrase=NULL;
683     }
684
685 /*! Free any memory allocated when parsing the packet content */
686 void ops_parser_content_free(ops_parser_content_t *c)
687     {
688     switch(c->tag)
689         {
690     case OPS_PARSER_PTAG:
691     case OPS_PTAG_CT_COMPRESSED:
692     case OPS_PTAG_SS_CREATION_TIME:
693     case OPS_PTAG_SS_EXPIRATION_TIME:
694     case OPS_PTAG_SS_KEY_EXPIRATION_TIME:
695     case OPS_PTAG_SS_TRUST:
696     case OPS_PTAG_SS_ISSUER_KEY_ID:
697     case OPS_PTAG_CT_ONE_PASS_SIGNATURE:
698     case OPS_PTAG_SS_PRIMARY_USER_ID:
699     case OPS_PTAG_SS_REVOCABLE:
700     case OPS_PTAG_SS_REVOCATION_KEY:
701     case OPS_PTAG_CT_LITERAL_DATA_HEADER:
702     case OPS_PTAG_CT_LITERAL_DATA_BODY:
703     case OPS_PTAG_CT_SIGNED_CLEARTEXT_BODY:
704     case OPS_PTAG_CT_UNARMOURED_TEXT:
705     case OPS_PTAG_CT_ARMOUR_TRAILER:
706     case OPS_PTAG_CT_SIGNATURE_HEADER:
707     case OPS_PTAG_CT_SE_DATA_HEADER:
708     case OPS_PTAG_CT_SE_IP_DATA_HEADER:
709     case OPS_PTAG_CT_SE_IP_DATA_BODY:
710     case OPS_PARSER_CMD_GET_SECRET_KEY:
711         break;
712
713     case OPS_PTAG_CT_SIGNED_CLEARTEXT_HEADER:
714         ops_headers_free(&c->content.signed_cleartext_header.headers);
715         break;
716
717     case OPS_PTAG_CT_ARMOUR_HEADER:
718         ops_headers_free(&c->content.armour_header.headers);
719         break;
720
721     case OPS_PTAG_CT_SIGNED_CLEARTEXT_TRAILER:
722         ops_signed_cleartext_trailer_free(&c->content.signed_cleartext_trailer);
723         break;
724
725     case OPS_PTAG_CT_TRUST:
726         ops_trust_free(&c->content.trust);
727         break;
728
729     case OPS_PTAG_CT_SIGNATURE:
730     case OPS_PTAG_CT_SIGNATURE_FOOTER:
731         ops_signature_free(&c->content.signature);
732         break;
733
734     case OPS_PTAG_CT_PUBLIC_KEY:
735     case OPS_PTAG_CT_PUBLIC_SUBKEY:
736         ops_public_key_free(&c->content.public_key);
737         break;
738
739     case OPS_PTAG_CT_USER_ID:
740         ops_user_id_free(&c->content.user_id);
741         break;
742
743     case OPS_PTAG_SS_SIGNERS_USER_ID:
744         ops_user_id_free(&c->content.ss_signers_user_id);
745         break;
746
747     case OPS_PTAG_CT_USER_ATTRIBUTE:
748         ops_user_attribute_free(&c->content.user_attribute);
749         break;
750
751     case OPS_PTAG_SS_PREFERRED_SKA:
752         ops_ss_preferred_ska_free(&c->content.ss_preferred_ska);
753         break;
754
755     case OPS_PTAG_SS_PREFERRED_HASH:
756         ops_ss_preferred_hash_free(&c->content.ss_preferred_hash);
757         break;
758
759     case OPS_PTAG_SS_PREFERRED_COMPRESSION:
760         ops_ss_preferred_compression_free(&c->content.ss_preferred_compression);
761         break;
762
763     case OPS_PTAG_SS_KEY_FLAGS:
764         ops_ss_key_flags_free(&c->content.ss_key_flags);
765         break;
766
767     case OPS_PTAG_SS_KEY_SERVER_PREFS:
768         ops_ss_key_server_prefs_free(&c->content.ss_key_server_prefs);
769         break;
770
771     case OPS_PTAG_SS_FEATURES:
772         ops_ss_features_free(&c->content.ss_features);
773         break;
774
775     case OPS_PTAG_SS_NOTATION_DATA:
776         ops_ss_notation_data_free(&c->content.ss_notation_data);
777         break;
778
779     case OPS_PTAG_SS_REGEXP:
780         ops_ss_regexp_free(&c->content.ss_regexp);
781         break;
782
783     case OPS_PTAG_SS_POLICY_URL:
784         ops_ss_policy_url_free(&c->content.ss_policy_url);
785         break;
786
787     case OPS_PTAG_SS_PREFERRED_KEY_SERVER:
788         ops_ss_preferred_key_server_free(&c->content.ss_preferred_key_server);
789         break;
790
791     case OPS_PTAG_SS_USERDEFINED00:
792     case OPS_PTAG_SS_USERDEFINED01:
793     case OPS_PTAG_SS_USERDEFINED02:
794     case OPS_PTAG_SS_USERDEFINED03:
795     case OPS_PTAG_SS_USERDEFINED04:
796     case OPS_PTAG_SS_USERDEFINED05:
797     case OPS_PTAG_SS_USERDEFINED06:
798     case OPS_PTAG_SS_USERDEFINED07:
799     case OPS_PTAG_SS_USERDEFINED08:
800     case OPS_PTAG_SS_USERDEFINED09:
801     case OPS_PTAG_SS_USERDEFINED10:
802         ops_ss_userdefined_free(&c->content.ss_userdefined);
803         break;
804
805     case OPS_PTAG_SS_RESERVED:
806         ops_ss_reserved_free(&c->content.ss_unknown);
807         break;
808
809     case OPS_PTAG_SS_REVOCATION_REASON:
810         ops_ss_revocation_reason_free(&c->content.ss_revocation_reason);
811         break;
812
813     case OPS_PARSER_PACKET_END:
814         ops_packet_free(&c->content.packet);
815         break;
816
817     case OPS_PARSER_ERROR:
818     case OPS_PARSER_ERRCODE:
819         break;
820
821     case OPS_PTAG_CT_SECRET_KEY:
822     case OPS_PTAG_CT_ENCRYPTED_SECRET_KEY:
823         ops_secret_key_free(&c->content.secret_key);
824         break;
825
826     case OPS_PTAG_CT_PK_SESSION_KEY:
827     case OPS_PTAG_CT_ENCRYPTED_PK_SESSION_KEY:
828         ops_pk_session_key_free(&c->content.pk_session_key);
829         break;
830
831     case OPS_PARSER_CMD_GET_SK_PASSPHRASE:
832         ops_cmd_get_passphrase_free(&c->content.secret_key_passphrase);
833         break;
834
835     default:
836         fprintf(stderr,"Can't free %d (0x%x)\n",c->tag,c->tag);
837         assert(0);
838         }
839     }
840
841 static void free_BN(BIGNUM **pp)
842     {
843     BN_free(*pp);
844     *pp=NULL;
845     }
846
847 void ops_pk_session_key_free(ops_pk_session_key_t *sk)
848     {
849     switch(sk->algorithm)
850         {
851     case OPS_PKA_RSA:
852         free_BN(&sk->parameters.rsa.encrypted_m);
853         break;
854
855     case OPS_PKA_ELGAMAL:
856         free_BN(&sk->parameters.elgamal.g_to_k);
857         free_BN(&sk->parameters.elgamal.encrypted_m);
858         break;
859
860     default:
861         assert(0);
862         }
863     }
864
865 /*! Free the memory used when parsing a public key */
866 void ops_public_key_free(ops_public_key_t *p)
867     {
868     switch(p->algorithm)
869         {
870     case OPS_PKA_RSA:
871     case OPS_PKA_RSA_ENCRYPT_ONLY:
872     case OPS_PKA_RSA_SIGN_ONLY:
873         free_BN(&p->key.rsa.n);
874         free_BN(&p->key.rsa.e);
875         break;
876
877     case OPS_PKA_DSA:
878         free_BN(&p->key.dsa.p);
879         free_BN(&p->key.dsa.q);
880         free_BN(&p->key.dsa.g);
881         free_BN(&p->key.dsa.y);
882         break;
883
884     case OPS_PKA_ELGAMAL:
885     case OPS_PKA_ELGAMAL_ENCRYPT_OR_SIGN:
886         free_BN(&p->key.elgamal.p);
887         free_BN(&p->key.elgamal.g);
888         free_BN(&p->key.elgamal.y);
889         break;
890
891     default:
892         assert(0);
893         }
894     }
895
896 static int parse_public_key_data(ops_public_key_t *key,ops_region_t *region,
897                                  ops_parse_info_t *pinfo)
898     {
899     ops_parser_content_t content;
900     unsigned char c[1];
901
902     assert (region->length_read == 0);  /* We should not have read anything so far */
903
904     if(!limited_read(c,1,region,pinfo))
905         return 0;
906     key->version=c[0];
907     if(key->version < 2 || key->version > 4)
908         ERR1P(pinfo,"Bad public key version (0x%02x)",key->version);
909
910     if(!limited_read_time(&key->creation_time,region,pinfo))
911         return 0;
912
913     key->days_valid=0;
914     if((key->version == 2 || key->version == 3)
915        && !limited_read_scalar(&key->days_valid,2,region,pinfo))
916         return 0;
917
918     if(!limited_read(c,1,region,pinfo))
919         return 0;
920
921     key->algorithm=c[0];
922
923     switch(key->algorithm)
924         {
925     case OPS_PKA_DSA:
926         if(!limited_read_mpi(&key->key.dsa.p,region,pinfo)
927            || !limited_read_mpi(&key->key.dsa.q,region,pinfo)
928            || !limited_read_mpi(&key->key.dsa.g,region,pinfo)
929            || !limited_read_mpi(&key->key.dsa.y,region,pinfo))
930             return 0;
931         break;
932
933     case OPS_PKA_RSA:
934     case OPS_PKA_RSA_ENCRYPT_ONLY:
935     case OPS_PKA_RSA_SIGN_ONLY:
936         if(!limited_read_mpi(&key->key.rsa.n,region,pinfo)
937            || !limited_read_mpi(&key->key.rsa.e,region,pinfo))
938             return 0;
939         break;
940
941     case OPS_PKA_ELGAMAL:
942     case OPS_PKA_ELGAMAL_ENCRYPT_OR_SIGN:
943         if(!limited_read_mpi(&key->key.elgamal.p,region,pinfo)
944            || !limited_read_mpi(&key->key.elgamal.g,region,pinfo)
945            || !limited_read_mpi(&key->key.elgamal.y,region,pinfo))
946             return 0;
947         break;
948
949     default:
950         ERR1P(pinfo,"Unknown public key algorithm (%d)",key->algorithm);
951         }
952
953     return 1;
954     }
955
956
957 /** Parse a public key packet.
958  *
959  * This function parses an entire v3 (== v2) or v4 public key packet for RSA, ElGamal, and DSA keys.
960  *
961  * Once the key has been parsed successfully, it is passed to the callback.
962  *
963  * \param *ptag         Pointer to the current Packet Tag.  This function should consume the entire packet.
964  * \param *reader       Our reader
965  * \param *cb           The callback
966  * \return              1 on success, 0 on error
967  *
968  * \see RFC2440bis-12 5.5.2
969  */
970 static int parse_public_key(ops_content_tag_t tag,ops_region_t *region,
971                             ops_parse_info_t *pinfo)
972     {
973     ops_parser_content_t content;
974
975     if(!parse_public_key_data(&C.public_key,region,pinfo))
976         return 0;
977
978     // XXX: this test should be done for all packets, surely?
979     if(region->length_read != region->length)
980         ERR1P(pinfo,"Unconsumed data (%d)",
981               region->length-region->length_read);
982
983     CBP(pinfo,tag,&content);
984
985     return 1;
986     }
987
988
989 /*! Free the memory used when parsing this signature sub-packet type */
990 void ops_ss_regexp_free(ops_ss_regexp_t *regexp)
991     {
992     string_free(&regexp->text);
993     }
994
995 /*! Free the memory used when parsing this signature sub-packet type */
996 void ops_ss_policy_url_free(ops_ss_policy_url_t *policy_url)
997     {
998     string_free(&policy_url->text);
999     }
1000
1001 /*! Free the memory used when parsing this signature sub-packet type */
1002 void ops_ss_preferred_key_server_free(ops_ss_preferred_key_server_t *preferred_key_server)
1003     {
1004     string_free(&preferred_key_server->text);
1005     }
1006
1007 /*! Free the memory used when parsing this packet type */
1008 void ops_user_attribute_free(ops_user_attribute_t *user_att)
1009     {
1010     data_free(&user_att->data);
1011     }
1012
1013 /** Parse one user attribute packet.
1014  *
1015  * User attribute packets contain one or more attribute subpackets.
1016  * For now, handle the whole packet as raw data.
1017  */
1018
1019 static int parse_user_attribute(ops_region_t *region, ops_parse_info_t *pinfo)
1020     {
1021
1022     ops_parser_content_t content;
1023
1024     /* xxx- treat as raw data for now. Could break down further
1025        into attribute sub-packets later - rachel */
1026
1027     assert(region->length_read == 0);  /* We should not have read anything so far */
1028
1029     if(!read_data(&C.user_attribute.data,region,pinfo))
1030         return 0;
1031
1032     CBP(pinfo,OPS_PTAG_CT_USER_ATTRIBUTE,&content);
1033
1034     return 1;
1035     }
1036
1037 /*! Free the memory used when parsing this packet type */
1038 void ops_user_id_free(ops_user_id_t *id)
1039     {
1040     free(id->user_id);
1041     id->user_id=NULL;
1042     }
1043
1044 /** Parse a user id.
1045  *
1046  * This function parses an user id packet, which is basically just a char array the size of the packet.
1047  *
1048  * The char array is to be treated as an UTF-8 string.
1049  *
1050  * The userid gets null terminated by this function.  Freeing it is the responsibility of the caller.
1051  *
1052  * Once the userid has been parsed successfully, it is passed to the callback.
1053  *
1054  * \param *ptag         Pointer to the Packet Tag.  This function should consume the entire packet.
1055  * \param *reader       Our reader
1056  * \param *cb           The callback
1057  * \return              1 on success, 0 on error
1058  *
1059  * \see RFC2440bis-12 5.11
1060  */
1061 static int parse_user_id(ops_region_t *region,ops_parse_info_t *pinfo)
1062     {
1063     ops_parser_content_t content;
1064
1065     assert(region->length_read == 0);  /* We should not have read anything so far */
1066
1067     C.user_id.user_id=malloc(region->length+1);  /* XXX should we not like check malloc's return value? */
1068
1069     if(region->length && !limited_read(C.user_id.user_id,region->length,region,
1070                                        pinfo))
1071         return 0;
1072
1073     C.user_id.user_id[region->length]='\0'; /* terminate the string */
1074
1075     CBP(pinfo,OPS_PTAG_CT_USER_ID,&content);
1076
1077     return 1;
1078     }
1079
1080 /**
1081  * \ingroup Memory
1082  *
1083  * Free the memory used when parsing a private/experimental PKA signature
1084  *
1085  * \param unknown_sig
1086  */
1087 void free_unknown_sig_pka(ops_unknown_signature_t *unknown_sig)
1088     {
1089     data_free(&unknown_sig->data);
1090     }
1091
1092 /**
1093  * \ingroup Memory
1094  *
1095  * Free the memory used when parsing a signature
1096  *
1097  * \param sig
1098  */
1099 void ops_signature_free(ops_signature_t *sig)
1100     {
1101     switch(sig->key_algorithm)
1102         {
1103     case OPS_PKA_RSA:
1104     case OPS_PKA_RSA_SIGN_ONLY:
1105         free_BN(&sig->signature.rsa.sig);
1106         break;
1107
1108     case OPS_PKA_DSA:
1109         free_BN(&sig->signature.dsa.r);
1110         free_BN(&sig->signature.dsa.s);
1111         break;
1112
1113     case OPS_PKA_ELGAMAL_ENCRYPT_OR_SIGN:
1114         free_BN(&sig->signature.elgamal.r);
1115         free_BN(&sig->signature.elgamal.s);
1116         break;
1117
1118     case OPS_PKA_PRIVATE00:
1119     case OPS_PKA_PRIVATE01:
1120     case OPS_PKA_PRIVATE02:
1121     case OPS_PKA_PRIVATE03:
1122     case OPS_PKA_PRIVATE04:
1123     case OPS_PKA_PRIVATE05:
1124     case OPS_PKA_PRIVATE06:
1125     case OPS_PKA_PRIVATE07:
1126     case OPS_PKA_PRIVATE08:
1127     case OPS_PKA_PRIVATE09:
1128     case OPS_PKA_PRIVATE10:
1129         free_unknown_sig_pka(&sig->signature.unknown);
1130         break;
1131
1132     default:
1133         assert(0);
1134         }
1135     }
1136
1137 /** Parse a version 3 signature.
1138  *
1139  * This function parses an version 3 signature packet, handling RSA and DSA signatures.
1140  *
1141  * Once the signature has been parsed successfully, it is passed to the callback.
1142  *
1143  * \param *ptag         Pointer to the Packet Tag.  This function should consume the entire packet.
1144  * \param *reader       Our reader
1145  * \param *cb           The callback
1146  * \return              1 on success, 0 on error
1147  *
1148  * \see RFC2440bis-12 5.2.2
1149  */
1150 static int parse_v3_signature(ops_region_t *region,
1151                               ops_parse_info_t *pinfo)
1152     {
1153     unsigned char c[1];
1154     ops_parser_content_t content;
1155
1156     C.signature.version=OPS_V3;
1157
1158     /* hash info length */
1159     if(!limited_read(c,1,region,pinfo))
1160         return 0;
1161     if(c[0] != 5)
1162         ERRP(pinfo,"bad hash info length");
1163
1164     if(!limited_read(c,1,region,pinfo))
1165         return 0;
1166     C.signature.type=c[0];
1167     /* XXX: check signature type */
1168
1169     if(!limited_read_time(&C.signature.creation_time,region,pinfo))
1170         return 0;
1171     C.signature.creation_time_set=ops_true;
1172
1173     if(!limited_read(C.signature.signer_id,OPS_KEY_ID_SIZE,region,pinfo))
1174         return 0;
1175     C.signature.signer_id_set=ops_true;
1176
1177     if(!limited_read(c,1,region,pinfo))
1178         return 0;
1179     C.signature.key_algorithm=c[0];
1180     /* XXX: check algorithm */
1181
1182     if(!limited_read(c,1,region,pinfo))
1183         return 0;
1184     C.signature.hash_algorithm=c[0];
1185     /* XXX: check algorithm */
1186    
1187     if(!limited_read(C.signature.hash2,2,region,pinfo))
1188         return 0;
1189
1190     switch(C.signature.key_algorithm)
1191         {
1192     case OPS_PKA_RSA:
1193     case OPS_PKA_RSA_SIGN_ONLY:
1194         if(!limited_read_mpi(&C.signature.signature.rsa.sig,region,pinfo))
1195             return 0;
1196         break;
1197
1198     case OPS_PKA_DSA:
1199         if(!limited_read_mpi(&C.signature.signature.dsa.r,region,pinfo)
1200            || !limited_read_mpi(&C.signature.signature.dsa.s,region,pinfo))
1201             return 0;
1202         break;
1203
1204     case OPS_PKA_ELGAMAL_ENCRYPT_OR_SIGN:
1205         if(!limited_read_mpi(&C.signature.signature.elgamal.r,region,pinfo)
1206            || !limited_read_mpi(&C.signature.signature.elgamal.s,region,pinfo))
1207             return 0;
1208         break;
1209
1210     default:
1211         ERR1P(pinfo,"Bad signature key algorithm (%d)",C.signature.key_algorithm);
1212         }
1213
1214     if(region->length_read != region->length)
1215         ERR1P(pinfo,"Unconsumed data (%d)",region->length-region->length_read);
1216
1217     if(C.signature.signer_id_set)
1218         C.signature.hash=ops_parse_hash_find(pinfo,C.signature.signer_id);
1219
1220     CBP(pinfo,OPS_PTAG_CT_SIGNATURE,&content);
1221
1222     return 1;
1223     }
1224
1225 /** Parse one signature sub-packet.
1226  *
1227  * Version 4 signatures can have an arbitrary amount of (hashed and unhashed) subpackets.  Subpackets are used to hold
1228  * optional attributes of subpackets.
1229  *
1230  * This function parses one such signature subpacket.
1231  *
1232  * Once the subpacket has been parsed successfully, it is passed to the callback.
1233  *
1234  * \param *ptag         Pointer to the Packet Tag.  This function should consume the entire subpacket.
1235  * \param *reader       Our reader
1236  * \param *cb           The callback
1237  * \return              1 on success, 0 on error
1238  *
1239  * \see RFC2440bis-12 5.2.3
1240  */
1241 static int parse_one_signature_subpacket(ops_signature_t *sig,
1242                                          ops_region_t *region,
1243                                          ops_parse_info_t *pinfo)
1244     {
1245     ops_region_t subregion;
1246     unsigned char c[1];
1247     ops_parser_content_t content;
1248     unsigned t8,t7;
1249     ops_boolean_t read=ops_true;
1250     unsigned char bool[1];
1251
1252     ops_init_subregion(&subregion,region);
1253     if(!limited_read_new_length(&subregion.length,region,pinfo))
1254         return 0;
1255
1256     if(subregion.length > region->length)
1257         ERRP(pinfo,"Subpacket too long");
1258
1259     if(!limited_read(c,1,&subregion,pinfo))
1260         return 0;
1261
1262     t8=(c[0]&0x7f)/8;
1263     t7=1 << (c[0]&7);
1264
1265     content.critical=c[0] >> 7;
1266     content.tag=OPS_PTAG_SIGNATURE_SUBPACKET_BASE+(c[0]&0x7f);
1267
1268     /* Application wants it delivered raw */
1269     if(pinfo->ss_raw[t8]&t7)
1270         {
1271         C.ss_raw.tag=content.tag;
1272         C.ss_raw.length=subregion.length-1;
1273         C.ss_raw.raw=malloc(C.ss_raw.length);
1274         if(!limited_read(C.ss_raw.raw,C.ss_raw.length,&subregion,pinfo))
1275             return 0;
1276         CBP(pinfo,OPS_PTAG_RAW_SS,&content);
1277         return 1;
1278         }
1279
1280     switch(content.tag)
1281         {
1282     case OPS_PTAG_SS_CREATION_TIME:
1283     case OPS_PTAG_SS_EXPIRATION_TIME:
1284     case OPS_PTAG_SS_KEY_EXPIRATION_TIME:
1285         if(!limited_read_time(&C.ss_time.time,&subregion,pinfo))
1286             return 0;
1287         if(content.tag == OPS_PTAG_SS_CREATION_TIME)
1288             {
1289             sig->creation_time=C.ss_time.time;
1290             sig->creation_time_set=ops_true;
1291             }
1292         break;
1293
1294     case OPS_PTAG_SS_TRUST:
1295         if(!limited_read(&C.ss_trust.level,1,&subregion,pinfo)
1296            || !limited_read(&C.ss_trust.amount,1,&subregion,pinfo))
1297             return 0;
1298         break;
1299
1300     case OPS_PTAG_SS_REVOCABLE:
1301         if(!limited_read(bool,1,&subregion,pinfo))
1302             return 0;
1303         C.ss_revocable.revocable=!!bool;
1304         break;
1305
1306     case OPS_PTAG_SS_ISSUER_KEY_ID:
1307         if(!limited_read(C.ss_issuer_key_id.key_id,OPS_KEY_ID_SIZE,
1308                              &subregion,pinfo))
1309             return 0;
1310         memcpy(sig->signer_id,C.ss_issuer_key_id.key_id,OPS_KEY_ID_SIZE);
1311         sig->signer_id_set=ops_true;
1312         break;
1313
1314     case OPS_PTAG_SS_PREFERRED_SKA:
1315         if(!read_data(&C.ss_preferred_ska.data,&subregion,pinfo))
1316             return 0;
1317         break;
1318                                
1319     case OPS_PTAG_SS_PREFERRED_HASH:
1320         if(!read_data(&C.ss_preferred_hash.data,&subregion,pinfo))
1321             return 0;
1322         break;
1323                                
1324     case OPS_PTAG_SS_PREFERRED_COMPRESSION:
1325         if(!read_data(&C.ss_preferred_compression.data,&subregion,pinfo))
1326             return 0;
1327         break;
1328                                
1329     case OPS_PTAG_SS_PRIMARY_USER_ID:
1330         if(!limited_read (bool,1,&subregion,pinfo))
1331             return 0;
1332         C.ss_primary_user_id.primary_user_id = !!bool;
1333         break;
1334  
1335     case OPS_PTAG_SS_KEY_FLAGS:
1336         if(!read_data(&C.ss_key_flags.data,&subregion,pinfo))
1337             return 0;
1338         break;
1339
1340     case OPS_PTAG_SS_KEY_SERVER_PREFS:
1341         if(!read_data(&C.ss_key_server_prefs.data,&subregion,pinfo))
1342             return 0;
1343         break;
1344
1345     case OPS_PTAG_SS_FEATURES:
1346         if(!read_data(&C.ss_features.data,&subregion,pinfo))
1347             return 0;
1348         break;
1349
1350     case OPS_PTAG_SS_SIGNERS_USER_ID:
1351         if(!read_unsigned_string(&C.ss_signers_user_id.user_id,&subregion,pinfo))
1352             return 0;
1353         break;
1354
1355     case OPS_PTAG_SS_NOTATION_DATA:
1356         if(!limited_read_data(&C.ss_notation_data.flags,4,&subregion,pinfo))
1357             return 0;
1358         if(!limited_read_size_t_scalar(&C.ss_notation_data.name.len,2,
1359                                        &subregion,pinfo))
1360             return 0;
1361         if(!limited_read_size_t_scalar(&C.ss_notation_data.value.len,2,
1362                                        &subregion,pinfo))
1363             return 0;
1364         if(!limited_read_data(&C.ss_notation_data.name,
1365                               C.ss_notation_data.name.len,&subregion,pinfo))
1366             return 0;
1367         if(!limited_read_data(&C.ss_notation_data.value,
1368                               C.ss_notation_data.value.len,&subregion,pinfo))
1369             return 0;
1370         break;
1371
1372     case OPS_PTAG_SS_POLICY_URL:
1373         if(!read_string(&C.ss_policy_url.text,&subregion,pinfo))
1374             return 0;
1375         break;
1376
1377     case OPS_PTAG_SS_REGEXP:
1378         if(!read_string(&C.ss_regexp.text,&subregion, pinfo))
1379             return 0;
1380         break;
1381
1382     case OPS_PTAG_SS_PREFERRED_KEY_SERVER:
1383         if(!read_string(&C.ss_preferred_key_server.text,&subregion,pinfo))
1384             return 0;
1385         break;
1386
1387     case OPS_PTAG_SS_USERDEFINED00:
1388     case OPS_PTAG_SS_USERDEFINED01:
1389     case OPS_PTAG_SS_USERDEFINED02:
1390     case OPS_PTAG_SS_USERDEFINED03:
1391     case OPS_PTAG_SS_USERDEFINED04:
1392     case OPS_PTAG_SS_USERDEFINED05:
1393     case OPS_PTAG_SS_USERDEFINED06:
1394     case OPS_PTAG_SS_USERDEFINED07:
1395     case OPS_PTAG_SS_USERDEFINED08:
1396     case OPS_PTAG_SS_USERDEFINED09:
1397     case OPS_PTAG_SS_USERDEFINED10:
1398         if(!read_data(&C.ss_userdefined.data,&subregion,pinfo))
1399             return 0;
1400         break;
1401
1402     case OPS_PTAG_SS_RESERVED:
1403         if(!read_data(&C.ss_unknown.data,&subregion,pinfo))
1404             return 0;
1405         break;
1406
1407     case OPS_PTAG_SS_REVOCATION_REASON:
1408         /* first byte is the machine-readable code */
1409         if(!limited_read(&C.ss_revocation_reason.code,1,&subregion,pinfo))
1410             return 0;
1411
1412         /* the rest is a human-readable UTF-8 string */
1413         if(!read_string(&C.ss_revocation_reason.text,&subregion,pinfo))
1414             return 0;
1415         break;
1416
1417     case OPS_PTAG_SS_REVOCATION_KEY:
1418         /* octet 0 = class. Bit 0x80 must be set */
1419         if(!limited_read (&C.ss_revocation_key.class,1,&subregion,pinfo))
1420             return 0;
1421         if(!(C.ss_revocation_key.class&0x80))
1422             {
1423             printf("Warning: OPS_PTAG_SS_REVOCATION_KEY class: "
1424                    "Bit 0x80 should be set\n");
1425             return 0;
1426             }
1427  
1428         /* octet 1 = algid */
1429         if(!limited_read(&C.ss_revocation_key.algid,1,&subregion,pinfo))
1430             return 0;
1431  
1432         /* octets 2-21 = fingerprint */
1433         if(!limited_read(&C.ss_revocation_key.fingerprint[0],20,&subregion,
1434                          pinfo))
1435             return 0;
1436         break;
1437  
1438     default:
1439         if(pinfo->ss_parsed[t8]&t7)
1440             ERR1P(pinfo,"Unknown signature subpacket type (%d)",
1441                   c[0]&0x7f);
1442         read=ops_false;
1443         break;
1444         }
1445
1446     /* Application doesn't want it delivered parsed */
1447     if(!(pinfo->ss_parsed[t8]&t7))
1448         {
1449         if(content.critical)
1450             ERR1P(pinfo,"Critical signature subpacket ignored (%d)",
1451                   c[0]&0x7f);
1452         if(!read && !limited_skip(subregion.length-1,&subregion,pinfo))
1453             return 0;
1454         //      printf("skipped %d length %d\n",c[0]&0x7f,subregion.length);
1455         if(read)
1456             ops_parser_content_free(&content);
1457         return 1;
1458         }
1459
1460     if(read && subregion.length_read != subregion.length)
1461         ERR1P(pinfo,"Unconsumed data (%d)", subregion.length-subregion.length_read);
1462  
1463     CBP(pinfo,content.tag,&content);
1464
1465     return 1;
1466     }
1467
1468 /*! Free the memory used when parsing this signature sub-packet type */
1469 void ops_ss_preferred_ska_free(ops_ss_preferred_ska_t *ss_preferred_ska)
1470     {
1471     data_free(&ss_preferred_ska->data);
1472     }
1473
1474 /*! Free the memory used when parsing this signature sub-packet type */
1475 void ops_ss_preferred_hash_free(ops_ss_preferred_hash_t *ss_preferred_hash)
1476     {
1477     data_free(&ss_preferred_hash->data);
1478     }
1479
1480 /*! Free the memory used when parsing this signature sub-packet type */
1481 void ops_ss_preferred_compression_free(ops_ss_preferred_compression_t *ss_preferred_compression)
1482     {
1483     data_free(&ss_preferred_compression->data);
1484     }
1485
1486 /*! Free the memory used when parsing this signature sub-packet type */
1487 void ops_ss_key_flags_free(ops_ss_key_flags_t *ss_key_flags)
1488     {
1489     data_free(&ss_key_flags->data);
1490     }
1491
1492 /*! Free the memory used when parsing this signature sub-packet type */
1493 void ops_ss_features_free(ops_ss_features_t *ss_features)
1494     {
1495     data_free(&ss_features->data);
1496     }
1497
1498 /*! Free the memory used when parsing this signature sub-packet type */
1499 void ops_ss_key_server_prefs_free(ops_ss_key_server_prefs_t *ss_key_server_prefs)
1500     {
1501     data_free(&ss_key_server_prefs->data);
1502     }
1503
1504 /** Parse several signature subpackets.
1505  *
1506  * Hashed and unhashed subpacket sets are preceded by an octet count that specifies the length of the complete set.
1507  * This function parses this length and then calls parse_one_signature_subpacket() for each subpacket until the
1508  * entire set is consumed.
1509  *
1510  * This function does not call the callback directly, parse_one_signature_subpacket() does for each subpacket.
1511  *
1512  * \param *ptag         Pointer to the Packet Tag.
1513  * \param *reader       Our reader
1514  * \param *cb           The callback
1515  * \return              1 on success, 0 on error
1516  *
1517  * \see RFC2440bis-12 5.2.3
1518  */
1519 static int parse_signature_subpackets(ops_signature_t *sig,
1520                                       ops_region_t *region,
1521                                       ops_parse_info_t *pinfo)
1522     {
1523     ops_region_t subregion;
1524     ops_parser_content_t content;
1525
1526     ops_init_subregion(&subregion,region);
1527     if(!limited_read_scalar(&subregion.length,2,region,pinfo))
1528         return 0;
1529
1530     if(subregion.length > region->length)
1531         ERRP(pinfo,"Subpacket set too long");
1532
1533     while(subregion.length_read < subregion.length)
1534         if(!parse_one_signature_subpacket(sig,&subregion,pinfo))
1535             return 0;
1536
1537     if(subregion.length_read != subregion.length)
1538         {
1539         if(!limited_skip(subregion.length-subregion.length_read,&subregion,
1540                          pinfo))
1541             ERRP(pinfo,"Read failed while recovering from subpacket length mismatch");
1542         ERRP(pinfo,"Subpacket length mismatch");
1543         }
1544
1545     return 1;
1546     }
1547
1548 /** Parse a version 4 signature.
1549  *
1550  * This function parses a version 4 signature including all its hashed and unhashed subpackets.
1551  *
1552  * Once the signature packet has been parsed successfully, it is passed to the callback.
1553  *
1554  * \param *ptag         Pointer to the Packet Tag.
1555  * \param *reader       Our reader
1556  * \param *cb           The callback
1557  * \return              1 on success, 0 on error
1558  *
1559  * \see RFC2440bis-12 5.2.3
1560  */
1561 static int parse_v4_signature(ops_region_t *region,ops_parse_info_t *pinfo,
1562                               size_t v4_hashed_data_start)
1563     {
1564     unsigned char c[1];
1565     ops_parser_content_t content;
1566
1567     memset(&C.signature,'\0',sizeof C.signature);
1568     C.signature.version=OPS_V4;
1569     C.signature.v4_hashed_data_start=v4_hashed_data_start;
1570
1571     if(!limited_read(c,1,region,pinfo))
1572         return 0;
1573     C.signature.type=c[0];
1574     /* XXX: check signature type */
1575
1576     if(!limited_read(c,1,region,pinfo))
1577         return 0;
1578     C.signature.key_algorithm=c[0];
1579     /* XXX: check algorithm */
1580
1581     if(!limited_read(c,1,region,pinfo))
1582         return 0;
1583     C.signature.hash_algorithm=c[0];
1584     /* XXX: check algorithm */
1585
1586     CBP(pinfo,OPS_PTAG_CT_SIGNATURE_HEADER,&content);
1587
1588     if(!parse_signature_subpackets(&C.signature,region,pinfo))
1589         return 0;
1590     C.signature.v4_hashed_data_length=pinfo->rinfo.alength
1591         -C.signature.v4_hashed_data_start;
1592
1593     if(!parse_signature_subpackets(&C.signature,region,pinfo))
1594         return 0;
1595    
1596     if(!limited_read(C.signature.hash2,2,region,pinfo))
1597         return 0;
1598
1599     switch(C.signature.key_algorithm)
1600         {
1601     case OPS_PKA_RSA:
1602         if(!limited_read_mpi(&C.signature.signature.rsa.sig,region,pinfo))
1603             return 0;
1604         break;
1605
1606     case OPS_PKA_DSA:
1607         if(!limited_read_mpi(&C.signature.signature.dsa.r,region,pinfo))
1608             ERRP(pinfo,"Error reading DSA r field in signature");
1609         if (!limited_read_mpi(&C.signature.signature.dsa.s,region,pinfo))
1610             ERRP(pinfo,"Error reading DSA s field in signature");
1611         break;
1612
1613     case OPS_PKA_ELGAMAL_ENCRYPT_OR_SIGN:
1614         if(!limited_read_mpi(&C.signature.signature.elgamal.r,region,pinfo)
1615            || !limited_read_mpi(&C.signature.signature.elgamal.s,region,pinfo))
1616             return 0;
1617         break;
1618
1619     case OPS_PKA_PRIVATE00:
1620     case OPS_PKA_PRIVATE01:
1621     case OPS_PKA_PRIVATE02:
1622     case OPS_PKA_PRIVATE03:
1623     case OPS_PKA_PRIVATE04:
1624     case OPS_PKA_PRIVATE05:
1625     case OPS_PKA_PRIVATE06:
1626     case OPS_PKA_PRIVATE07:
1627     case OPS_PKA_PRIVATE08:
1628     case OPS_PKA_PRIVATE09:
1629     case OPS_PKA_PRIVATE10:
1630         if (!read_data(&C.signature.signature.unknown.data,region,pinfo))
1631             return 0;
1632         break;
1633
1634     default:
1635         ERR1P(pinfo,"Bad v4 signature key algorithm (%d)",
1636               C.signature.key_algorithm);
1637         }
1638
1639     if(region->length_read != region->length)
1640         ERR1P(pinfo,"Unconsumed data (%d)",
1641               region->length-region->length_read);
1642
1643     CBP(pinfo,OPS_PTAG_CT_SIGNATURE_FOOTER,&content);
1644
1645     return 1;
1646     }
1647
1648 /** Parse a signature subpacket.
1649  *
1650  * This function calls the appropriate function to handle v3 or v4 signatures.
1651  *
1652  * Once the signature packet has been parsed successfully, it is passed to the callback.
1653  *
1654  * \param *ptag         Pointer to the Packet Tag.
1655  * \param *reader       Our reader
1656  * \param *cb           The callback
1657  * \return              1 on success, 0 on error
1658  */
1659 static int parse_signature(ops_region_t *region,ops_parse_info_t *pinfo)
1660     {
1661     unsigned char c[1];
1662     ops_parser_content_t content;
1663     size_t v4_hashed_data_start;
1664
1665     assert(region->length_read == 0);  /* We should not have read anything so far */
1666
1667     memset(&content,'\0',sizeof content);
1668
1669     v4_hashed_data_start=pinfo->rinfo.alength;
1670     if(!limited_read(c,1,region,pinfo))
1671         return 0;
1672
1673     if(c[0] == 2 || c[0] == 3)
1674         return parse_v3_signature(region,pinfo);
1675     else if(c[0] == 4)
1676         return parse_v4_signature(region,pinfo,v4_hashed_data_start);
1677     ERR1P(pinfo,"Bad signature version (%d)",c[0]);
1678     }
1679
1680 static int parse_compressed(ops_region_t *region,ops_parse_info_t *pinfo)
1681     {
1682     unsigned char c[1];
1683     ops_parser_content_t content;
1684
1685     if(!limited_read(c,1,region,pinfo))
1686         return 0;
1687
1688     C.compressed.type=c[0];
1689
1690     CBP(pinfo,OPS_PTAG_CT_COMPRESSED,&content);
1691
1692     /* The content of a compressed data packet is more OpenPGP packets
1693        once decompressed, so recursively handle them */
1694
1695     return ops_decompress(region,pinfo,C.compressed.type);
1696     }
1697
1698 static int parse_one_pass(ops_region_t *region,ops_parse_info_t *pinfo)
1699     {
1700     unsigned char c[1];
1701     ops_parser_content_t content;
1702
1703     if(!limited_read(&C.one_pass_signature.version,1,region,pinfo))
1704         return 0;
1705     if(C.one_pass_signature.version != 3)
1706         ERR1P(pinfo,"Bad one-pass signature version (%d)",
1707              C.one_pass_signature.version);
1708
1709     if(!limited_read(c,1,region,pinfo))
1710         return 0;
1711     C.one_pass_signature.sig_type=c[0];
1712
1713     if(!limited_read(c,1,region,pinfo))
1714         return 0;
1715     C.one_pass_signature.hash_algorithm=c[0];
1716
1717     if(!limited_read(c,1,region,pinfo))
1718         return 0;
1719     C.one_pass_signature.key_algorithm=c[0];
1720
1721     if(!limited_read(C.one_pass_signature.keyid,
1722                          sizeof C.one_pass_signature.keyid,region,pinfo))
1723         return 0;
1724
1725     if(!limited_read(c,1,region,pinfo))
1726         return 0;
1727     C.one_pass_signature.nested=!!c[0];
1728
1729     CBP(pinfo,OPS_PTAG_CT_ONE_PASS_SIGNATURE,&content);
1730
1731     // XXX: we should, perhaps, let the app choose whether to hash or not
1732     ops_parse_hash_init(pinfo,C.one_pass_signature.hash_algorithm,
1733                         C.one_pass_signature.keyid);
1734
1735     return 1;
1736     }
1737
1738 /*! Free the memory used when parsing this signature sub-packet type */
1739 void ops_ss_userdefined_free(ops_ss_userdefined_t *ss_userdefined)
1740     {
1741     data_free(&ss_userdefined->data);
1742     }
1743
1744 /*! Free the memory used when parsing this signature sub-packet type */
1745 void ops_ss_reserved_free(ops_ss_unknown_t *ss_unknown)
1746     {
1747     data_free(&ss_unknown->data);
1748     }
1749
1750 /*! Free the memory used when parsing this signature sub-packet type */
1751 void ops_ss_notation_data_free(ops_ss_notation_data_t *ss_notation_data)
1752      {
1753      data_free(&ss_notation_data->name);
1754      data_free(&ss_notation_data->value);
1755      }
1756
1757 /*! Free the memory used when parsing this signature sub-packet type */
1758 void ops_ss_revocation_reason_free(ops_ss_revocation_reason_t *ss_revocation_reason)
1759     {
1760     string_free(&ss_revocation_reason->text);
1761     }
1762
1763 /*! Free the memory used when parsing this packet type */
1764 void ops_trust_free(ops_trust_t *trust)
1765     {
1766     data_free(&trust->data);
1767     }
1768
1769 static int
1770 parse_trust (ops_region_t *region, ops_parse_info_t *pinfo)
1771     {
1772     ops_parser_content_t content;
1773
1774     if(!read_data(&C.trust.data,region,pinfo))
1775             return 0;
1776
1777     CBP(pinfo,OPS_PTAG_CT_TRUST, &content);
1778
1779     return 1;
1780     }
1781
1782 static int parse_literal_data(ops_region_t *region,ops_parse_info_t *pinfo)
1783     {
1784     ops_parser_content_t content;
1785     unsigned char c[1];
1786
1787     if(!limited_read(c,1,region,pinfo))
1788         return 0;
1789     C.literal_data_header.format=c[0];
1790
1791     if(!limited_read(c,1,region,pinfo))
1792         return 0;
1793     if(!limited_read((unsigned char *)C.literal_data_header.filename,c[0],
1794                      region,pinfo))
1795         return 0;
1796     C.literal_data_header.filename[c[0]]='\0';
1797
1798     if(!limited_read_time(&C.literal_data_header.modification_time,region,pinfo))
1799         return 0;
1800
1801     CBP(pinfo,OPS_PTAG_CT_LITERAL_DATA_HEADER,&content);
1802
1803     while(region->length_read < region->length)
1804         {
1805         unsigned l=region->length-region->length_read;
1806
1807         if(l > sizeof C.literal_data_body.data)
1808             l=sizeof C.literal_data_body.data;
1809
1810         if(!limited_read(C.literal_data_body.data,l,region,pinfo))
1811             return 0;
1812
1813         C.literal_data_body.length=l;
1814
1815         ops_parse_hash_data(pinfo,C.literal_data_body.data,l);
1816
1817         CBP(pinfo,OPS_PTAG_CT_LITERAL_DATA_BODY,&content);
1818         }
1819
1820     return 1;
1821     }
1822
1823 /**
1824  * \ingroup Memory
1825  *
1826  * ops_secret_key_free() frees the memory associated with "key". Note that
1827  * the key itself is not freed.
1828  *
1829  * \param key
1830  */
1831
1832 void ops_secret_key_free(ops_secret_key_t *key)
1833     {
1834     switch(key->public_key.algorithm)
1835         {
1836     case OPS_PKA_RSA:
1837     case OPS_PKA_RSA_ENCRYPT_ONLY:
1838     case OPS_PKA_RSA_SIGN_ONLY:
1839         free_BN(&key->key.rsa.d);
1840         free_BN(&key->key.rsa.p);
1841         free_BN(&key->key.rsa.q);
1842         free_BN(&key->key.rsa.u);
1843         break;
1844
1845     case OPS_PKA_DSA:
1846         free_BN(&key->key.dsa.x);
1847         break;
1848
1849     default:
1850         fprintf(stderr,"Unknown algorithm: %d\n",key->public_key.algorithm);
1851         assert(0);
1852         }
1853
1854     ops_public_key_free(&key->public_key);
1855     }
1856
1857 static int consume_packet(ops_region_t *region,ops_parse_info_t *pinfo,
1858                           ops_boolean_t warn)
1859     {
1860     ops_data_t remainder;
1861     ops_parser_content_t content;
1862
1863     if(region->indeterminate)
1864         ERRP(pinfo,"Can't consume indeterminate packets");
1865
1866     if(read_data(&remainder,region,pinfo))
1867         {
1868         /* now throw it away */
1869         data_free(&remainder);
1870         if(warn)
1871             ERRCODEP(pinfo,OPS_E_P_PACKET_CONSUMED);
1872         }
1873     else if(warn)
1874         WARNP(pinfo,"Problem consuming remainder of error packet.");
1875     else
1876         return 0;
1877
1878     return 1;
1879     }
1880
1881 static int parse_secret_key(ops_region_t *region,ops_parse_info_t *pinfo)
1882     {
1883     ops_parser_content_t content;
1884     unsigned char c[1];
1885     ops_decrypt_t decrypt;
1886     int ret=1;
1887     ops_region_t encregion;
1888     ops_region_t *saved_region=NULL;
1889     size_t checksum_length=2;
1890     ops_hash_t checkhash;
1891     int blocksize;
1892     ops_boolean_t crypted;
1893
1894     memset(&content,'\0',sizeof content);
1895     if(!parse_public_key_data(&C.secret_key.public_key,region,pinfo))
1896         return 0;
1897
1898     pinfo->reading_v3_secret=C.secret_key.public_key.version != OPS_V4;
1899
1900     if(!limited_read(c,1,region,pinfo))
1901         return 0;
1902     C.secret_key.s2k_usage=c[0];
1903
1904     if(C.secret_key.s2k_usage == OPS_S2KU_ENCRYPTED_AND_HASHED)
1905         checksum_length=20;
1906
1907     if(C.secret_key.s2k_usage == OPS_S2KU_ENCRYPTED
1908        || C.secret_key.s2k_usage == OPS_S2KU_ENCRYPTED_AND_HASHED)
1909         {
1910         if(!limited_read(c,1,region,pinfo))
1911             return 0;
1912         C.secret_key.algorithm=c[0];
1913
1914         if(!limited_read(c,1,region,pinfo))
1915             return 0;
1916         C.secret_key.s2k_specifier=c[0];
1917
1918         assert(C.secret_key.s2k_specifier == OPS_S2KS_SIMPLE
1919                || C.secret_key.s2k_specifier == OPS_S2KS_SALTED
1920                || C.secret_key.s2k_specifier == OPS_S2KS_ITERATED_AND_SALTED);
1921
1922         if(!limited_read(c,1,region,pinfo))
1923             return 0;
1924         C.secret_key.hash_algorithm=c[0];
1925
1926         if(C.secret_key.s2k_specifier != OPS_S2KS_SIMPLE
1927            && !limited_read(C.secret_key.salt,8,region,pinfo))
1928             return 0;
1929
1930         if(C.secret_key.s2k_specifier == OPS_S2KS_ITERATED_AND_SALTED)
1931             {
1932             if(!limited_read(c,1,region,pinfo))
1933                 return 0;
1934             C.secret_key.octet_count=(16+(c[0]&15)) << ((c[0] >> 4)+6);
1935             }
1936         }
1937     else if(C.secret_key.s2k_usage != OPS_S2KU_NONE)
1938         {
1939         // this is V3 style, looks just like a V4 simple hash
1940         C.secret_key.algorithm=C.secret_key.s2k_usage;
1941         C.secret_key.s2k_usage=OPS_S2KU_ENCRYPTED;
1942         C.secret_key.s2k_specifier=OPS_S2KS_SIMPLE;
1943         C.secret_key.hash_algorithm=OPS_HASH_MD5;
1944         }
1945
1946     crypted=C.secret_key.s2k_usage == OPS_S2KU_ENCRYPTED
1947         || C.secret_key.s2k_usage == OPS_S2KU_ENCRYPTED_AND_HASHED;
1948
1949     if(crypted)
1950         {
1951         int n;
1952         ops_parser_content_t pc;
1953         char *passphrase;
1954         unsigned char key[OPS_MAX_KEY_SIZE+OPS_MAX_HASH_SIZE];
1955         ops_hash_t hashes[(OPS_MAX_KEY_SIZE+OPS_MIN_HASH_SIZE-1)/OPS_MIN_HASH_SIZE];
1956         int keysize;
1957         int hashsize;
1958         size_t l;
1959
1960         blocksize=ops_block_size(C.secret_key.algorithm);
1961         assert(blocksize > 0 && blocksize <= OPS_MAX_BLOCK_SIZE);
1962
1963         if(!limited_read(C.secret_key.iv,blocksize,region,pinfo))
1964             return 0;
1965
1966         memset(&pc,'\0',sizeof pc);
1967         passphrase=NULL;
1968         pc.content.secret_key_passphrase.passphrase=&passphrase;
1969         pc.content.secret_key_passphrase.secret_key=&C.secret_key;
1970         CBP(pinfo,OPS_PARSER_CMD_GET_SK_PASSPHRASE,&pc);
1971         if(!passphrase)
1972             {
1973             if(!consume_packet(region,pinfo,ops_false))
1974                return 0;
1975
1976             CBP(pinfo,OPS_PTAG_CT_ENCRYPTED_SECRET_KEY,&content);
1977
1978             return 1;
1979             }
1980
1981         keysize=ops_key_size(C.secret_key.algorithm);
1982         assert(keysize > 0 && keysize <= OPS_MAX_KEY_SIZE);
1983
1984         hashsize=ops_hash_size(C.secret_key.hash_algorithm);
1985         assert(hashsize > 0 && hashsize <= OPS_MAX_HASH_SIZE);
1986
1987         for(n=0 ; n*hashsize < keysize ; ++n)
1988             {
1989             int i;
1990
1991             ops_hash_any(&hashes[n],C.secret_key.hash_algorithm);
1992             hashes[n].init(&hashes[n]);
1993             // preload hashes with zeroes...
1994             for(i=0 ; i < n ; ++i)
1995                 hashes[n].add(&hashes[n],(unsigned char *)"",1);
1996             }
1997
1998         l=strlen(passphrase);
1999
2000         for(n=0 ; n*hashsize < keysize ; ++n)
2001             {
2002             unsigned i;
2003
2004             switch(C.secret_key.s2k_specifier)
2005                 {
2006             case OPS_S2KS_SALTED:
2007                 hashes[n].add(&hashes[n],C.secret_key.salt,OPS_SALT_SIZE);
2008                 // flow through...
2009             case OPS_S2KS_SIMPLE:
2010                 hashes[n].add(&hashes[n],(unsigned char*)passphrase,l);
2011                 break;
2012
2013             case OPS_S2KS_ITERATED_AND_SALTED:
2014                 for(i=0 ; i < C.secret_key.octet_count ; i+=l+OPS_SALT_SIZE)
2015                     {
2016                     int j=l+OPS_SALT_SIZE;
2017
2018                     if(i+j > C.secret_key.octet_count && i != 0)
2019                         j=C.secret_key.octet_count-i;
2020
2021                     hashes[n].add(&hashes[n],C.secret_key.salt,
2022                                   j > OPS_SALT_SIZE ? OPS_SALT_SIZE : j);
2023                     if(j > OPS_SALT_SIZE)
2024                         hashes[n].add(&hashes[n],(unsigned char *)passphrase,j-OPS_SALT_SIZE);
2025                     }
2026                        
2027                 }
2028             }
2029
2030         for(n=0 ; n*hashsize < keysize ; ++n)
2031             {
2032             int r=hashes[n].finish(&hashes[n],key+n*hashsize);
2033             assert(r == hashsize);
2034             }
2035
2036         free(passphrase);
2037
2038         ops_decrypt_any(&decrypt,C.secret_key.algorithm);
2039         decrypt.set_iv(&decrypt,C.secret_key.iv);
2040         decrypt.set_key(&decrypt,key);
2041
2042         ops_reader_push_decrypt(pinfo,&decrypt,region);
2043
2044         /* Since all known encryption for PGP doesn't compress, we can
2045            limit to the same length as the current region (for now).
2046         */
2047         ops_init_subregion(&encregion,NULL);
2048         encregion.length=region->length-region->length_read;
2049         if(C.secret_key.public_key.version != OPS_V4)
2050             encregion.length-=2;
2051         saved_region=region;
2052         region=&encregion;
2053         }
2054
2055     if(C.secret_key.s2k_usage == OPS_S2KU_ENCRYPTED_AND_HASHED)
2056         {
2057         ops_hash_sha1(&checkhash);
2058         ops_reader_push_hash(pinfo,&checkhash);
2059         }
2060     else
2061         ops_reader_push_sum16(pinfo);
2062
2063     switch(C.secret_key.public_key.algorithm)
2064         {
2065     case OPS_PKA_RSA:
2066     case OPS_PKA_RSA_ENCRYPT_ONLY:
2067     case OPS_PKA_RSA_SIGN_ONLY:
2068         if(!limited_read_mpi(&C.secret_key.key.rsa.d,region,pinfo)
2069            || !limited_read_mpi(&C.secret_key.key.rsa.p,region,pinfo)
2070            || !limited_read_mpi(&C.secret_key.key.rsa.q,region,pinfo)
2071            || !limited_read_mpi(&C.secret_key.key.rsa.u,region,pinfo))
2072             ret=0;
2073         break;
2074
2075     case OPS_PKA_DSA:
2076         if(!limited_read_mpi(&C.secret_key.key.dsa.x,region,pinfo))
2077             ret=0;
2078         break;
2079
2080     default:
2081         fprintf(stderr,"Unexpected aglorithm: %d\n",
2082                 C.secret_key.public_key.algorithm);
2083         ret=0;
2084         assert(0);
2085         }
2086
2087     pinfo->reading_v3_secret=ops_false;
2088
2089     if(C.secret_key.s2k_usage == OPS_S2KU_ENCRYPTED_AND_HASHED)
2090         {
2091         unsigned char hash[20];
2092
2093         ops_reader_pop_hash(pinfo);
2094         checkhash.finish(&checkhash,hash);
2095            
2096         if(crypted && C.secret_key.public_key.version != OPS_V4)
2097             {
2098             ops_reader_pop_decrypt(pinfo);
2099             region=saved_region;
2100             }
2101
2102         if(ret)
2103             {
2104             if(!limited_read(C.secret_key.checkhash,20,region,pinfo))
2105                 return 0;
2106
2107             if(memcmp(hash,C.secret_key.checkhash,20))
2108                 ERRP(pinfo,"Hash mismatch in secret key");
2109             }
2110         }
2111     else
2112         {
2113         unsigned short sum;
2114
2115         sum=ops_reader_pop_sum16(pinfo);
2116
2117         if(crypted && C.secret_key.public_key.version != OPS_V4)
2118             {
2119             ops_reader_pop_decrypt(pinfo);
2120             region=saved_region;
2121             }
2122
2123         if(ret)
2124             {
2125             if(!limited_read_scalar(&C.secret_key.checksum,2,region,
2126                                     pinfo))
2127                 return 0;
2128
2129             if(sum != C.secret_key.checksum)
2130                 ERRP(pinfo,"Checksum mismatch in secret key");
2131             }
2132         }
2133
2134     if(crypted && C.secret_key.public_key.version == OPS_V4)
2135         ops_reader_pop_decrypt(pinfo);
2136
2137     assert(!ret || region->length_read == region->length);
2138
2139     if(!ret)
2140         return 0;
2141
2142     CBP(pinfo,OPS_PTAG_CT_SECRET_KEY,&content);
2143
2144     return 1;
2145     }
2146
2147 static int parse_pk_session_key(ops_region_t *region,
2148                                 ops_parse_info_t *pinfo)
2149     {
2150     unsigned char c[1];
2151     ops_parser_content_t content;
2152     ops_parser_content_t pc;
2153     unsigned char buf[8192];
2154     int n;
2155     BIGNUM *enc_m;
2156     unsigned k;
2157     const ops_secret_key_t *secret;
2158
2159     if(!limited_read(c,1,region,pinfo))
2160         return 0;
2161     C.pk_session_key.version=c[0];
2162     if(C.pk_session_key.version != OPS_PKSK_V3)
2163         ERR1P(pinfo,
2164               "Bad public-key encrypted session key version (%d)",
2165               C.pk_session_key.version);
2166
2167     if(!limited_read(C.pk_session_key.key_id,
2168                      sizeof C.pk_session_key.key_id,region,pinfo))
2169         return 0;
2170
2171     if(!limited_read(c,1,region,pinfo))
2172         return 0;
2173     C.pk_session_key.algorithm=c[0];
2174     switch(C.pk_session_key.algorithm)
2175         {
2176     case OPS_PKA_RSA:
2177         if(!limited_read_mpi(&C.pk_session_key.parameters.rsa.encrypted_m,
2178                              region,pinfo))
2179             return 0;
2180         enc_m=C.pk_session_key.parameters.rsa.encrypted_m;
2181         break;
2182
2183     case OPS_PKA_ELGAMAL:
2184         if(!limited_read_mpi(&C.pk_session_key.parameters.elgamal.g_to_k,
2185                              region,pinfo)
2186            || limited_read_mpi(&C.pk_session_key.parameters.elgamal.encrypted_m,
2187                              region,pinfo))
2188             return 0;
2189         enc_m=C.pk_session_key.parameters.elgamal.encrypted_m;
2190         break;
2191
2192     default:
2193         ERR1P(pinfo,
2194               "Unknown public key algorithm in session key (%d)",
2195               C.pk_session_key.algorithm);
2196         return 0;
2197         }
2198
2199     memset(&pc,'\0',sizeof pc);
2200     secret=NULL;
2201     pc.content.get_secret_key.secret_key=&secret;
2202     pc.content.get_secret_key.pk_session_key=&C.pk_session_key;
2203
2204     CBP(pinfo,OPS_PARSER_CMD_GET_SECRET_KEY,&pc);
2205
2206     if(!secret)
2207         {
2208         CBP(pinfo,OPS_PTAG_CT_ENCRYPTED_PK_SESSION_KEY,&content);
2209
2210         return 1;
2211         }
2212
2213     n=ops_decrypt_mpi(buf,sizeof buf,enc_m,secret);
2214
2215     if(n < 1)
2216         ERRP(pinfo,"decrypted message too short");
2217
2218     C.pk_session_key.symmetric_algorithm=buf[0];
2219     k=ops_key_size(C.pk_session_key.symmetric_algorithm);
2220
2221     if((unsigned)n != k+3)
2222         ERR2P(pinfo,"decrypted message wrong length (got %d expected %d)",
2223               n,k+3);
2224    
2225     assert(k <= sizeof C.pk_session_key.key);
2226
2227     memcpy(C.pk_session_key.key,buf+1,k);
2228
2229     C.pk_session_key.checksum=buf[k+1]+(buf[k+2] << 8);
2230
2231     // XXX: Check checksum!
2232
2233     CBP(pinfo,OPS_PTAG_CT_PK_SESSION_KEY,&content);
2234
2235     ops_decrypt_any(&pinfo->decrypt,C.pk_session_key.symmetric_algorithm);
2236     pinfo->decrypt.set_key(&pinfo->decrypt,C.pk_session_key.key);
2237
2238     return 1;
2239     }
2240
2241 // XXX: make this static?
2242 int ops_decrypt_data(ops_content_tag_t tag,ops_region_t *region,
2243                      ops_parse_info_t *pinfo)
2244     {
2245     int r=1;
2246     ops_decrypt_t *decrypt=ops_parse_get_decrypt(pinfo);
2247
2248     if(decrypt)
2249         {
2250         unsigned char buf[OPS_MAX_BLOCK_SIZE+2];
2251         size_t b=decrypt->blocksize;
2252         ops_parser_content_t content;
2253         ops_region_t encregion;
2254
2255        
2256         ops_reader_push_decrypt(pinfo,decrypt,region);
2257
2258         ops_init_subregion(&encregion,NULL);
2259         encregion.length=b+2;
2260
2261         if(!exact_limited_read(buf,b+2,&encregion,pinfo))
2262             return 0;
2263
2264         if(buf[b-2] != buf[b] || buf[b-1] != buf[b+1])
2265             {
2266             ops_reader_pop_decrypt(pinfo);
2267             ERR4P(pinfo,"Bad symmetric decrypt (%02x%02x vs %02x%02x)",
2268                   buf[b-2],buf[b-1],buf[b],buf[b+1]);
2269             }
2270
2271         if(tag == OPS_PTAG_CT_SE_DATA_BODY)
2272             {
2273             decrypt->resync(decrypt);
2274             decrypt->block_encrypt(decrypt,decrypt->civ,decrypt->civ);
2275             }
2276
2277         r=ops_parse(pinfo);
2278         ops_reader_pop_decrypt(pinfo);
2279         }
2280     else
2281         {
2282         ops_parser_content_t content;
2283
2284         while(region->length_read < region->length)
2285             {
2286             unsigned l=region->length-region->length_read;
2287
2288             if(l > sizeof C.se_data_body.data)
2289                 l=sizeof C.se_data_body.data;
2290
2291             if(!limited_read(C.se_data_body.data,l,region,pinfo))
2292                 return 0;
2293
2294             C.se_data_body.length=l;
2295
2296             CBP(pinfo,tag,&content);
2297             }
2298         }
2299
2300     return r;
2301     }
2302
2303 static int parse_se_data(ops_region_t *region,ops_parse_info_t *pinfo)
2304     {
2305     ops_parser_content_t content;
2306
2307     /* there's no info to go with this, so just announce it */
2308     CBP(pinfo,OPS_PTAG_CT_SE_DATA_HEADER,&content);
2309
2310     /* The content of an encrypted data packet is more OpenPGP packets
2311        once decrypted, so recursively handle them */
2312     return ops_decrypt_data(OPS_PTAG_CT_SE_DATA_BODY,region,pinfo);
2313     }
2314
2315 static int parse_se_ip_data(ops_region_t *region,ops_parse_info_t *pinfo)
2316     {
2317     unsigned char c[1];
2318     ops_parser_content_t content;
2319
2320     if(!limited_read(c,1,region,pinfo))
2321         return 0;
2322     C.se_ip_data_header.version=c[0];
2323     assert(C.se_ip_data_header.version == OPS_SE_IP_V1);
2324
2325     CBP(pinfo,OPS_PTAG_CT_SE_IP_DATA_HEADER,&content);
2326
2327     /* The content of an encrypted data packet is more OpenPGP packets
2328        once decrypted, so recursively handle them */
2329     return ops_decrypt_data(OPS_PTAG_CT_SE_IP_DATA_BODY,region,pinfo);
2330     }
2331
2332 /** Parse one packet.
2333  *
2334  * This function parses the packet tag.  It computes the value of the
2335  * content tag and then calls the appropriate function to handle the
2336  * content.
2337  *
2338  * \param *pinfo        How to parse
2339  * \param *pktlen       On return, will contain number of bytes in packet
2340  * \return 1 on success, 0 on error, -1 on EOF */
2341 static int ops_parse_one_packet(ops_parse_info_t *pinfo,
2342                                 unsigned long *pktlen)
2343     {
2344     unsigned char ptag[1];
2345     ops_parser_content_t content;
2346     int r;
2347     ops_region_t region;
2348     ops_boolean_t indeterminate=ops_false;
2349
2350     C.ptag.position=pinfo->rinfo.position;
2351
2352     r=base_read(ptag,1,pinfo);
2353
2354     // errors in the base read are effectively EOF.
2355     if(r <= 0)
2356         return -1;
2357
2358     *pktlen=0;
2359
2360     if(!(*ptag&OPS_PTAG_ALWAYS_SET))
2361         {
2362         C.error.error="Format error (ptag bit not set)";
2363         CBP(pinfo,OPS_PARSER_ERROR,&content);
2364         return 0;
2365         }
2366     C.ptag.new_format=!!(*ptag&OPS_PTAG_NEW_FORMAT);
2367     if(C.ptag.new_format)
2368         {
2369         C.ptag.content_tag=*ptag&OPS_PTAG_NF_CONTENT_TAG_MASK;
2370         C.ptag.length_type=0;
2371         if(!read_new_length(&C.ptag.length,pinfo))
2372             return 0;
2373
2374         }
2375     else
2376         {
2377         ops_boolean_t rb;
2378
2379         C.ptag.content_tag=(*ptag&OPS_PTAG_OF_CONTENT_TAG_MASK)
2380             >> OPS_PTAG_OF_CONTENT_TAG_SHIFT;
2381         C.ptag.length_type=*ptag&OPS_PTAG_OF_LENGTH_TYPE_MASK;
2382         switch(C.ptag.length_type)
2383             {
2384         case OPS_PTAG_OF_LT_ONE_BYTE:
2385             rb=_read_scalar(&C.ptag.length,1,pinfo);
2386             break;
2387
2388         case OPS_PTAG_OF_LT_TWO_BYTE:
2389             rb=_read_scalar(&C.ptag.length,2,pinfo);
2390             break;
2391
2392         case OPS_PTAG_OF_LT_FOUR_BYTE:
2393             rb=_read_scalar(&C.ptag.length,4,pinfo);
2394             break;
2395
2396         case OPS_PTAG_OF_LT_INDETERMINATE:
2397             C.ptag.length=0;
2398             indeterminate=ops_true;
2399             rb=ops_true;
2400             break;
2401             }
2402         if(!rb)
2403             return 0;
2404         }
2405
2406     CBP(pinfo,OPS_PARSER_PTAG,&content);
2407
2408     ops_init_subregion(&region,NULL);
2409     region.length=C.ptag.length;
2410     region.indeterminate=indeterminate;
2411     switch(C.ptag.content_tag)
2412         {
2413     case OPS_PTAG_CT_SIGNATURE:
2414         r=parse_signature(&region,pinfo);
2415         break;
2416
2417     case OPS_PTAG_CT_PUBLIC_KEY:
2418     case OPS_PTAG_CT_PUBLIC_SUBKEY:
2419         r=parse_public_key(C.ptag.content_tag,&region,pinfo);
2420         break;
2421
2422     case OPS_PTAG_CT_TRUST:
2423         r=parse_trust(&region, pinfo);
2424         break;
2425      
2426     case OPS_PTAG_CT_USER_ID:
2427         r=parse_user_id(&region,pinfo);
2428         break;
2429
2430     case OPS_PTAG_CT_COMPRESSED:
2431         r=parse_compressed(&region,pinfo);
2432         break;
2433
2434     case OPS_PTAG_CT_ONE_PASS_SIGNATURE:
2435         r=parse_one_pass(&region,pinfo);
2436         break;
2437
2438     case OPS_PTAG_CT_LITERAL_DATA:
2439         r=parse_literal_data(&region,pinfo);
2440         break;
2441
2442     case OPS_PTAG_CT_USER_ATTRIBUTE:
2443         r=parse_user_attribute(&region,pinfo);
2444         break;
2445
2446     case OPS_PTAG_CT_SECRET_KEY:
2447         r=parse_secret_key(&region,pinfo);
2448         break;
2449
2450     case OPS_PTAG_CT_PK_SESSION_KEY:
2451         r=parse_pk_session_key(&region,pinfo);
2452         break;
2453
2454     case OPS_PTAG_CT_SE_DATA:
2455         r=parse_se_data(&region,pinfo);
2456         break;
2457
2458     case OPS_PTAG_CT_SE_IP_DATA:
2459         r=parse_se_ip_data(&region,pinfo);
2460         break;
2461
2462     default:
2463         format_error(&content,"Format error (unknown content tag %d)",
2464                      C.ptag.content_tag);
2465         ERRCODEP(pinfo,OPS_E_P_UNKNOWN_TAG);
2466         r=0;
2467         }
2468
2469     /* Ensure that the entire packet has been consumed */
2470
2471     if(region.length != region.length_read && !region.indeterminate)
2472         if(!consume_packet(&region,pinfo,ops_true))
2473             r=-1;
2474
2475     /* set pktlen */
2476
2477     *pktlen=pinfo->rinfo.alength;
2478
2479     /* do callback on entire packet, if desired */
2480
2481     if(pinfo->rinfo.accumulate)
2482         {
2483         C.packet.length=pinfo->rinfo.alength;
2484         C.packet.raw=pinfo->rinfo.accumulated;
2485         pinfo->rinfo.accumulated=NULL;
2486         pinfo->rinfo.asize=0;
2487         CBP(pinfo,OPS_PARSER_PACKET_END,&content);
2488         }
2489     pinfo->rinfo.alength=0;
2490        
2491     if(r < 0)
2492         return -1;
2493
2494     return r ? 1 : 0;
2495     }
2496
2497 /**
2498  * \ingroup Parse
2499  *
2500  * ops_parse() parses packets from an input stream until EOF or error.
2501  *
2502  * All the necessary information for parsing should have been set up by the
2503  * calling function in "*pinfo" beforehand.
2504  *
2505  * That information includes :
2506  *
2507  * - a "reader" function to be used to get the data to be parsed
2508  *
2509  * - a "callback" function to be called when this library has identified
2510  * a parseable object within the data
2511  *
2512  * - whether the calling function wants the signature subpackets returned raw, parsed or not at all.
2513  *
2514  * \sa See Detailed Description for usage.
2515  *
2516  * \param *pinfo        How to parse
2517  * \return              1 on success in all packets, 0 on error in any packet
2518  * \todo Add some error checking to make sure *pinfo contains a sensible setup?
2519  */
2520
2521 int ops_parse(ops_parse_info_t *pinfo)
2522     {
2523     int r;
2524     unsigned long pktlen;
2525
2526     do
2527         {
2528         r=ops_parse_one_packet(pinfo,&pktlen);
2529         } while (r != -1);
2530
2531     return pinfo->errors ? 0 : 1;
2532     }
2533
2534 #if 0
2535 /**
2536  *
2537  * \return 1 if success, 0 otherwise
2538  * XXX may not now be needed? RW
2539  */
2540
2541 int ops_parse_errs(ops_parse_info_t *pinfo, ops_ulong_list_t *errs)
2542     {
2543     unsigned err;
2544     int r;
2545     unsigned long pktlen;
2546     ops_reader_fd_arg_t *arg;
2547     int orig_acc;
2548
2549     /* can only handle ops_reader_fd for now */
2550
2551     if (pinfo->rinfo.reader != ops_reader_fd)
2552         {
2553         fprintf(stderr,"ops_parse_errs: can only handle ops_reader_fd\n");
2554         return 0;
2555         }
2556
2557     arg=pinfo->rinfo.arg;
2558
2559     /* store current state of accumulate flag */
2560
2561     orig_acc=pinfo->rinfo.accumulate;
2562
2563     /* set accumulate flag */
2564
2565     pinfo->rinfo.accumulate=1;
2566
2567     /* now parse each error in turn. */
2568
2569     for(err=0; err < errs->used ; err++)
2570         {
2571
2572         //      printf("\n***\n*** Error at offset %lu \n***\n",errs->ulongs[err]);
2573
2574         /* move stream to offset of error */
2575
2576         r=lseek(arg->fd,errs->ulongs[err],SEEK_SET);
2577         if (r==-1)
2578             {
2579             printf("error %d in first lseek to offset\n", errno);
2580             return 0;
2581             }
2582
2583         /* parse packet */
2584
2585         ops_parse_one_packet(pinfo,&pktlen);
2586
2587         }
2588
2589     /* restore accumulate flag original value */
2590     pinfo->rinfo.accumulate=orig_acc;
2591
2592     return 1;
2593     }
2594 #endif
2595
2596 /**
2597  * \ingroup Parse
2598  *
2599  * ops_parse_options() specifies whether one or more signature
2600  * subpacket types should be returned parsed or raw or ignored.
2601  *
2602  * \param       pinfo   Pointer to previously allocated structure
2603  * \param       tag     Packet tag. OPS_PTAG_SS_ALL for all SS tags; or one individual signature subpacket tag
2604  * \param       type    Parse type
2605  * \todo XXX: Make all packet types optional, not just subpackets */
2606 void ops_parse_options(ops_parse_info_t *pinfo,
2607                        ops_content_tag_t tag,
2608                        ops_parse_type_t type)
2609     {
2610     int t8,t7;
2611
2612     if(tag == OPS_PTAG_SS_ALL)
2613         {
2614         int n;
2615
2616         for(n=0 ; n < 256 ; ++n)
2617             ops_parse_options(pinfo,OPS_PTAG_SIGNATURE_SUBPACKET_BASE+n,
2618                               type);
2619         return;
2620         }
2621
2622     assert(tag >= OPS_PTAG_SIGNATURE_SUBPACKET_BASE
2623            && tag <= OPS_PTAG_SIGNATURE_SUBPACKET_BASE+NTAGS-1);
2624     t8=(tag-OPS_PTAG_SIGNATURE_SUBPACKET_BASE)/8;
2625     t7=1 << ((tag-OPS_PTAG_SIGNATURE_SUBPACKET_BASE)&7);
2626     switch(type)
2627         {
2628     case OPS_PARSE_RAW:
2629         pinfo->ss_raw[t8] |= t7;
2630         pinfo->ss_parsed[t8] &= ~t7;
2631         break;
2632
2633     case OPS_PARSE_PARSED:
2634         pinfo->ss_raw[t8] &= ~t7;
2635         pinfo->ss_parsed[t8] |= t7;
2636         break;
2637
2638     case OPS_PARSE_IGNORE:
2639         pinfo->ss_raw[t8] &= ~t7;
2640         pinfo->ss_parsed[t8] &= ~t7;
2641         break;
2642         }
2643     }
2644
2645 ops_parse_info_t *ops_parse_info_new(void)
2646     { return ops_mallocz(sizeof(ops_parse_info_t)); }
2647
2648 void ops_parse_info_delete(ops_parse_info_t *pinfo)
2649     { free(pinfo); }
2650
2651 ops_reader_info_t *ops_parse_get_rinfo(ops_parse_info_t *pinfo)
2652     { return &pinfo->rinfo; }
2653
2654 void ops_parse_cb_set(ops_parse_info_t *pinfo,ops_parse_cb_t *cb,void *arg)
2655     {
2656     pinfo->cbinfo.cb=cb;
2657     pinfo->cbinfo.arg=arg;
2658     }
2659
2660 void ops_parse_cb_push(ops_parse_info_t *pinfo,ops_parse_cb_t *cb,void *arg)
2661     {
2662     ops_parse_cb_info_t *cbinfo=malloc(sizeof *cbinfo);
2663
2664     *cbinfo=pinfo->cbinfo;
2665     pinfo->cbinfo.next=cbinfo;
2666     ops_parse_cb_set(pinfo,cb,arg);
2667     }
2668
2669 void *ops_parse_cb_get_arg(ops_parse_cb_info_t *cbinfo)
2670     { return cbinfo->arg; }
2671
2672 ops_parse_cb_return_t ops_parse_cb(const ops_parser_content_t *content,
2673                                    ops_parse_cb_info_t *cbinfo)
2674     {
2675     if(cbinfo->cb)
2676         return cbinfo->cb(content,cbinfo);
2677     else
2678         return OPS_FINISHED;
2679     }
2680
2681 ops_parse_cb_return_t ops_parse_stacked_cb(const ops_parser_content_t *content,
2682                                            ops_parse_cb_info_t *cbinfo)
2683     { return ops_parse_cb(content,cbinfo->next); }
2684
2685 void ops_reader_set(ops_parse_info_t *pinfo,ops_reader_t *reader,void *arg)
2686     {
2687     pinfo->rinfo.reader=reader;
2688     pinfo->rinfo.arg=arg;
2689     }
2690
2691 void ops_reader_push(ops_parse_info_t *pinfo,ops_reader_t *reader,void *arg)
2692     {
2693     ops_reader_info_t *rinfo=malloc(sizeof *rinfo);
2694
2695     *rinfo=pinfo->rinfo;
2696     memset(&pinfo->rinfo,'\0',sizeof pinfo->rinfo);
2697     pinfo->rinfo.next=rinfo;
2698     pinfo->rinfo.pinfo=pinfo;
2699     ops_reader_set(pinfo,reader,arg);
2700     }
2701
2702 void ops_reader_pop(ops_parse_info_t *pinfo)
2703     {
2704     ops_reader_info_t *next=pinfo->rinfo.next;
2705
2706     pinfo->rinfo=*next;
2707     free(next);
2708     }
2709
2710 void *ops_reader_get_arg(ops_reader_info_t *rinfo)
2711     { return rinfo->arg; }
2712
2713 void *ops_reader_get_arg_from_pinfo(ops_parse_info_t *pinfo)
2714     { return pinfo->rinfo.arg; }
2715
2716 ops_error_t *ops_parse_info_get_errors(ops_parse_info_t *pinfo)
2717     { return pinfo->errors; }
2718
2719 ops_decrypt_t *ops_parse_get_decrypt(ops_parse_info_t *pinfo)
2720     {
2721     if(pinfo->decrypt.algorithm)
2722         return &pinfo->decrypt;
2723     return NULL;
2724     }
2725
2726 // XXX: this could be improved by sharing all hashes that are the
2727 // same, then duping them just before checking the signature.
2728 void ops_parse_hash_init(ops_parse_info_t *pinfo,ops_hash_algorithm_t type,
2729                          const unsigned char *keyid)
2730     {
2731     ops_parse_hash_info_t *hash;
2732
2733     pinfo->hashes=realloc(pinfo->hashes,
2734                           (pinfo->nhashes+1)*sizeof *pinfo->hashes);
2735     hash=&pinfo->hashes[pinfo->nhashes++];
2736
2737     ops_hash_any(&hash->hash,type);
2738     hash->hash.init(&hash->hash);
2739     memcpy(hash->keyid,keyid,sizeof hash->keyid);
2740     }
2741
2742 void ops_parse_hash_data(ops_parse_info_t *pinfo,const void *data,
2743                          size_t length)
2744     {
2745     size_t n;
2746
2747     for(n=0 ; n < pinfo->nhashes ; ++n)
2748         pinfo->hashes[n].hash.add(&pinfo->hashes[n].hash,data,length);
2749     }
2750
2751 ops_hash_t *ops_parse_hash_find(ops_parse_info_t *pinfo,
2752                                 const unsigned char keyid[OPS_KEY_ID_SIZE])
2753     {
2754     size_t n;
2755
2756     for(n=0 ; n < pinfo->nhashes ; ++n)
2757         if(!memcmp(pinfo->hashes[n].keyid,keyid,OPS_KEY_ID_SIZE))
2758             return &pinfo->hashes[n].hash;
2759     return NULL;
2760     }
2761
2762 /* vim:set textwidth=120: */
2763 /* vim:set ts=8: */
Note: See TracBrowser for help on using the browser.