1*ebfedea0SLionel Sambuc /*- 2*ebfedea0SLionel Sambuc * Copyright (c) 2009 The NetBSD Foundation, Inc. 3*ebfedea0SLionel Sambuc * All rights reserved. 4*ebfedea0SLionel Sambuc * 5*ebfedea0SLionel Sambuc * This code is derived from software contributed to The NetBSD Foundation 6*ebfedea0SLionel Sambuc * by Alistair Crooks (agc@NetBSD.org) 7*ebfedea0SLionel Sambuc * 8*ebfedea0SLionel Sambuc * Redistribution and use in source and binary forms, with or without 9*ebfedea0SLionel Sambuc * modification, are permitted provided that the following conditions 10*ebfedea0SLionel Sambuc * are met: 11*ebfedea0SLionel Sambuc * 1. Redistributions of source code must retain the above copyright 12*ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer. 13*ebfedea0SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright 14*ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer in the 15*ebfedea0SLionel Sambuc * documentation and/or other materials provided with the distribution. 16*ebfedea0SLionel Sambuc * 17*ebfedea0SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 18*ebfedea0SLionel Sambuc * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 19*ebfedea0SLionel Sambuc * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 20*ebfedea0SLionel Sambuc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 21*ebfedea0SLionel Sambuc * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22*ebfedea0SLionel Sambuc * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23*ebfedea0SLionel Sambuc * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24*ebfedea0SLionel Sambuc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25*ebfedea0SLionel Sambuc * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26*ebfedea0SLionel Sambuc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27*ebfedea0SLionel Sambuc * POSSIBILITY OF SUCH DAMAGE. 28*ebfedea0SLionel Sambuc */ 29*ebfedea0SLionel Sambuc /* 30*ebfedea0SLionel Sambuc * Copyright (c) 2005-2008 Nominet UK (www.nic.uk) 31*ebfedea0SLionel Sambuc * All rights reserved. 32*ebfedea0SLionel Sambuc * Contributors: Ben Laurie, Rachel Willmer. The Contributors have asserted 33*ebfedea0SLionel Sambuc * their moral rights under the UK Copyright Design and Patents Act 1988 to 34*ebfedea0SLionel Sambuc * be recorded as the authors of this copyright work. 35*ebfedea0SLionel Sambuc * 36*ebfedea0SLionel Sambuc * Licensed under the Apache License, Version 2.0 (the "License"); you may not 37*ebfedea0SLionel Sambuc * use this file except in compliance with the License. 38*ebfedea0SLionel Sambuc * 39*ebfedea0SLionel Sambuc * You may obtain a copy of the License at 40*ebfedea0SLionel Sambuc * http://www.apache.org/licenses/LICENSE-2.0 41*ebfedea0SLionel Sambuc * 42*ebfedea0SLionel Sambuc * Unless required by applicable law or agreed to in writing, software 43*ebfedea0SLionel Sambuc * distributed under the License is distributed on an "AS IS" BASIS, 44*ebfedea0SLionel Sambuc * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 45*ebfedea0SLionel Sambuc * 46*ebfedea0SLionel Sambuc * See the License for the specific language governing permissions and 47*ebfedea0SLionel Sambuc * limitations under the License. 48*ebfedea0SLionel Sambuc */ 49*ebfedea0SLionel Sambuc 50*ebfedea0SLionel Sambuc /** \file 51*ebfedea0SLionel Sambuc * Parser for OpenPGP packets - headers. 52*ebfedea0SLionel Sambuc */ 53*ebfedea0SLionel Sambuc 54*ebfedea0SLionel Sambuc #ifndef PACKET_PARSE_H_ 55*ebfedea0SLionel Sambuc #define PACKET_PARSE_H_ 56*ebfedea0SLionel Sambuc 57*ebfedea0SLionel Sambuc #include "types.h" 58*ebfedea0SLionel Sambuc #include "packet.h" 59*ebfedea0SLionel Sambuc 60*ebfedea0SLionel Sambuc /** pgp_region_t */ 61*ebfedea0SLionel Sambuc typedef struct pgp_region_t { 62*ebfedea0SLionel Sambuc struct pgp_region_t *parent; 63*ebfedea0SLionel Sambuc unsigned length; 64*ebfedea0SLionel Sambuc unsigned readc; /* length read */ 65*ebfedea0SLionel Sambuc unsigned last_read; 66*ebfedea0SLionel Sambuc /* length of last read, only valid in deepest child */ 67*ebfedea0SLionel Sambuc unsigned indeterminate:1; 68*ebfedea0SLionel Sambuc } pgp_region_t; 69*ebfedea0SLionel Sambuc 70*ebfedea0SLionel Sambuc void pgp_init_subregion(pgp_region_t *, pgp_region_t *); 71*ebfedea0SLionel Sambuc 72*ebfedea0SLionel Sambuc /** pgp_cb_ret_t */ 73*ebfedea0SLionel Sambuc typedef enum { 74*ebfedea0SLionel Sambuc PGP_RELEASE_MEMORY, 75*ebfedea0SLionel Sambuc PGP_KEEP_MEMORY, 76*ebfedea0SLionel Sambuc PGP_FINISHED 77*ebfedea0SLionel Sambuc } pgp_cb_ret_t; 78*ebfedea0SLionel Sambuc 79*ebfedea0SLionel Sambuc typedef struct pgp_cbdata_t pgp_cbdata_t; 80*ebfedea0SLionel Sambuc 81*ebfedea0SLionel Sambuc typedef pgp_cb_ret_t pgp_cbfunc_t(const pgp_packet_t *, 82*ebfedea0SLionel Sambuc pgp_cbdata_t *); 83*ebfedea0SLionel Sambuc 84*ebfedea0SLionel Sambuc pgp_cb_ret_t 85*ebfedea0SLionel Sambuc get_passphrase_cb(const pgp_packet_t *, pgp_cbdata_t *); 86*ebfedea0SLionel Sambuc 87*ebfedea0SLionel Sambuc typedef struct pgp_stream_t pgp_stream_t; 88*ebfedea0SLionel Sambuc typedef struct pgp_reader_t pgp_reader_t; 89*ebfedea0SLionel Sambuc typedef struct pgp_cryptinfo_t pgp_cryptinfo_t; 90*ebfedea0SLionel Sambuc 91*ebfedea0SLionel Sambuc /* 92*ebfedea0SLionel Sambuc A reader MUST read at least one byte if it can, and should read up 93*ebfedea0SLionel Sambuc to the number asked for. Whether it reads more for efficiency is 94*ebfedea0SLionel Sambuc its own decision, but if it is a stacked reader it should never 95*ebfedea0SLionel Sambuc read more than the length of the region it operates in (which it 96*ebfedea0SLionel Sambuc would have to be given when it is stacked). 97*ebfedea0SLionel Sambuc 98*ebfedea0SLionel Sambuc If a read is short because of EOF, then it should return the short 99*ebfedea0SLionel Sambuc read (obviously this will be zero on the second attempt, if not the 100*ebfedea0SLionel Sambuc first). Because a reader is not obliged to do a full read, only a 101*ebfedea0SLionel Sambuc zero return can be taken as an indication of EOF. 102*ebfedea0SLionel Sambuc 103*ebfedea0SLionel Sambuc If there is an error, then the callback should be notified, the 104*ebfedea0SLionel Sambuc error stacked, and -1 should be returned. 105*ebfedea0SLionel Sambuc 106*ebfedea0SLionel Sambuc Note that although length is a size_t, a reader will never be asked 107*ebfedea0SLionel Sambuc to read more than INT_MAX in one go. 108*ebfedea0SLionel Sambuc 109*ebfedea0SLionel Sambuc */ 110*ebfedea0SLionel Sambuc typedef int pgp_reader_func_t(pgp_stream_t *, void *, size_t, pgp_error_t **, 111*ebfedea0SLionel Sambuc pgp_reader_t *, pgp_cbdata_t *); 112*ebfedea0SLionel Sambuc 113*ebfedea0SLionel Sambuc typedef void pgp_reader_destroyer_t(pgp_reader_t *); 114*ebfedea0SLionel Sambuc 115*ebfedea0SLionel Sambuc void pgp_stream_delete(pgp_stream_t *); 116*ebfedea0SLionel Sambuc pgp_error_t *pgp_stream_get_errors(pgp_stream_t *); 117*ebfedea0SLionel Sambuc pgp_crypt_t *pgp_get_decrypt(pgp_stream_t *); 118*ebfedea0SLionel Sambuc 119*ebfedea0SLionel Sambuc void pgp_set_callback(pgp_stream_t *, pgp_cbfunc_t *, void *); 120*ebfedea0SLionel Sambuc void pgp_callback_push(pgp_stream_t *, pgp_cbfunc_t *, void *); 121*ebfedea0SLionel Sambuc void *pgp_callback_arg(pgp_cbdata_t *); 122*ebfedea0SLionel Sambuc void *pgp_callback_errors(pgp_cbdata_t *); 123*ebfedea0SLionel Sambuc void pgp_reader_set(pgp_stream_t *, pgp_reader_func_t *, 124*ebfedea0SLionel Sambuc pgp_reader_destroyer_t *, void *); 125*ebfedea0SLionel Sambuc void pgp_reader_push(pgp_stream_t *, pgp_reader_func_t *, 126*ebfedea0SLionel Sambuc pgp_reader_destroyer_t *, void *); 127*ebfedea0SLionel Sambuc void pgp_reader_pop(pgp_stream_t *); 128*ebfedea0SLionel Sambuc 129*ebfedea0SLionel Sambuc void *pgp_reader_get_arg(pgp_reader_t *); 130*ebfedea0SLionel Sambuc 131*ebfedea0SLionel Sambuc pgp_cb_ret_t pgp_callback(const pgp_packet_t *, 132*ebfedea0SLionel Sambuc pgp_cbdata_t *); 133*ebfedea0SLionel Sambuc pgp_cb_ret_t pgp_stacked_callback(const pgp_packet_t *, 134*ebfedea0SLionel Sambuc pgp_cbdata_t *); 135*ebfedea0SLionel Sambuc pgp_reader_t *pgp_readinfo(pgp_stream_t *); 136*ebfedea0SLionel Sambuc 137*ebfedea0SLionel Sambuc int pgp_parse(pgp_stream_t *, const int); 138*ebfedea0SLionel Sambuc 139*ebfedea0SLionel Sambuc /** Used to specify whether subpackets should be returned raw, parsed 140*ebfedea0SLionel Sambuc * or ignored. */ 141*ebfedea0SLionel Sambuc typedef enum { 142*ebfedea0SLionel Sambuc PGP_PARSE_RAW, /* Callback Raw */ 143*ebfedea0SLionel Sambuc PGP_PARSE_PARSED, /* Callback Parsed */ 144*ebfedea0SLionel Sambuc PGP_PARSE_IGNORE /* Don't callback */ 145*ebfedea0SLionel Sambuc } pgp_parse_type_t; 146*ebfedea0SLionel Sambuc 147*ebfedea0SLionel Sambuc void pgp_parse_options(pgp_stream_t *, pgp_content_enum, 148*ebfedea0SLionel Sambuc pgp_parse_type_t); 149*ebfedea0SLionel Sambuc 150*ebfedea0SLionel Sambuc unsigned pgp_limited_read(pgp_stream_t *, uint8_t *, size_t, pgp_region_t *, 151*ebfedea0SLionel Sambuc pgp_error_t **, pgp_reader_t *, 152*ebfedea0SLionel Sambuc pgp_cbdata_t *); 153*ebfedea0SLionel Sambuc unsigned pgp_stacked_limited_read(pgp_stream_t *, uint8_t *, unsigned, 154*ebfedea0SLionel Sambuc pgp_region_t *, pgp_error_t **, 155*ebfedea0SLionel Sambuc pgp_reader_t *, pgp_cbdata_t *); 156*ebfedea0SLionel Sambuc void pgp_parse_hash_init(pgp_stream_t *, pgp_hash_alg_t, 157*ebfedea0SLionel Sambuc const uint8_t *); 158*ebfedea0SLionel Sambuc void pgp_parse_hash_data(pgp_stream_t *, const void *, size_t); 159*ebfedea0SLionel Sambuc void pgp_parse_hash_finish(pgp_stream_t *); 160*ebfedea0SLionel Sambuc pgp_hash_t *pgp_parse_hash_find(pgp_stream_t *, const uint8_t *); 161*ebfedea0SLionel Sambuc 162*ebfedea0SLionel Sambuc pgp_reader_func_t pgp_stacked_read; 163*ebfedea0SLionel Sambuc 164*ebfedea0SLionel Sambuc int pgp_decompress(pgp_region_t *, pgp_stream_t *, 165*ebfedea0SLionel Sambuc pgp_compression_type_t); 166*ebfedea0SLionel Sambuc unsigned pgp_writez(pgp_output_t *, const uint8_t *, 167*ebfedea0SLionel Sambuc const unsigned); 168*ebfedea0SLionel Sambuc 169*ebfedea0SLionel Sambuc #endif /* PACKET_PARSE_H_ */ 170