xref: /netbsd-src/crypto/external/bsd/netpgp/dist/src/lib/packet-parse.h (revision 9ee9e0d7de4c59c936a17df52be682915dc66f43)
1 /*
2  * Copyright (c) 2005-2008 Nominet UK (www.nic.uk)
3  * All rights reserved.
4  * Contributors: Ben Laurie, Rachel Willmer. The Contributors have asserted
5  * their moral rights under the UK Copyright Design and Patents Act 1988 to
6  * be recorded as the authors of this copyright work.
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
9  * use this file except in compliance with the License.
10  *
11  * You may obtain a copy of the License at
12  *     http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  *
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */
21 
22 /** \file
23  * Parser for OpenPGP packets - headers.
24  */
25 
26 #ifndef OPS_PACKET_PARSE_H
27 #define OPS_PACKET_PARSE_H
28 
29 #include "types.h"
30 #include "packet.h"
31 
32 /** __ops_region_t */
33 typedef struct __ops_region_t {
34 	struct __ops_region_t *parent;
35 	unsigned        length;
36 	unsigned        length_read;
37 	unsigned        last_read;	/* !< length of last read, only valid
38 					 * in deepest child */
39 	unsigned   indeterminate:1;
40 }               __ops_region_t;
41 
42 void            __ops_init_subregion(__ops_region_t * subregion, __ops_region_t * region);
43 
44 /** __ops_parse_callback_return_t */
45 typedef enum {
46 	OPS_RELEASE_MEMORY,
47 	OPS_KEEP_MEMORY,
48 	OPS_FINISHED
49 }               __ops_parse_cb_return_t;
50 
51 typedef struct __ops_parse_cb_info __ops_parse_cb_info_t;
52 
53 typedef         __ops_parse_cb_return_t __ops_parse_cb_t(const __ops_parser_content_t *, __ops_parse_cb_info_t *);
54 
55 __ops_parse_cb_return_t
56 get_passphrase_cb(const __ops_parser_content_t *, __ops_parse_cb_info_t *);
57 
58 typedef struct __ops_parse_info __ops_parse_info_t;
59 typedef struct __ops_reader_info __ops_reader_info_t;
60 typedef struct __ops_crypt_info __ops_crypt_info_t;
61 
62 /*
63    A reader MUST read at least one byte if it can, and should read up
64    to the number asked for. Whether it reads more for efficiency is
65    its own decision, but if it is a stacked reader it should never
66    read more than the length of the region it operates in (which it
67    would have to be given when it is stacked).
68 
69    If a read is short because of EOF, then it should return the short
70    read (obviously this will be zero on the second attempt, if not the
71    first). Because a reader is not obliged to do a full read, only a
72    zero return can be taken as an indication of EOF.
73 
74    If there is an error, then the callback should be notified, the
75    error stacked, and -1 should be returned.
76 
77    Note that although length is a size_t, a reader will never be asked
78    to read more than INT_MAX in one go.
79 
80  */
81 
82 typedef int __ops_reader_t(void *, size_t, __ops_error_t **, __ops_reader_info_t *, __ops_parse_cb_info_t *);
83 
84 typedef void    __ops_reader_destroyer_t(__ops_reader_info_t *);
85 
86 __ops_parse_info_t *__ops_parse_info_new(void);
87 void            __ops_parse_info_delete(__ops_parse_info_t *);
88 __ops_error_t    *__ops_parse_info_get_errors(__ops_parse_info_t *);
89 __ops_crypt_t    *__ops_parse_get_decrypt(__ops_parse_info_t *);
90 
91 void            __ops_parse_cb_set(__ops_parse_info_t *, __ops_parse_cb_t *, void *);
92 void            __ops_parse_cb_push(__ops_parse_info_t *, __ops_parse_cb_t *, void *);
93 void           *__ops_parse_cb_get_arg(__ops_parse_cb_info_t *);
94 void           *__ops_parse_cb_get_errors(__ops_parse_cb_info_t *);
95 void            __ops_reader_set(__ops_parse_info_t *, __ops_reader_t *, __ops_reader_destroyer_t *, void *);
96 void            __ops_reader_push(__ops_parse_info_t *, __ops_reader_t *, __ops_reader_destroyer_t *, void *);
97 void            __ops_reader_pop(__ops_parse_info_t *);
98 
99 void           *__ops_reader_get_arg(__ops_reader_info_t *);
100 
101 __ops_parse_cb_return_t __ops_parse_cb(const __ops_parser_content_t *, __ops_parse_cb_info_t *);
102 __ops_parse_cb_return_t __ops_parse_stacked_cb(const __ops_parser_content_t *, __ops_parse_cb_info_t *);
103 __ops_reader_info_t *__ops_parse_get_rinfo(__ops_parse_info_t *);
104 
105 int             __ops_parse(__ops_parse_info_t *);
106 int             __ops_parse_and_print_errors(__ops_parse_info_t *);
107 
108 void            __ops_parse_and_validate(__ops_parse_info_t *);
109 
110 /** Used to specify whether subpackets should be returned raw, parsed or ignored.
111  */
112 typedef enum {
113 	OPS_PARSE_RAW,		/* !< Callback Raw */
114 	OPS_PARSE_PARSED,	/* !< Callback Parsed */
115 	OPS_PARSE_IGNORE	/* !< Don't callback */
116 }               __ops_parse_type_t;
117 
118 void
119 __ops_parse_options(__ops_parse_info_t *, __ops_content_tag_t, __ops_parse_type_t);
120 
121 bool __ops_limited_read(unsigned char *, size_t, __ops_region_t *, __ops_error_t **, __ops_reader_info_t *, __ops_parse_cb_info_t *);
122 bool __ops_stacked_limited_read(unsigned char *, unsigned, __ops_region_t *, __ops_error_t **, __ops_reader_info_t *, __ops_parse_cb_info_t *);
123 void __ops_parse_hash_init(__ops_parse_info_t *, __ops_hash_algorithm_t, const unsigned char *);
124 void __ops_parse_hash_data(__ops_parse_info_t *, const void *, size_t);
125 void            __ops_parse_hash_finish(__ops_parse_info_t *);
126 __ops_hash_t     *__ops_parse_hash_find(__ops_parse_info_t *, const unsigned char *);
127 
128 __ops_reader_t    __ops_stacked_read;
129 
130 int __ops_decompress(__ops_region_t *, __ops_parse_info_t *, __ops_compression_type_t);
131 bool __ops_write_compressed(const unsigned char *, const unsigned int, __ops_create_info_t *);
132 
133 #endif
134