xref: /netbsd-src/crypto/external/bsd/netpgp/dist/src/lib/packet-parse.h (revision b0df0a2281017af482adf3467149a39a38762ac8)
12232f800Sagc /*-
22232f800Sagc  * Copyright (c) 2009 The NetBSD Foundation, Inc.
32232f800Sagc  * All rights reserved.
42232f800Sagc  *
52232f800Sagc  * This code is derived from software contributed to The NetBSD Foundation
62232f800Sagc  * by Alistair Crooks (agc@NetBSD.org)
72232f800Sagc  *
82232f800Sagc  * Redistribution and use in source and binary forms, with or without
92232f800Sagc  * modification, are permitted provided that the following conditions
102232f800Sagc  * are met:
112232f800Sagc  * 1. Redistributions of source code must retain the above copyright
122232f800Sagc  *    notice, this list of conditions and the following disclaimer.
132232f800Sagc  * 2. Redistributions in binary form must reproduce the above copyright
142232f800Sagc  *    notice, this list of conditions and the following disclaimer in the
152232f800Sagc  *    documentation and/or other materials provided with the distribution.
162232f800Sagc  *
172232f800Sagc  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
182232f800Sagc  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
192232f800Sagc  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
202232f800Sagc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
212232f800Sagc  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
222232f800Sagc  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
232232f800Sagc  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
242232f800Sagc  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
252232f800Sagc  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
262232f800Sagc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
272232f800Sagc  * POSSIBILITY OF SUCH DAMAGE.
282232f800Sagc  */
2993bf6008Sagc /*
3093bf6008Sagc  * Copyright (c) 2005-2008 Nominet UK (www.nic.uk)
3193bf6008Sagc  * All rights reserved.
3293bf6008Sagc  * Contributors: Ben Laurie, Rachel Willmer. The Contributors have asserted
3393bf6008Sagc  * their moral rights under the UK Copyright Design and Patents Act 1988 to
3493bf6008Sagc  * be recorded as the authors of this copyright work.
3593bf6008Sagc  *
3693bf6008Sagc  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
3793bf6008Sagc  * use this file except in compliance with the License.
3893bf6008Sagc  *
3993bf6008Sagc  * You may obtain a copy of the License at
4093bf6008Sagc  *     http://www.apache.org/licenses/LICENSE-2.0
4193bf6008Sagc  *
4293bf6008Sagc  * Unless required by applicable law or agreed to in writing, software
4393bf6008Sagc  * distributed under the License is distributed on an "AS IS" BASIS,
4493bf6008Sagc  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
4593bf6008Sagc  *
4693bf6008Sagc  * See the License for the specific language governing permissions and
4793bf6008Sagc  * limitations under the License.
4893bf6008Sagc  */
4993bf6008Sagc 
5093bf6008Sagc /** \file
5193bf6008Sagc  * Parser for OpenPGP packets - headers.
5293bf6008Sagc  */
5393bf6008Sagc 
544b3a3e18Sagc #ifndef PACKET_PARSE_H_
554b3a3e18Sagc #define PACKET_PARSE_H_
5693bf6008Sagc 
5793bf6008Sagc #include "types.h"
5893bf6008Sagc #include "packet.h"
5993bf6008Sagc 
60fc1f8641Sagc /** pgp_region_t */
61fc1f8641Sagc typedef struct pgp_region_t {
62fc1f8641Sagc 	struct pgp_region_t	*parent;
6393bf6008Sagc 	unsigned		 length;
6457324b9fSagc 	unsigned		 readc;		/* length read */
6557324b9fSagc 	unsigned		 last_read;
6657324b9fSagc 		/* length of last read, only valid in deepest child */
6793bf6008Sagc 	unsigned		 indeterminate:1;
68fc1f8641Sagc } pgp_region_t;
6993bf6008Sagc 
70fc1f8641Sagc void pgp_init_subregion(pgp_region_t *, pgp_region_t *);
7193bf6008Sagc 
72fc1f8641Sagc /** pgp_cb_ret_t */
7393bf6008Sagc typedef enum {
74fc1f8641Sagc 	PGP_RELEASE_MEMORY,
75fc1f8641Sagc 	PGP_KEEP_MEMORY,
76fc1f8641Sagc 	PGP_FINISHED
77fc1f8641Sagc } pgp_cb_ret_t;
7893bf6008Sagc 
79fc1f8641Sagc typedef struct pgp_cbdata_t	 pgp_cbdata_t;
8093bf6008Sagc 
81fc1f8641Sagc typedef pgp_cb_ret_t pgp_cbfunc_t(const pgp_packet_t *,
82fc1f8641Sagc 					pgp_cbdata_t *);
8393bf6008Sagc 
84fc1f8641Sagc pgp_cb_ret_t
85fc1f8641Sagc get_passphrase_cb(const pgp_packet_t *, pgp_cbdata_t *);
8693bf6008Sagc 
87fc1f8641Sagc typedef struct pgp_stream_t	pgp_stream_t;
88fc1f8641Sagc typedef struct pgp_reader_t		pgp_reader_t;
89fc1f8641Sagc typedef struct pgp_cryptinfo_t	pgp_cryptinfo_t;
9093bf6008Sagc 
9193bf6008Sagc /*
9293bf6008Sagc    A reader MUST read at least one byte if it can, and should read up
9393bf6008Sagc    to the number asked for. Whether it reads more for efficiency is
9493bf6008Sagc    its own decision, but if it is a stacked reader it should never
9593bf6008Sagc    read more than the length of the region it operates in (which it
9693bf6008Sagc    would have to be given when it is stacked).
9793bf6008Sagc 
9893bf6008Sagc    If a read is short because of EOF, then it should return the short
9993bf6008Sagc    read (obviously this will be zero on the second attempt, if not the
10093bf6008Sagc    first). Because a reader is not obliged to do a full read, only a
10193bf6008Sagc    zero return can be taken as an indication of EOF.
10293bf6008Sagc 
10393bf6008Sagc    If there is an error, then the callback should be notified, the
10493bf6008Sagc    error stacked, and -1 should be returned.
10593bf6008Sagc 
10693bf6008Sagc    Note that although length is a size_t, a reader will never be asked
10793bf6008Sagc    to read more than INT_MAX in one go.
10893bf6008Sagc 
10993bf6008Sagc  */
110*b0df0a22Sagc typedef int pgp_reader_func_t(pgp_stream_t *, void *, size_t, pgp_error_t **,
111fc1f8641Sagc 				pgp_reader_t *, pgp_cbdata_t *);
11293bf6008Sagc 
113fc1f8641Sagc typedef void pgp_reader_destroyer_t(pgp_reader_t *);
11493bf6008Sagc 
115fc1f8641Sagc void pgp_stream_delete(pgp_stream_t *);
116fc1f8641Sagc pgp_error_t *pgp_stream_get_errors(pgp_stream_t *);
117fc1f8641Sagc pgp_crypt_t *pgp_get_decrypt(pgp_stream_t *);
11893bf6008Sagc 
119fc1f8641Sagc void pgp_set_callback(pgp_stream_t *, pgp_cbfunc_t *, void *);
120fc1f8641Sagc void pgp_callback_push(pgp_stream_t *, pgp_cbfunc_t *, void *);
121fc1f8641Sagc void *pgp_callback_arg(pgp_cbdata_t *);
122fc1f8641Sagc void *pgp_callback_errors(pgp_cbdata_t *);
123fc1f8641Sagc void pgp_reader_set(pgp_stream_t *, pgp_reader_func_t *,
124fc1f8641Sagc 			pgp_reader_destroyer_t *, void *);
125fc1f8641Sagc void pgp_reader_push(pgp_stream_t *, pgp_reader_func_t *,
126fc1f8641Sagc 			pgp_reader_destroyer_t *, void *);
127fc1f8641Sagc void pgp_reader_pop(pgp_stream_t *);
12893bf6008Sagc 
129fc1f8641Sagc void *pgp_reader_get_arg(pgp_reader_t *);
13093bf6008Sagc 
131fc1f8641Sagc pgp_cb_ret_t pgp_callback(const pgp_packet_t *,
132fc1f8641Sagc 					pgp_cbdata_t *);
133fc1f8641Sagc pgp_cb_ret_t pgp_stacked_callback(const pgp_packet_t *,
134fc1f8641Sagc 					pgp_cbdata_t *);
135fc1f8641Sagc pgp_reader_t *pgp_readinfo(pgp_stream_t *);
13693bf6008Sagc 
137fc1f8641Sagc int pgp_parse(pgp_stream_t *, const int);
13893bf6008Sagc 
13957324b9fSagc /** Used to specify whether subpackets should be returned raw, parsed
14057324b9fSagc * or ignored.  */
14193bf6008Sagc typedef enum {
142fc1f8641Sagc 	PGP_PARSE_RAW,		/* Callback Raw */
143fc1f8641Sagc 	PGP_PARSE_PARSED,	/* Callback Parsed */
144fc1f8641Sagc 	PGP_PARSE_IGNORE	/* Don't callback */
145fc1f8641Sagc } pgp_parse_type_t;
14693bf6008Sagc 
147fc1f8641Sagc void pgp_parse_options(pgp_stream_t *, pgp_content_enum,
148fc1f8641Sagc 			pgp_parse_type_t);
14993bf6008Sagc 
150*b0df0a22Sagc unsigned pgp_limited_read(pgp_stream_t *, uint8_t *, size_t, pgp_region_t *,
151fc1f8641Sagc 			pgp_error_t **, pgp_reader_t *,
152fc1f8641Sagc 			pgp_cbdata_t *);
153*b0df0a22Sagc unsigned pgp_stacked_limited_read(pgp_stream_t *, uint8_t *, unsigned,
154fc1f8641Sagc 			pgp_region_t *, pgp_error_t **,
155fc1f8641Sagc 			pgp_reader_t *, pgp_cbdata_t *);
156fc1f8641Sagc void pgp_parse_hash_init(pgp_stream_t *, pgp_hash_alg_t,
157b15ec256Sagc 			const uint8_t *);
158fc1f8641Sagc void pgp_parse_hash_data(pgp_stream_t *, const void *, size_t);
159fc1f8641Sagc void pgp_parse_hash_finish(pgp_stream_t *);
160fc1f8641Sagc pgp_hash_t *pgp_parse_hash_find(pgp_stream_t *, const uint8_t *);
16193bf6008Sagc 
162fc1f8641Sagc pgp_reader_func_t    pgp_stacked_read;
16393bf6008Sagc 
164fc1f8641Sagc int pgp_decompress(pgp_region_t *, pgp_stream_t *,
165fc1f8641Sagc 			pgp_compression_type_t);
166fc1f8641Sagc unsigned pgp_writez(pgp_output_t *, const uint8_t *,
167b15ec256Sagc 			const unsigned);
16893bf6008Sagc 
1694b3a3e18Sagc #endif /* PACKET_PARSE_H_ */
170